Skip to content
Advertisement

linux get specific fields from several files

I have several files starting with the string “file” then a number (file1, file2, etc).

The content of these files is similar and looks like this

JavaScript

For each file, I want only keep the index of the element and the 2 numeric fields just after coordinates (in the same file or in another file):

file1:

JavaScript

What I tried to do is like this (but It is not correct)

JavaScript

Advertisement

Answer

This is the perfect use of awk. With awk you can simply print specific words.

If the index is the line number you can use this:

cat -n ./file1 | awk '{print $1 -1 " " $7 " " $8}' this simply prints the files with line numbers and prints the first, seventh and eighth word.

If the index is the $elt_(0) number in parenthese you can use sed like this:

JavaScript

output:

JavaScript

Link to awk Link to sed

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement