AWK - Extracting matched line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK - Extracting matched line
# 1  
Old 11-01-2006
AWK - Extracting matched line

Hi all,

I have one more query related to AWK. I have the following csv data:

Code:
,qwertyA, field1, field2, field3, field4, field5, field6
,,,,,,,,,,,,,,,,,,,100,200
,,,,,,,,,,,,,,,,,,,300,400
,qwertyB, field1, field2, field3, field4, field5, field6
,,,,,,,,,,,,,,,,,,,100,200
,,,,,,,,,,,,,,,,,,,300,400

Code:
nawk -F"," ' /qwertyA/ 
    {print $0}
'

I need to match the qwertyA row which its doing and then be able to carry on to print the rest of its values which are the lines with 100, 200 and 300, 400. Im confused as the /qwertyA/ will return the line matched but how do you go abouts prints its records stopping at the next line which is qwertyB?

Any suggestions on doing this please?
# 2  
Old 11-01-2006
You can force it with the getline function if the number of lines following qwertyA is constant.

Code:
nawk -F"," ' /qwertyA/ 
    {print $0 ; getline ; print $0; getline ; print $0}
'

# 3  
Old 11-01-2006
given your sample data, what is the desired output?
# 4  
Old 11-01-2006
This is the data to work on:

Code:
,qwertyA, field1, field2, field3, field4, field5, field6
,,,,,,,,,,,,,,,,,,,100,200
,,,,,,,,,,,,,,,,,,,300,400
,qwertyB, field1, field2, field3, field4, field5, field6
,,,,,,,,,,,,,,,,,,,100,200
,,,,,,,,,,,,,,,,,,,300,400

I want too extract:
Code:
,qwertyA, field1, field2, field3, field4, field5, field6
,,,,,,,,,,,,,,,,,,,100,200
,,,,,,,,,,,,,,,,,,,300,400

The length of the values following qwertyA and B is not fixed so the getline would not solve this issue,

Thanks again,
# 5  
Old 11-01-2006
assuming:
  1. you're searching for a string that appears in the SECOND field
  2. any subsequent records to be printed have NO value in the second field

nawk -v pat='qwertyA' -f goog.awk myFile.txt

goog.awk:
Code:
BEGIN {
   FS=OFS=","
}
$2 == pat {found=1; print;next}
found && $2 == "" {print;next}
found {found=0}

If the above assumptions are not valid, please elaborate what the 'real life' patterns you have and how would YOU do it manually.
# 6  
Old 11-02-2006
Hi thanks again for the suggestions,

The data to be working on is:

Code:
,qwertyA, field1, field2, field3, field4, field5, field6
,,,,,,,,,,,,,,,,,,,100,200
,,,,,,,,,,,,,,,,,,,300,400
,qwertyB, field1, field2, field3, field4, field5, field6
,,,,,,,,,,,,,,,,,,,100,200
,,,,,,,,,,,,,,,,,,,300,400
,qwertyC, field1, field2, field3, field4, field5, field6
,,,,,,,,,,,,,,,,,,,1200,2300
,,,,,,,,,,,,,,,,,,,3200,4400

I want to extract the line with qwertyA and the subsequent 2 lines beneath it (wont always be 2 lines of data, these are the lines with the ",,,,,,,,").

Doing it manually I would look for the first instance of the data with qwertyA and then get all the lines beneath that untill I come across another line which doesnt contain the list of "," (the lines that are italicised)...

Hope thats a bit clearer,
# 7  
Old 11-02-2006
does the posted work for you with the mentioned assumptions?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extracting the two lines where the first line is matched

Hi, If I have a file of something like @hg19_gold_AL122127.6-131160 GCTTCATCATGCATGGATAGGCTGGCGCCTTTCCTGAGGCCATATGCCGATGGATATG @hg19_gold_AL122127.6-131159 CTTTAATATTTCCGCCACCATCCTGAGTGAATCCCAGCAAGGACAGTCTTTGGGGATT @hg19_gold_AL122127.6-131158... (4 Replies)
Discussion started by: jyu429
4 Replies

2. Shell Programming and Scripting

sed , awk script for printing matched line before regular expression

hi All , I am having a large file with lots of modules as shown below ############################################### module KKK kksd kskks jsn;lsm jsnlsn; Ring jjsjsj kskmsm jjs endmodule module llll 1kksd11 k232skks j33sn;l55sm (6 Replies)
Discussion started by: kshitij
6 Replies

3. Shell Programming and Scripting

AWK - Extracting substr and using it on the same line

Hey, I'm new to the forums. I've searched around and found some great help on how to work with sub-strings but I'm having problems extracting a substr and then using it on the line it was extracted from :wall: I have a text file full of a varying amount of tables (each with the same number of... (7 Replies)
Discussion started by: pyro214
7 Replies

4. Shell Programming and Scripting

Use AWK to move matched line back one?

Can somebody help me with this? I'm sure it's a no-brainer if you know awk... but I don't. Input: Blah Blah Me love you long time Blah Blah awk magic with 'long time' ==> Output: Blah Blah Me love you long time (0 Replies)
Discussion started by: Ryan.
0 Replies

5. Shell Programming and Scripting

AWK Print Line If Specific Character Is Matched

Hello, I have a file as such: FFFFFFF6C000000 225280 225240 - - rwxs- FFFFFFFF79C00000 3240 3240 - - rwxs- FFFFFFFF7A000000 4096 4096 - - rwxs- FFFFFFFF7A400000 64 64 ... (3 Replies)
Discussion started by: PointyWombat
3 Replies

6. Shell Programming and Scripting

awk get matched line's previous line

hi everyone, a b in c d e f in g output is: a e so awk search for "in", then print out the matched line's previuos line. Please advice. (11 Replies)
Discussion started by: jimmy_y
11 Replies

7. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies

8. Shell Programming and Scripting

extracting matched pattern from a line using sed

I am trying to pull certain pieces of data out of a line of a file that matches a certain pattern: The three pieces that I want to pull out of this line are the only occurrences of that pattern within the line, but the rest of the line is not consistent in each file. Basically the line is... (3 Replies)
Discussion started by: ellhef
3 Replies

9. Shell Programming and Scripting

Extracting specified line from a file using awk

Hi, I am just trying to extract a line at a time from a file using awk and can't yet extract even the first line using the following:- awk '/(^) && (NR==1)/ {print $1}' file1 Is there an obvious silly error here? Thanks (3 Replies)
Discussion started by: sirtrancealot
3 Replies

10. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question