Ashi
Ashi

Reputation: 389

In-file column deletion

I am trying to delete last 4 column of multiple file. I am using gawk function. Its working for some file. But the in some output file there is nothing or less number of rows than the input file.

  gawk '{print >$NF".txt"} meta.txt| gawk -i inplace '{NF-=4; print}' *.txt

Am I doing anything wrong here??

MODIFICATION/ UPDATES: if both the command run separately without pipe gawk -i inplace work perfectly.

Upvotes: 2

Views: 65

Answers (1)

Ed Morton
Ed Morton

Reputation: 203229

What you're apparently doing doesn't make sense. Instead of creating the files with all fields and then removing the last 4 fields from each file you just created, simply don't print the final 4 fields when you create the files in the first place:

awk '{out=$NF".txt"; NF-=4; print > out}' meta.txt

To not impact field separators in the remaining fields use this instead:

awk '{out=$NF".txt"; sub(/(\s+\S+){4}$/,""); print > out}' meta.txt

Both of the above assume you;re using GNU awk - the first for decrementing NF actually changing $0 and the 2nd for \s/\S shorthand.

Upvotes: 4

Related Questions