Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract 3rd,4th and 5th element

Status
Not open for further replies.

viclap

Programmer
Oct 12, 2000
39
PH
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 -f sss vvvv 7777 /usr/share 2011201 -f sss vvvv 8888 /home/bin 2011201
ANy help would be gladly appreciated. TIA.
 
Hi

No need to [tt]split()[/tt] it yourself, see [tt]-a[/tt] in man perlrun :
Code:
perl -pae '[navy]$_[/navy][teal]=[/teal][b]join[/b][teal]([/teal][green][i]" "[/i][/green][teal],[/teal][navy]@F[/navy][teal][[/teal][purple]2[/purple][teal],[/teal][purple]3[/purple][teal],[/teal][purple]4[/purple][teal]]).[/teal][green][i]"\n"[/i][/green]' TestDoc.txt

Feherke.
[link feherke.github.com/][/url]
 
Hi

Grr. While talking about the [tt]-l[/tt] option I forgot to mention [tt]-l[/tt], which would simplify the solution even more :
Code:
perl -plae '[navy]$_[/navy][teal]=[/teal][b]join[/b][green][i]" "[/i][/green][teal],[/teal][navy]@F[/navy][teal][[/teal][purple]2[/purple][teal],[/teal][purple]3[/purple][teal],[/teal][purple]4[/purple][teal]][/teal]' TestDoc.txt

Feherke.
[link feherke.github.com/][/url]
 
Hi Sir,

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:~/scripts$ perl -plae '$_=join"~",@F[0,2,13]' LINE_STATUS.csv ///// data to be extracted is field 0,1 and 13. Values are seperated by tilde and last field is date/time.

I apologize for the inconvenience.
Thanks.



 
Hi

You were close. But specifying tilde ( ~ ) as [tt]join()[/tt]'s parameter is only one step. You also have to specify it as delimiter used by the [tt]-a[/tt] option. For that you use the [tt]-F[/tt] option :
Code:
perl -plaF~ -e '[navy]$_[/navy][teal]=[/teal][b]join[/b][green][i]"~"[/i][/green][teal],[/teal][navy]@F[/navy][teal][[/teal][purple]0[/purple][teal],[/teal][purple]2[/purple][teal],[/teal][purple]13[/purple][teal]][/teal]' LINE_STATUS.csv


Feherke.
[link feherke.github.com/][/url]
 
Yup Got it...Thanks Feherke. Nice solution.
 
Hi All,

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.






 
Hi

viclap said:
Is there anything wrong with the code?
/usr/bin/perl -plaF[COLOR=red yellow]~[/color] -e '$_=join"\;",@F[0,3,4,5]' $INFLE > $OUTFLE #extraction of 1st,4th,5th,6th fiel
Feherke said:
You were close. But specifying tilde ( ~ ) as [tt]join()[/tt]'s parameter is only one step. You also have to specify it as delimiter used by the [tt]-a[/tt] option. For that you use the [tt]-F[/tt] option

viclap said:
Have tried to escape the ;
No need for that.

Feherke.
[link feherke.github.com/][/url]
 
Sir feherke,

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top