Parsing through Awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing through Awk
# 1  
Old 03-03-2011
Parsing through Awk

Hi All,

I have an input file something like this:

Code:
Line1
Line2
....
LineN

Identifier
( Field1a, Field1b;
  Field2a, Field1b;
  Field3a, Field1b;
  .....
)
LineN+1
LineN+2
etc..

I basically need Field1a, Field2a, Field3a.... from the above file

I am trying the below command
Code:
cat FileName | awk '/^Identifier/,/\(/ { flg=1;}' '/\)/ { flg=0;}' { If flg=1 print $0 };

the above command fetches
Code:
Identifier
( Field1a, Field1b;
  Field2a, Field1b;
  Field3a, Field1b;
  .....

I need to parse again using grep, cut commands:

Is there any better approach for this one?

Thanks
# 2  
Old 03-03-2011
Code:
awk '/Identifier/{p=1;next}/\)/{p=0}p==1{print $2}' file

or:

Code:
sed -nr '/Identifier/,/\)/{s/.*(Field..), (Field..;)*/\1/p}' file


Last edited by yinyuemi; 03-03-2011 at 11:16 PM..
# 3  
Old 03-03-2011
try this
Code:
grep -o "Field[0-9]\+a" ufile


Last edited by Franklin52; 03-04-2011 at 03:02 AM.. Reason: Please use code tags, thank you
# 4  
Old 03-03-2011
--Assumption here is fields in this file aren't actually named Field1a, Field2a, etc. but have real names.

Code:
awk -F'[;,]' '/^\)/{p=0}p==1{sub(/^[( ]*/,"",$1); print $1}/^Identifier/{p=1}' infile

Code:
sed -nr '/^Identifier/,/^\)/{s/^[ (]*([^,;]*)[,;]( *[^,;]*[;,])*$/\1/p}' infile


Last edited by Chubler_XL; 03-03-2011 at 11:58 PM..
# 5  
Old 03-05-2011
Code:
$ ruby -0777 -ne 'puts $_.scan(/(?:Identifier.*\()(.[^)]*)\)/m)[0].join("\n").scan(/(.*),/)' file
 Field1a
  Field2a
  Field3a

# 6  
Old 03-06-2011
Code:
awk -F'[ \t,]*' '/\)/{p=0}p{print $2}/^Identifier/{p=1}' file

Code:
Field1a
Field2a
Field3a
.....

# 7  
Old 03-06-2011
Here is a little sed script thats a lot more robust; it can even deal with input like this:


Code:
Identifier ( Field1a,Field1b, Field1c; Field2a, Field2b;
             Field3a; ..... )


Code:
sed -nr '/Identifier /!b
:j
H;n;/\)/!bj;H
:a
x;s/.*\(//;s/\).*$//;s/ *[\n]//g
:p
s/ *([^,;]*),*[^;]*; */\1\
/;tp;p;q' infile


Last edited by Chubler_XL; 03-07-2011 at 07:59 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script and parsing

Hello I am trying to find how to do the following with awk and other tools. Find a line in the file where the element 1 is equal to 12345, from the first occurrence, in that line find the element 11 what is equal to and print all lines in file where their element 11 is equal to the value of... (2 Replies)
Discussion started by: koutroul
2 Replies

2. Shell Programming and Scripting

Another parsing question (awk)

Input File Name of the session: filesrv_quo snap Logical Units UID: 60:06:01:60:01:7B:25:00:C8:86:B0:CA:5B:A2:E0:11 Name of the session: verspn2_at_176_0218 snap Logical Units UID: Name of the session: DRT-ny-iadsql1-c_ny-iadsql2-c snap Logical Units UID: ... (4 Replies)
Discussion started by: greycells
4 Replies

3. Shell Programming and Scripting

Parsing help ( awk)

Input File ( this is a sample record ) Storage Group Name: DRT_ny-iadsql1-c_ny-iadsql2-c Storage Group UID: 00:21:E9:C7:2D:E0:E1:11:82:CC:00:60:16:10:04:0A HBA/SP Pairs: HBA UID SP Name SPPort ------- ... (2 Replies)
Discussion started by: greycells
2 Replies

4. Shell Programming and Scripting

XML Parsing using awk

Hi All, I have a problem to resolve. For following XML file, I need to parse the values based on Tag Name. I would prefer to use this by awk. I have used sed command to replace the tags (s/<SeqNo>//). In this case there can be new tags introduced. So need to parse it based on Tag Name. Any... (9 Replies)
Discussion started by: Tons
9 Replies

5. Shell Programming and Scripting

parsing xml using awk

hello , i am trying to parse xml using awk however its a little bit tricky as i want <databases> <source> <host>prod</host> <port>1522</port> <tns>GP1</tns> <user>P11</user>... (6 Replies)
Discussion started by: amit1_x
6 Replies

6. UNIX for Dummies Questions & Answers

parsing with awk

I have a file that reads like this: name:john name:bill name:james phone:123 phone:456 phone:789 i would like change the file so it appears like this: name:john phone:123 name:bill phone:456 name:james phone:789 any help is greatly appreciated! (1 Reply)
Discussion started by: chknstrp
1 Replies

7. Shell Programming and Scripting

awk and file parsing

Hi, I have a input file like this TH2TH2867Y NOW33332106Yo You Baby TH2TH3867Y NOW33332106No Way Out TH2TH9867Y NOW33332106Can't find it TJ2TJ2872N WOW33332017sure thing alas TJ2TJ3872N WOW33332017the sky rocks TJ2TJ4872N WOW33332017nothing else matters ... (4 Replies)
Discussion started by: devtakh
4 Replies

8. Shell Programming and Scripting

Awk Parsing

Hi there Not to good in awk . So bear with me I would need a one liner to take a file with this in it name: joe smoe; group: group1; name: linda smith; ... (2 Replies)
Discussion started by: bombcan
2 Replies

9. Shell Programming and Scripting

awk sed parsing

hi , i would like to parse some file with the fallowing data : data data data "unwanted data" data data "unwanted data" data data data data #unwanted data. what i want it to have any coments between "" and after # to be erased using awk or/and sed. has anyone an idea? thanks. (3 Replies)
Discussion started by: Darsh
3 Replies

10. Shell Programming and Scripting

awk parsing problem

I need help with a problem that I have not been able to figure out. I have a file that is about 650K lines. Records are seperated by blank lines, fields seperated by new lines. I was trying to make a report that would add up 2 fields and associate them with a CP. example output would be... (11 Replies)
Discussion started by: timj123
11 Replies
Login or Register to Ask a Question