File Parser


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File Parser
# 1  
Old 06-04-2013
File Parser

Hi

need help parsing a file.

I have tag fields and values in a file with delimiter |.

sample records from the file listed below
Code:
8=value|9=value|35=value|49=value|56=value|34=value|50=value|48=value|10=value
8=value|9=value|35=value|49=value|56=value|34=value|51=value|48=value|10=value
8=value|9=value|35=value|49=value|56=value|34=value|54=value|48=value|10=value

if i have to extract 49 && 59 tag values from the same line..i am using the below awk program
Code:
nawk -F'|' '{
 for(i=1;i<NF;i++)
 {
  if ( $i~/^49=/ && $i~/^59=/ )
   { x=x+$i; }
 }
print x; x="";
 }
 }' filename

it's working fine as expected.

i am trying to prepare a fileparser script where i can pass args like "TAG=&&TAG=||TAG="

Code:
scriptname "59&&49||34="

is there anyway to form if condition dynamicaly in awk based on tags and condition provided in args list.

please give me some ideas i will take it further from there..

Last edited by Scott; 06-04-2013 at 11:38 PM.. Reason: Please use code tags
# 2  
Old 06-04-2013
Code:
myvar="&&TAG"
awk -v var="$myvar" '/$49/ ~ var { do something with field 49 == &&TAG} 'infile

Pass shell variables to nawk or awk with -v[awkvarname]=$shellvar
# 3  
Old 06-05-2013
Quote:
Originally Posted by jim mcnamara
Code:
myvar="&&TAG"
awk -v var="$myvar" '/$49/ ~ var { do something with field 49 == &&TAG} 'infile

Pass shell variables to nawk or awk with -v[awkvarname]=$shellvar
Hi Jim,
Am I correct in assuming that the / characters marked in red above were typos? I don't think they should be there.

Also note that although the standards require conforming implementations of awk to accept -v options and their option-arguments to be accepted in either of the forms:
Code:
-vAwkVarVame=$ShellVarName

and
Code:
-v AwkVarName=$ShellVarName

some buggy implementations (including awk on Mac OS X 10.7.5) won't accept a -v option unless the -v and its option-argument are in separate arguments.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A Stanza File Parser in Pure ksh

As it was ultimately Don Craguns idea that saved the whole project i can as well give something back to the community. This is my stanza file parser, which was written only using ksh without any external programs. The stanza structure There is some inconsistency as to what exactly is meant by... (0 Replies)
Discussion started by: bakunin
0 Replies

2. UNIX for Beginners Questions & Answers

Making a parser

input 1..100km 112..403km 500..623km required output 1..51 112..162 500..550 (i.e 50kms added to the initial distance) (2 Replies)
Discussion started by: ANKIT ROY
2 Replies

3. Shell Programming and Scripting

Parser

Hi All, I am trying to create a parser to find out what cobol programs are being called by which JCL's. I need to search recursively until the main cobol program is found being called by a JCL. I tried to create a script but I am not able to generalize it. Can someone please help. ... (1 Reply)
Discussion started by: nua7
1 Replies

4. Shell Programming and Scripting

EDI File Parser

I've one EDI file which is to be parsed into 7 different file. I managed to extract required segments for a file(HEADER) to a separate file(sample3.dat) and is given below. $ cat sample3.dat REF*EI*273543997~ REF*2U*HELLO~ REF*G2*77685|132~ CLM*1000*0.00***12>B>1*N*A*Y*I~ CN1*05~... (5 Replies)
Discussion started by: ashokv3
5 Replies

5. Shell Programming and Scripting

SQL Parser

Hi, I have been assigned a task to migrate few thousands of sql scripts to a different db format. there could be sub queries and complex joins. there would be functions that needs to be replaced from a given list to another values. this should also parse the sub\inline queries. Can you please... (1 Reply)
Discussion started by: hitmansilentass
1 Replies

6. Programming

Parser

Hi Everyone I have an out put of multiple lines which I would like to parse and retrieve certain info from it. The output consists of multiple sections that starts with the line Client: and ends with STL tag: each section separated by an empty line. So basically somehting like Client: ... (10 Replies)
Discussion started by: bombcan1
10 Replies

7. Shell Programming and Scripting

Parser with sed

Hi, I have this variable: <a href="http://www.rtve.es/mediateca/videos/20100916/video-calamares-rellenos-salsa-pimientos-garbanzos-16-09-10/878586.shtml">V�deo: Calamares rellenos con salsa de pimientos y ...</a> I would like to have: ... (7 Replies)
Discussion started by: mierdatuti
7 Replies

8. Shell Programming and Scripting

Help with an (easy) parser

Hello, i'm workig with a file with structural information about biological macromolecules (proteins etc). In a certain file, the info is structured like this @<TRIPOS>MOLECULE blah 1 blah 2 blah 3 @<TRIPOS>MOLECULE foo 1 foo 2 foo 3 @<TRIPOS>MOLECULE mmm 1 mmm 2 mmm 3 I would... (7 Replies)
Discussion started by: aristegui
7 Replies

9. Shell Programming and Scripting

Text Parser

I am having a text file as follows say server.txt Date Time server ip error code -------------------------------------------------------------------------- 02/21/2008 18:10:14 server1 xxx.xxx.xxx.xxx 6 02/21/2008 08:10:14 server2 ... (8 Replies)
Discussion started by: karthikn7974
8 Replies

10. Shell Programming and Scripting

string parser

I am new to scripting I want to parse a string in a loop eg A:B:C:D E:F:G:H and put them in different variable attr1 = A attr2 = B attr3 = C attr4 = D . . /* do processing with attr1, attr2, attr3 and attr4 */ then go to next line E:F:G:H and again assign... (8 Replies)
Discussion started by: flextronics
8 Replies
Login or Register to Ask a Question