Home > OS >  bash scripting - print only specific rows in file
bash scripting - print only specific rows in file

Time:08-29

I have the following file (file.txt):

Ex1=-1.397311E 03 WEIGHT=    8.123000E-01
Ex2= 1.952565E 03 WEIGHT=    1.204200E 00
Ex3=-2.737476E 03 WEIGHT=    9.174491E-01
Ex4=-1.703835E 03 WEIGHT=    3.339300E-01
Ey1= 8.098997E 01 WEIGHT=    1.000000E 00
Ey2= 1.812711E 03 WEIGHT=    1.977300E 00
Ex5= 1.654618E 03 WEIGHT=    6.365700E-02
Ey3=-5.958670E 02 WEIGHT=    1.480850E 00
Ey4= 0.000000E 00 WEIGHT=    0.000000E 00
Ey5= 0.000000E 00 WEIGHT=    1.000000E 00
Ex6=-5.413463E 03 WEIGHT=    3.998912E-02
Ex7= 5.537537E 02 WEIGHT=    1.583826E 00

I want to print first and second column from rows beginning with Ex1, Ex2, Ex3, Ex4, Ex5, Ex6, Ex7. I've tried to do this using sed command, but I have many problems in this area. Could you help me ?

CodePudding user response:

$ awk '/^Ex[1-7]=/' file 

should do.

CodePudding user response:

It's not entirely clear what you mean by "column", but I suspect you want one of the following:

$ awk '/Ex[1-7]/{print $1, $2}' FS='[= ]*' OFS== file.txt
Ex1=-1.397311E 03
Ex2=1.952565E 03
Ex3=-2.737476E 03
Ex4=-1.703835E 03
Ex5=1.654618E 03
Ex6=-5.413463E 03
Ex7=5.537537E 02
$ awk '/Ex[1-7]/{print $2, $4}' FS='[= ]*' file.txt
-1.397311E 03 8.123000E-01
1.952565E 03 1.204200E 00
-2.737476E 03 9.174491E-01
-1.703835E 03 3.339300E-01
1.654618E 03 6.365700E-02
-5.413463E 03 3.998912E-02
5.537537E 02 1.583826E 00
  • Related