Print everything except fields in awk

I have a large file with hundreds of columns in which I want to delete only the third and fourth columns and print the rest in the file. My initial idea was to make awk script like awk '{print $1, $2, for (i=$5; i <= NF; i++) print $i }' file > outfile . However, this code does not work.

Then I tried:

 awk '{for(i = 1; i<=NF; i++) if(i == 3 || i == 4) continue else print($i)}' file > outfile 

But it just printed everything in one field. It would be possible to split this into two scripts and combine them with unix paste , but this is similar to what should be done on one line.

+7
source share
6 answers

Your first attempt was pretty close. Modify it to use printf and include field separators in it:

 awk '{printf $1FS$2; for (i=5; i <= NF; i++) printf FS$i; print NL }' 
+9
source

Let's say you have a tab delimited file that looks like this:

temp.txt

field1 field2 field3 field4 field5 field6
field1 field2 field3 field4 field5 field6
field1 field2 field3 field4 field5 field6

starting the next one will delete field 3 and 4 and be displayed at the end of the line.

awk '{print $1"\t"$2"\t"substr($0, index($0,$5))}' temp.txt

field1 field2 field5 field6
field1 field2 field5 field6
field1 field2 field5 field6

My example is print to stdout. > newFile will send stdout to newFile, and >> newFile will add newFile.

Therefore, you can use the following:

awk '{print $1"\t"$2"\t"substr($0, index($0,$5))}' temp.txt > newFile.txt

some will argue that cut

cut -f1,2,5- temp.txt

which produce the same output, and the slice is great for simplicity, but does not handle conflicting delimiters. For example, a mixture of different spaces. However, in this case, cut may be what you need.

you can also do this in perl, python, ruby ​​and many others, but here is the simplest awk solution.

+6
source

Something like:

 cat SOURCEFILE | cut -f1-2,5- >> DESTFILE 

It prints the first two columns, skips the 3rd and 4th, and then prints from 5 to the end.

+6
source

How to simply set the third and fourth columns to an empty row:

 echo 1 2 3 4 5 6 7 8 9 10 | awk -F" " '{ $3=""; $4=""; print}' 
0
source

Yes, you can simply set the third and fourth columns to an empty row; but, in addition, the $1 field must be set to itself ( $1=$1 ) so that awk actually consumes the input field separator (separator) : on the entire current line $0 at a time.

 echo 1:2:3:4:5:6:7:8:9:10 | awk -F: '{ $1=$1; $3=""; $4=""; print $0}' 
0
source

A difficult but general way (forget about a simple oneliner)

 awk -v "Exclude=3:4:5" ' # load exclusion BEGIN{ Count=split(Exclude, aTmp, ":") for( i = 1; i <= Count; i++) aExc[ aTmp[ i]]=1 } # treat each line, taking only wanted field { Result="" for( i = 1; i <= NF; i++) { # field to take ? if( ! aExc[ i]) { # first element or add a separator before if( Result != "") Result=Result OFS $i else Result=$i } } print Result }' YourFile 
  • you can specify any field that you want to exclude
    • index of the fill field in varaible Exclude separate in the first row
    Delimiter
  • true in place quantity
  • The code is "extended" for a better understanding.
  • the final result is not quite like an input (without an excluded field), because the output separator is used instead of the original separator (ex 2 space or tab changes to 1 space with default behavior)
0
source

All Articles