×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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!
  • Students Click Here

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

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

extract 3rd,4th and 5th element

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.

RE: extract 3rd,4th and 5th element

(OP)
million thanks feherke.

RE: extract 3rd,4th and 5th element

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/

RE: extract 3rd,4th and 5th element

(OP)
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.



RE: extract 3rd,4th and 5th element

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/

RE: extract 3rd,4th and 5th element

(OP)
Yup Got it...Thanks Feherke. Nice solution.

RE: extract 3rd,4th and 5th element

(OP)
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.






RE: extract 3rd,4th and 5th element

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/

RE: extract 3rd,4th and 5th element

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

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! Already a Member? Login

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