Print Field 'N' to End of Line
I agree with matchew's suggestion to use cut
: it's the right tool for this job. But if this is just going to become a part of a larger awk
script, here's how to do it:
awk -F "\t" '{ for (i=6; i<=NF; ++i) $(i-5) = $i; NF = NF-5; print; }
How to print third column to last column?
...or a simpler solution: cut -f 3- INPUTFILE
just add the correct delimiter (-d) and you got the same effect.
Using awk to print all columns from the nth to the last
Print all columns:
awk '{print $0}' somefile
Print all but the first column:
awk '{$1=""; print $0}' somefile
Print all but the first two columns:
awk '{$1=$2=""; print $0}' somefile
AWK command to print until end of line
It may be more straight-forward to use cut
:
$ cut -d' ' -f5- file
This is line one
This is line two
This is line three
This is line four
This says: on space-separated fields, print from the 5th up to the end of the line.
If you happen to have multiple spaces in between fields, you may initially want to squeeze them with tr -s' '
.
Cut from column to end of line
The cut
command lines in your question specify colon-separated fields and that you want the output to consist only of field 7; since there is no 7th field in your input, the result you're getting isn't what you intend.
Since the "From:" prefix appears to be identical across all lines, you can simply cut from the 7th character onward:
egrep '^From:' $file | cut -c7-
and get the result you intend.
How to print a line with no field separator in awk?
Using gnu-awk
:
awk -F '[[:blank:]]*;[[:blank:]]*' '{
for (i=1; i<=NF; i++) uniq[$i]
}
END {
PROCINFO["sorted_in"]="@ind_str_asc"
for (i in uniq)
print i
}' file
a
b
c
d
e
f
g
h
i
For non-gnu awk
use:
awk -F '[[:blank:]]*;[[:blank:]]*' '{for (i=1; i<=NF; i++) uniq[$i]}
END{for (i in uniq) print i}' file | sort
How to add quotes from specific delimiter to end of the line, using awk?
You have a ~
separator at the end of your lines. So, you have an extra empty field after this field separator. You can check this with:
$ awk -F'~' '{print NF "|" $NF "|"}' file.txt
9||
13||
See? When printing this last empty field followed by a ~
, you simply concatenate it to the previous one, thus the ~~
. Try:
$ awk -F '~' -vOFS='~' '{$5 = "\"" $5; $NF = $NF "\""; print}' file.txt
abc~123~xyz~123456~"12~0.12~14~1.1~"
omn~124~xdz~923231~"13~0.0~13~1.1~14~0.45~19~80.1~"
We just declare ~
as the input and output (with variable OFS
) field separator, prepend a "
to the fifth field, append one to the last field, and print.
print the last letter of each word to make a string using `awk` command
If you have just this one single line to handle you can use
awk '{for (i=1;i<=NF;i++) r = r "" substr($i,length($i))} END{print r}' file
If you have multiple lines in the input:
awk '{r=""; for (i=1;i<=NF;i++) r = r "" substr($i,length($i)); print r}' file
Details:
{for (i=1;i<=NF;i++) r = r "" substr($i,length($i))
- iterate over all fields in the current record,i
is the field ID,$i
is the field value, and all last chars of each field (retrieved withsubstr($i,length($i))
) are appended tor
variableEND{print r}
prints ther
variable onceawk
script finishes processing.- In the second solution,
r
value is cleared upon each line processing start, and its value is printed after processing all fields in the current record.
See the online demo:
#!/bin/bash
s='UDACBG UYAZAM DJSUBU WJKMBC NTCGCH DIDEVO RHWDAS'
awk '{for (i=1;i<=NF;i++) r = r "" substr($i,length($1))} END{print r}' <<< "$s"
Output:
GMUCHOS
Related Topics
Less Gets Keyboard Input from Stderr
Hosting Two Website Under One Web App - Azure Services
Linux Why Can't I Pipe Find Result to Rm
Recursively Cat All the Files into Single File
Using 'Find -Perm' to Find When a Permission Is Not Set
How to Dynamically Allocate Memory Using Assembly and System Calls Under Linux
How to Test If Your Linux Support Sse2
How to Make Cscope Display Full File Paths During Search
Configure Error: Could Not Link Against Boost_System
Bash 'Swallowing' Sub-Shell Children Process When Executing a Single Command
Bash Shell Script Variable Assignment
How to Paste Multi-Line Bash Codes into Terminal and Run It All at Once
How to Capture All of My Compiler's Output to a File
How to Find Files Modified in Last X Minutes (Find -Mmin Does Not Work as Expected)