Using sed to extract Nth record?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using sed to extract Nth record?
# 1  
Old 04-17-2008
Using sed to extract Nth record?

I have a comma-separated record and I'd like to use sed to pull the Nth record from it.

It seems like it'd need to be something like this:
Code:
sed -n 's/'"\,$1\,"'/&/p'

Am I close?

Last edited by Yogesh Sawant; 04-18-2008 at 08:16 AM.. Reason: added code tags
# 2  
Old 04-17-2008
Try cut...
Code:
cut -d ',' -f $N

...where $N is a number.
# 3  
Old 04-18-2008
Ideal for awk

or awk (or nawk or gawk)...
Code:
awk '{print $N}'


Last edited by Yogesh Sawant; 04-18-2008 at 08:16 AM.. Reason: added code tags
# 4  
Old 04-19-2008
Just to fill in on the sed question: I'm afraid you're not terribly close. If your sed understands the \{m,n\} repetition operator, you could use that:

Code:
sed 's/^\([^,]*,\)\{'"$N"'\}\([^,]*\),.*/\2/' file

This matches start of line, followed by a parenthesized group of any character which is not comma, any number of times (one field) followed by comma; the whole group, $N times; followed by a group of one field (again, arbitrary number of non-comma characters) followed by a comma and anything; if matched, replace with just the contents of the second group. (This assumes all lines will match this pattern; if not, you'll need some minor tweaks, but that goes for the other solutions, as well.)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

2. UNIX for Dummies Questions & Answers

Extract until nth occurence

Hi, I couldn't figure how to extract until last occurence of a character. I have the string ./dir1/file1/abc.sh The output should be /dir1/file1 So, the command should display the path until last occurence of "/". Thanks. (3 Replies)
Discussion started by: rajivn786
3 Replies

3. Shell Programming and Scripting

Extract the text between the nth occurrence of square brackets

Please can someone help with this? I have a file with lines as follows: word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 When I use the... (7 Replies)
Discussion started by: Subhadeep_Sahu
7 Replies

4. Shell Programming and Scripting

Extract a nth field from a comma delimited file

Hi, In my file (which is "," delimited and text qualifier is "), I have to extract a particualr field. file1: 1,"aa,b",4 expected is the 2nd field: aa,b I tried the basic cut -d "," -f 2 file 1, this gave me aa alone instead aa,b. A small hint ot help on this will be very... (5 Replies)
Discussion started by: machomaddy
5 Replies

5. UNIX for Dummies Questions & Answers

Print first, second, every nth, and last record

does anyone have an awk one-liner to: print the first line, the second line, then every Nth line, and the last line of a file. Thanks, Kenny. (5 Replies)
Discussion started by: kenneth.mcbride
5 Replies

6. Shell Programming and Scripting

Script to extract particular record

Hi, I have a large file with huge number of records which are of following pattern: TYPE1 { originNodeType : "IVR" originHostName : "AAIVR" originTransactionID : "01310559" originTimeStamp : "20110620192440+0530" hostName : "hhhh" voucher : '0'D rProfileID : "ZZZZ" Before { Flags :... (1 Reply)
Discussion started by: madhukar1anand
1 Replies

7. Shell Programming and Scripting

extract nth line of all files and print in output file on separate lines.

Hello UNIX experts, I have 124 text files in a directory. I want to extract the 45678th line of all the files sequentialy by file names. The extracted lines should be printed in the output file on seperate lines. e.g. The input Files are one.txt, two.txt, three.txt, four.txt The cat of four... (1 Reply)
Discussion started by: yogeshkumkar
1 Replies

8. Shell Programming and Scripting

Get Nth record from last line

I have a growing file . I knew only last few lines which is constant for evey job run. I'd need to pull the Nth record from the last line. In this case i should not use search pattern. (2 Replies)
Discussion started by: ford2020
2 Replies

9. Shell Programming and Scripting

extract data between nth and n+1th instance of a line

folks.. i need a simple one liner to extract data from between the (n)th and (n+1)th instance of a line in a 2 colum file. eg....for n=3 i should get back 0 1 4 6 help would be much appreciated. file blah.txt ################## identifer line 0 3 0 3 identifer line 0 2 0... (5 Replies)
Discussion started by: ryanp200
5 Replies

10. Shell Programming and Scripting

how to extract last line in record

Hi all!! After experiencing great helpfulness the last time I posted a problem at this site, I once again turn to the forum for expert help. The problem: I have a file.dat containing x, y, and z coordinates, like: x y z 1 1 4 1 2 3 1 3 9 2 1 7 2 2 2 2 3 8 3 1 ... (7 Replies)
Discussion started by: bjorb
7 Replies
Login or Register to Ask a Question