How to extract 3rd,4th and 5th element


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract 3rd,4th and 5th element
# 1  
Old 06-01-2012
How to extract 3rd,4th and 5th element

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 like to extract 3 more fields in the text file but couldnt get the right syntax on how to do it.
command is:
Code:
perl -pe '$_ = (split(/[ \n]/)) [0] . "\n"' TestDoc.txt ---> extracts only the first field in the text file.

Sample Data:
Code:
    -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.

Last edited by Franklin52; 06-01-2012 at 04:03 AM.. Reason: Please use code tags
# 2  
Old 06-01-2012
For example:
Code:
 perl -ple '$_ = join(" ",(split(/\s+/)) [1,3..6])."\n" '  TestDoc.txt

In this case you should use:
Code:
 perl -ple '$_ = join(" ",(split(/\s+/)) [3,4,5])."\n" ' TestDoc.txt


Last edited by Klashxx; 06-01-2012 at 05:23 AM..
# 3  
Old 06-01-2012
This can also be done with awk.

Code:
$ awk '{print $3 " " $4 " " $5}' awktest 
vvvv 6765 /etc/password
vvvv 7777 /usr/share
vvvv 8888 /home/bin

# 4  
Old 06-01-2012
Hi all,

million thanks to those who provided the solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print the string between 3rd and 4th backslashs to end of line

im trying to get awk to print the string between 3rd and 4th backslashs to end of line test could be any word this http://example.com/test/ >to this http://example.com/test/ > testalso the other way round insert string at end of line... (13 Replies)
Discussion started by: bob123
13 Replies

2. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

3. UNIX for Dummies Questions & Answers

Extract Element from XML file

<?xml version = '1.0' encoding =... (8 Replies)
Discussion started by: Siva SQL
8 Replies

4. Shell Programming and Scripting

How to extract 4th field if numerics?

I have a file which contains fields comma separated & with each field surrounded by quotes. The 4th field contains either a serial number, the text ABC, the text XYZ or it's blank. I want to only extract records which have a serial number. Here's some sample data: > cat myfile... (4 Replies)
Discussion started by: CHoggarth
4 Replies

5. UNIX for Dummies Questions & Answers

Write 2nd and 3rd fields to a 4th file name?

I have a flatfile A.txt date|products|notes|location 121117|a108|this is a test|florida 121118|b111|just test it|tampa How do i write an awk to create a file name as location.txt and have products:notes awk -F'|' '{ print $2 ":" $3 }' A.txt > $4.txt I am sure it cannot write to... (5 Replies)
Discussion started by: sabercats
5 Replies

6. UNIX for Dummies Questions & Answers

Find the list of filenames that have the string 31 at 4th and 5th position

Hi, Can anyone let me know the command to know the list of filenames that have string 31 in their 4th and 5th positions inside the file: grep -l "31" main*.txt The above grep lists all the files which have 31 at any position but I want filenames having 31 at position 4 and position 5. (8 Replies)
Discussion started by: okkadu
8 Replies

7. UNIX for Advanced & Expert Users

if 4th and 5th character of sting -ge 45 then add 1 to 3rd character

I want to know how to, given a string like W87151WR71C, if the 4th and 5th character (in this case 15) are greater than 45, then to add 1 to the 3rd character (in this case 7) and assign the revised string the variable name MODSTRING. Thanks in advance. This is ultimately to grab info from... (6 Replies)
Discussion started by: glev2005
6 Replies

8. Shell Programming and Scripting

How to extract 3rd line 4th column of a file

Hi, Shell script: I would need help on How to extract 3rd line 4th column of a file with single liner Thanks in advance. (4 Replies)
Discussion started by: krishnamurthig
4 Replies

9. Shell Programming and Scripting

Extract XML Element Values

I have a rather large file with XML-style content. Each line contains one full XML entry. For example: 1:<Message><DNIS>1234</DNIS><UCID>3456</UCID><TransferGroup>XYZXYZ</TransferGroup></Message> 2:<Message><DNIS>9999</DNIS><UCID>2584</UCID><TransferGroup>ABCABC</TransferGroup></Message>... (1 Reply)
Discussion started by: sharpi03
1 Replies

10. Shell Programming and Scripting

printing 3rd or 4th feild from last in awk.

Whats up fellas... hope someone can help me with the following... I am parsing an file that is space delimited, however, in the middle, there is an ugly "Account Name" feild that in itself has multiple varying spaces, and commas which throws off my script. The 1st 3 feilds I am able to obtain... (8 Replies)
Discussion started by: djsal
8 Replies
Login or Register to Ask a Question