Printing selective fields following 1st column instructions

awk

Suppose I have a file in which I'd like to simultaneously print different awk commands following the first column instuctions, without messing up the original file (as it would happen with two separate prints):

File:

End 1st 2nd 3rd
Fin 1st 2nd 3rd

I'd like to combine the following commands into a one-liner:

awk '$1 ~ /^E/ {print $2}'
awk '$1 ~ /^F/ {print $3}'

To obtain the following output:

End 1st
Fin 2nd

EDIT
What I meant by saying "messing up the original file":

File 1:

E1   NAME1 LASTNAME1
FA   22   1992         #age, year
FC   UK   London       #country, city
FJ   IT   HP           #job, company
E2   NAME2 LASTNAME2
FA   25   1989        
FC   CH   Geneva      
FJ   CS   SIB    

Now, if I run two separate awk prints, I won't be able to match the information in File 3 with the names of File 2 (especially if the number of ^F fields are not of the same number):

awk '$1 ~ /^E/ {print $2}' File 1 > File 2

Output (File 2):

NAME1
NAME2

awk '$1 ~ /^F/ {print $3}' File 1 > File 3

Output (File 3):

1992        
London       
HP           
1989        
Geneva      
SIB  

But, If I join them (as suggested in the answers) I'll be able to have something like this:

Expected output:

NAME1
1992        
London       
HP  
NAME2
1989        
Geneva      
SIB  

Best Answer

awk '$1 ~ /^E|^F/ {if ($1 == "End") print $1" "$2; if ($1 == "Fin") print $1" "$3}'

or

awk '/^End/{print $1" "$2}/^Fin/{print $1" "$3}'

(thanks to Jidder)

Should work.

Related Question