usage of Awk inside awk results....

usage of Awk inside awk results....

Post by karthik.pr » Wed, 20 Apr 2005 02:59:45


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.
 
 
 

usage of Awk inside awk results....

Post by Robert Kat » Wed, 20 Apr 2005 03:12:47


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

--
Regards,

---Robert