extract 3rd,4th and 5th element
extract 3rd,4th and 5th element
(OP)
Hi All!
I trying to execute this perl script to extract a particular field in a text file, luckily it works. But, right now I would to extract 3 more fields in the text file but couldnt get right syntax on how to do it.
command is: perl -pe '$_ = (split(/[ \n]/)) [0] . "\n"' TestDoc.txt ---> extracts only the first field in the text file.
Sample Data:
-f sss vvvv 6765 /etc/password 2011201 wwwwww
-f sss vvvv 7777 /usr/share 2011201 wwwwww
-f sss vvvv 8888 /home/bin 2011201 wwwwww
ANy help would be gladly appreciated. TIA.
I trying to execute this perl script to extract a particular field in a text file, luckily it works. But, right now I would to extract 3 more fields in the text file but couldnt get right syntax on how to do it.
command is: perl -pe '$_ = (split(/[ \n]/)) [0] . "\n"' TestDoc.txt ---> extracts only the first field in the text file.
Sample Data:
-f sss vvvv 6765 /etc/password 2011201 wwwwww
-f sss vvvv 7777 /usr/share 2011201 wwwwww
-f sss vvvv 8888 /home/bin 2011201 wwwwww
ANy help would be gladly appreciated. TIA.
RE: extract 3rd,4th and 5th element
No need to split() it yourself, see -a in man perlrun :
CODE
Feherke.
http://feherke.github.com/
RE: extract 3rd,4th and 5th element
RE: extract 3rd,4th and 5th element
Grr. While talking about the -l option I forgot to mention -l, which would simplify the solution even more :
CODE
Feherke.
http://feherke.github.com/
RE: extract 3rd,4th and 5th element
It works with the above mentioned data but when I tried to execute it on the actual data, nothing happens, same output as with actual data.
Sample contents of the actual data:
LCNUTAG022_1/4/8~130~23288000~No Defect~FFB54753~2~8~0~60~202~1~(co-0123456)~60~1~~~3612300~8~3~1~3612300~1~16301.6~2011-11-28 23:32:29
LCNUTAG022_1/4/8~310~1166000~No Defect~B5005453~2~8~0~163~124~2~?l???~163~1~~~926700~8~3~2~926700~2~816.2~2011-11-28 23:32:29
LCNUTAG022_1/6/10~345~856000~No Defect~FFB54753~2~10~0~330~145~1~(co-0123456)~330~1~~~379000~10~7~1~379000~1~599.2~2011-11-28 23:35:10
command executed: netit@NetIT
I apologize for the inconvenience.
Thanks.
RE: extract 3rd,4th and 5th element
You were close. But specifying tilde ( ~ ) as join()'s parameter is only one step. You also have to specify it as delimiter used by the -a option. For that you use the -F option :
CODE
Feherke.
http://feherke.github.com/
RE: extract 3rd,4th and 5th element
RE: extract 3rd,4th and 5th element
Just a follow on this, I'm wondering why the above perl code doesn't work when the delimeter of the file is ; (semicolon)? It works well previously when the values in the file is seperated by ~ (tilde). Have tried to escape the ; but still it has no effect in the file. Is there anything wrong with the code? The code works:
/usr/bin/perl -e "print \"Extraction of $INFLE File, Please wait...\n\"";
/usr/bin/perl -e "sleep(2);" #delay
/usr/bin/perl -plaF~ -e '$_=join"\;",@F[0,3,4,5]' $INFLE > $OUTFLE #extraction of 1st,4th,5th,6th fiel
/usr/bin/perl -i -pe "s/\;/,/g" $OUTFLE #replace semicolon with comma values
sed -i 1'i\Option 82,Attenuation,Max Attainable Rate,SNR Value' $OUTFLE # put a header in the file
Any help would be gladly appreciated.
RE: extract 3rd,4th and 5th element
No need for that.
Feherke.
http://feherke.github.com/
RE: extract 3rd,4th and 5th element
Million Thanks for such a good advice. Now, the correct code would be:
/usr/bin/perl -plaF\; -e '$_=join";",@F[0,3,4,5]' $INFLE > $OUTFLE
Regards
Vic