awk or sed to find specific column from different files

Hi everybody,

I have a folder with many files:

Files with 8 columns:

X 123 A B C D E F

And files with 7 columns:

X1234 A B C D E F

I am trying to find a way to extract the 5th column when the files have eight columns, or the 4th column when the files have seven columns.

Any suggestions will be much appreciated

T

Like this?

$ cat file7
aa bb cc me! ee ff gg
$ cat file8
aa bb cc dd me! ff gg hh
$ awk 'NF==7 {print FILENAME, $4; next} {print FILENAME, $5}' file*
file7 me!
file8 me!
1 Like

or

awk '{print $(NF-3)}' file
1 Like

Many thanks pamu and zaxxon,

I first try pamu's solution and works. very smart :slight_smile:
I will try and zaxxon's of course.

In any case many thanks!!!