Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Thank you. It's already helped me greatly, and I enjoy just reading the inputs from the other members..."

Geography

Where in the world do Tek-Tips members come from?

extract 3rd,4th and 5th element

viclap (Programmer)
1 Jun 12 2:51
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.
feherke (Programmer)
1 Jun 12 3:06
Hi

No need to split() it yourself, see -a in man perlrun :

CODE

perl -pae '$_=join(" ",@F[2,3,4])."\n"' TestDoc.txt

Feherke.
http://feherke.github.com/

viclap (Programmer)
1 Jun 12 3:10
million thanks feherke.
feherke (Programmer)
1 Jun 12 3:16
Hi

Grr. While talking about the -l option I forgot to mention -l, which would simplify the solution even more :

CODE

perl -plae '$_=join" ",@F[2,3,4]' TestDoc.txt

Feherke.
http://feherke.github.com/

viclap (Programmer)
1 Jun 12 4:11
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@NetITblushingscripts$ 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.



feherke (Programmer)
1 Jun 12 4:21
Hi

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

perl -plaF~ -e '$_=join"~",@F[0,2,13]' LINE_STATUS.csv

Feherke.
http://feherke.github.com/

viclap (Programmer)
1 Jun 12 17:41
Yup Got it...Thanks Feherke. Nice solution.
viclap (Programmer)
3 Jul 12 6:21
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.






feherke (Programmer)
3 Jul 12 6:32
Hi

Quote (viclap)

Is there anything wrong with the code?
/usr/bin/perl -plaF~ -e '$_=join"\;",@F[0,3,4,5]' $INFLE > $OUTFLE #extraction of 1st,4th,5th,6th fiel

Quote (Feherke)

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

Quote (viclap)

Have tried to escape the ;
No need for that.

Feherke.
http://feherke.github.com/

viclap (Programmer)
3 Jul 12 7:41
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

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close