Print field "N" to the end of the line

I would like to have help or direction on the issue that I have in awk.

I have a tab delimited file with more than 5 fields. I want to display fields excluding the first 5 fields.

Could you tell me how to write an awk script to complete this task?

Best, jianfeng.mao

Pay attention to the following kind of comments:

There are a lot of fields in my files. Different lines have a different number of fields. The number of fields per row is not standard.

+2
source share
4 answers

I agree with the mathematical suggestion to use cut : this is the right tool for this job. But if it only becomes 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; } 
+3
source

In my temp.txt tab delimited temp.txt it looks like this

field1 field2 field3 field4 field5 field6
field1 field2 field3 field4 field5 field6 field7
field1 field2 field3 field4 field5 field6 field7 field 8

According to your update, I highly recommend using cut :

 cut -f6- temp.txt 

prints field 6 to the end of the line.

Note -d indicates the delimiter, but the tab is the default delimiter. You can do this in awk , but I find cut easier.

With awk it will look like this:

  awk '{print substr($0, index($0, $6))}' temp.txt 

if my temp.txt tab delimited file looks like this

field1 field2 field3 field4 field5 field6
field1 field2 field3 field4 field5 field6 field7
field1 field2 field3 field4 field5 field6 field7 field 8

 awk -F"\t" '{print $6}' temp.txt 

will only print the 6th box. if the separator is a tab, it will most likely work without setting -F, but I like to set my field separator when I can.

in the same way it will also shrink.

 cut -f6 temp.txt 

I have a suspicion that your question is a little more complicated, so if you answer my comment, I can try to expand my answer. Strike>

+5
source

perl way?

 perl -lane 'splice @F,0,5;print "@F"' 

So,

 echo 'field1 field2 field3 field4 field5 field6' | perl -lane 'splice @F,0,5;print "@F"' 

will create

 field6 
+2
source
 awk -vFS='\t' -vOFS='\t' '{ $1=$2=$3=$4=$5="" print substr($0,6) # delete leading tabs }' 

I use -vFS='\t' rather than -F'\t' because some awk implementations (like BusyBox) don't honor C screens in the latest construct.

+1
source

Source: https://habr.com/ru/post/911768/


All Articles