Print Field 'N' to End of Line

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 with substr($i,length($i))) are appended to r variable
  • END{print r} prints the r variable once awk 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



Leave a reply



Submit