parsing a delimited line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parsing a delimited line
# 1  
Old 07-12-2006
Question parsing a delimited line

I have a text file of lines like:

A=5|B=7|G=4|C=3|P=4|...

In other words, each line is a pipe-delimited set of pairs of strings of the form "X=Y".

What I want to do is find the token starting with "C", and print it and its value (so I'd want to print "C=3" in the example above).

I'm pretty new to sed, awk, etc. - so any help is appreciated.

Note that you do not know which position the "C" string is in. If we did (assuming it was the 13th token), I'd use:

Code:
awk -F "|" '{print $13}'

But since it can be in different locations in different lines, this won't work.

Thanks in advance - this is my first post, by the way.
# 2  
Old 07-12-2006
Code:
awk -F "|" '{for(i=1;i<=NF;i++) {
                 if(index($i,"C") ==1) { 
                      {print $i}
                 }} 
             }'


Last edited by jim mcnamara; 07-12-2006 at 10:54 AM..
# 3  
Old 07-12-2006
Great, thanks!
# 4  
Old 07-12-2006
Code:
echo 'A=5|B=7|G=4|C=3|P=4|' | nawk -F '[|=]' -v c='C' '{for(i=1;i<=NF;i++) if($i==c) print $i "=" $(i+1)}'

# 5  
Old 07-12-2006
Try...
Code:
awk 'BEGIN{FS="=";RS="|"}$1=="C"{print $2}'

# 6  
Old 07-12-2006
A pretty straightforward one:

awk -F'|' '{
for(i=1; i<=NF; i++)
{
if (index($i, "C") == 1)
print $i
}
}' input-text-file
# 7  
Old 07-12-2006
Quote:
Originally Posted by sajithn
A pretty straightforward one:

awk -F'|' '{
for(i=1; i<=NF; i++)
{
if (index($i, "C") == 1)
print $i
}
}' input-text-file
and what if 'A=5|B=7|G=4|CAB=3|P=4|'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add the line to previous line in | delimited text?

Hi All, I am new to Unix and I have one challenge and below are the details. I have pipe delimited text file in that data has span into multiple lines instead of single line. Sample data. Data should be like below for entire file. 41|216|398555|77|provided complete NP outcome data ... (21 Replies)
Discussion started by: Narasimhasss
21 Replies

2. Shell Programming and Scripting

Replace ^M and the new line that follows it in a delimited file

Hello, I want to Replace/Remove ^M and the new line that follows it in a delimited file. So far I have tried following and nothing seems to work for me . Tr –d ‘\r\n’ < old.dat > new.dat -removes all the linefeed, not just the ones after a ^M. Sed ‘/^M$/{N; s/.\n//;}’ < old.dat >... (7 Replies)
Discussion started by: bluestarmoon
7 Replies

3. Shell Programming and Scripting

How can i comma-delimited last field in line?

Awk gurus, Greatly appreciate for any kind of assistance from the expert community Input line: abc,11.22.33.44,xyz,7-8-9-10 pqr,111.222.333.444,wxy,1-2-3 def,22.33.44.55,stu,7-8 used the gsub function below but it changes all of the "-" delimiter: awk 'gsub("-",",")' Desired... (4 Replies)
Discussion started by: ux4me
4 Replies

4. UNIX for Dummies Questions & Answers

Parsing file, reading each line to variable, evaluating date/time stamp of each line

So, the beginning of my script will cat & grep a file with the output directed to a new file. The data I have in this file needs to be parsed, read and evaluated. Basically, I need to identify the latest date/time stamp and then calculate whether or not it is within 15 minutes of the current... (1 Reply)
Discussion started by: hynesward
1 Replies

5. Shell Programming and Scripting

Convert Rows to Space Delimited Line

Hello, I would like to convert a list of rows to a space separated line. Any ideas? Input file: "Administration Tools" "Design Suite" "Dial-up Networking Support" "Editors" desired output "Administration Tools" "Design Suite" "Dial-up Networking Support" "Editors" Thanks,... (4 Replies)
Discussion started by: jaysunn
4 Replies

6. Shell Programming and Scripting

Parsing delimited file

I'll have a file with ever changing lengths depending on the report. Right now it is stored by : delimiter. My question is how do I parse through this b/c it will be changing lengths each time. Example: 1:2:3:4:5 1:2:3:4:5:6:7:8:9 1:2:3 I want to take each one and place in newline for... (5 Replies)
Discussion started by: mhuffs90171
5 Replies

7. Red Hat

help with parsing through delimited file

I need to parse through the following file and extract the BIG02 field. ISA*00* *00* *01*069737914 *01*044760643 ... (1 Reply)
Discussion started by: bds052189
1 Replies

8. Shell Programming and Scripting

4 GB delimited-textfile on ONE LINE

I have delimited-text files ( > 4GB ) and is just one line. OS: HP-UX 11.23 Awk / cut / sed all have line_max limitations. & unable to read one line in (Buffered-mode). Sample file: xxxx|adsfadf|Afdsa|adsf|afds|Asdfas|ads|Afds|Asdf| .....till forever, I want to put a carriage... (5 Replies)
Discussion started by: magedfawzy
5 Replies

9. Shell Programming and Scripting

Perl question, parsing line by line

Hello, Im very new to PERL and as a project to work on developing my skills at PERL Im trying to parse poker hands. Ive tried many methods however I cant get the last step. $yourfile= 'FILENAME';#poker hands to parse open (FILE, "$yourfile") or die $!; @lines = <FILE>; for (@lines) ... (1 Reply)
Discussion started by: Ek0
1 Replies

10. Shell Programming and Scripting

Parsing comma delimited text file

I need to delete a set of files in certain directories if there're older than a certain number of days. So I have a text file, with each line containing the directory & number of days. The format is like this: dirA,5 dirB,7 How do I write script to iteratively parse this text file & delete... (5 Replies)
Discussion started by: chengwei
5 Replies
Login or Register to Ask a Question