Grep command inside awk

Hi,

I would like to use grep command inside awk.

Here is my requirement below :

file.txt

col1     col2  col3 col 4 col 5
wrxwrx 124 jun 3 Sensex.EMEA 
wrxwrx 120 jun 4 Emex.US
wrxwrx 130 feb 3 passion.AUS
wrxwrx 145 feb 9 lession.AUS
wrxwrx 130 feb 5 pass.US
wrxwrx 130 feb 8 letter.EMEA

Required out put :

output.txt

col1     col2  col3 col 4 col 5         col6
wrxwrx 124 jun 3 Sensex.EMEA        EMEA 
wrxwrx 120 jun 4 Emex.US                          US
wrxwrx 130 feb 3 passion.AUS             AUS
wrxwrx 145 feb 9 lession.AUS               AUS
wrxwrx 130 feb 5 pass.US                          US
wrxwrx 130 feb 8 letter.EMEA              EMEA

I tired

awk 'NR=FNR   {TEM[NR]=$(grep "EMEA \| US \| AUS" file.txt ; next }' {print $1,$2,$3,$4,$5,TEM[NR]}' file.txt output.txt

If can any one help me that would be great thanks!!!!

Try:

awk 'NR==1{$(NF+1)="col 6"}NR>1{x=$NF;sub(".*\\.","",x);$(NF+1)=x}1' file.txt > output.txt

or maybe this

awk -F '.'  'FNR==1 { print $0, "      col6"; next} {print $0, "       ", $(NF)}' infile

Dear Bartus,

Could you please explain your code for me ???

awk 'NR==1{$(NF+1)="col 6"}NR>1{x=$NF;sub(".*\\.","",x);$(NF+1)=x}1' file.txt > output.txt
                                                                                                                                                                                             [[IMG]http://linux.unix.com/images/buttons/quote.gif[/IMG]](http://www.unix.com/newreply.php?do=newreply&p=302827669)

---------- Post updated at 02:30 PM ---------- Previous update was at 02:24 PM ----------

Here there is a quick question.

I will have the files like below

Sensex.AM.EMEA
Emex.PM.US
passion.AM.PL.AUS
lession.PM.GT.AUS
pass.AF.US
letter.AT.EMEA  

Please consider the above comments also

'NR==1{$(NF+1)="col 6"}

For first record only, create a new field to hold the field heading.

NR>1{x=$NF;sub(".*\\.","",x);

For all records after the first record, get the characters to the right of the last decimal and assign it to variable x. It does this by using Regular Expression to replace all characters to the left of and including the decimal with nothing.

$(NF+1)=x}1' 

Print entire record including the value for the new field.

It appears that both of the solutions will handle your new file if your requirement is to print the characters to the right of the last decimal place in new field at end of each record.

Thanks a lot for your explanation !!!

It works fine!!!