The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
get_source[34]: 0403-057 Syntax error at line 66 : `"' is not matched. vishal_ranjan Shell Programming and Scripting 1 05-15-2008 07:48 AM
extracting a line based on line number narendra.pant Shell Programming and Scripting 2 09-20-2007 06:00 AM
How to get a next line of a matched word? Muktesh Shell Programming and Scripting 10 08-06-2007 11:17 PM
getting the line number by extracting a line bishweshwar Shell Programming and Scripting 2 08-04-2007 10:46 AM
sed, grep, awk, regex -- extracting a matched substring from a file/string ropers Shell Programming and Scripting 2 05-23-2006 02:56 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-01-2006
not4google not4google is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 15
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 (permalink)  
Old 11-01-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
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 (permalink)  
Old 11-01-2006
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
given your sample data, what is the desired output?
  #4 (permalink)  
Old 11-01-2006
not4google not4google is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 15
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 (permalink)  
Old 11-01-2006
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
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 (permalink)  
Old 11-02-2006
not4google not4google is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 15
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,
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:57 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0