awk one liner to print to end of line

Given:

1,2,whatever,a,940,sot

how can i print from one particular field to the end of line?

awk -F"," '{print $2 - endofline}'

the delimiter just happens to be a comma "," in this case. in other cases, it could be hypens:

1---2---whatever---a---940---sot
# example with three -> end
awk '{ for(i=3; i<=NF; i++) {printf( (i==NF)?"%s\n" : "%s ", $i) } somefile
1 Like

jim mcnamara's proposal needs minor corrections:

$ awk '{ for(i=3; i<=NF; i++) {printf( (i==NF)?"%s\n" : "%s---", $i) }}' FS="---" file
whatever---a---940---sot

You may also want to give this a try:

$ awk '{sub($1FS$2FS,"")}1' FS="---" OFS="---" file
whatever---a---940---sot
1 Like
echo "one,two,three,four,five" | awk -F, '{ for (x=nr; x<NF; x++) {printf $x ","} print $NF }' nr=3
three,four,five
echo "one---two---three---four---five" | awk -F-+ '{ for (x=nr; x<NF; x++) {printf $x "---"} print $NF }' nr=3
three---four---five
1 Like
awk -F, '{s=$f; for(i=f+1; i<=NF; i++) s=s FS $i; print s}' f=2
sed 's/[^,]*,*/�/2; s/.*�//'

if there is just a single character as field separator:

cut -d, -f2-

--
@ jotne, if printf is used without explicitly specifying the format field than this is left to the input data.. Try replacing one of the fields with "%s" for example

2 Likes