For Programmers: Free Programming Magazines  


Home > Archive > AWK > April 2005 > usage of Awk inside awk results....









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author usage of Awk inside awk results....
karthik.prabaharan@gmail.com

2005-04-18, 3:55 pm

Hello, can someone help me in solving my "awk" doubt..

I have an input file as shown below:

XYZ Company.FFF.MKS
XYZ Company.FFF.MKS|ABC-Designer
XYZ Company.FFF.MKS|ABC-Designer|User1
XYZ Company.FFF.MKS|DEF-Engineer|User2
XYZ Company.FFF.MKS-FCSF|DEF-Engineer|X-User2

And I need to tweak the above file into:

XYZ Company.FFF.MKS
XYZ Company.FFF.MKS|Designer
XYZ Company.FFF.MKS|Designer|User1
XYZ Company.FFF.MKS|Engineer|User2
XYZ Company.FFF.MKS-FCSF|Engineer|X-User2

I tried to get the output file using WHILE loop and "cut " combination
but my input file has around 30,000 lines and it takes a long time to
process with the below script.
-----------------------------------------------------------------
exec<input.txt

while read line
do
first=`echo $line|awk -F"|" '{print $1}'`
second=`echo $line|awk -F"|" '{print $2}'|awk -F"-" '{print $2}'`
third=`echo $line|awk -F"|" '{print $3}'`

echo "$first|$second|$third" >> output.txt

done
-----------------------------------------------------------------

I am not sure how to use it directly with awk like..

cat input.txt|awk -F"|" '{print $1"|"$2 and awk on $2 with -F"-"
option}

can someone give me some suggestion please.

Thanks in advance.

Robert Katz

2005-04-18, 3:55 pm

karthik.prabaharan@gmail.com wrote:
> Hello, can someone help me in solving my "awk" doubt..
>
> I have an input file as shown below:
>
> XYZ Company.FFF.MKS
> XYZ Company.FFF.MKS|ABC-Designer
> XYZ Company.FFF.MKS|ABC-Designer|User1
> XYZ Company.FFF.MKS|DEF-Engineer|User2
> XYZ Company.FFF.MKS-FCSF|DEF-Engineer|X-User2
>
> And I need to tweak the above file into:
>
> XYZ Company.FFF.MKS
> XYZ Company.FFF.MKS|Designer
> XYZ Company.FFF.MKS|Designer|User1
> XYZ Company.FFF.MKS|Engineer|User2
> XYZ Company.FFF.MKS-FCSF|Engineer|X-User2


awk -v OFS="|" -F "|" 'sub(/.*-/, "", $2)1' inputfile

--
Regards,

---Robert
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com