The UNIX and Linux Forums  


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
Storing space delimited line in var with loop? eltinator Shell Programming and Scripting 2 08-24-2007 02:09 AM
Parsing line out of a file, please help !! xeniya Shell Programming and Scripting 5 05-31-2007 12:52 PM
Parsing comma delimited text file chengwei Shell Programming and Scripting 5 02-23-2007 05:38 AM
Problem parsing line in file rlwilli Shell Programming and Scripting 2 09-02-2006 12:15 PM
command line argument parsing rmjoe UNIX for Dummies Questions & Answers 1 07-28-2005 04:39 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 07-12-2006
monkeys monkeys is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 2
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 (permalink)  
Old 07-12-2006
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,787

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 (permalink)  
Old 07-12-2006
monkeys monkeys is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 2
Great, thanks!
  #4 (permalink)  
Old 07-12-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,130

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 (permalink)  
Old 07-12-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
Try...
Code:
awk 'BEGIN{FS="=";RS="|"}$1=="C"{print $2}'

  #6 (permalink)  
Old 07-12-2006
sajithn sajithn is offline
Registered User
  
 

Join Date: Jul 2006
Location: Bangalore, India
Posts: 2
A pretty straightforward one:

awk -F'|' '{
for(i=1; i<=NF; i++)
{
if (index($i, "C") == 1)
print $i
}
}' input-text-file
  #7 (permalink)  
Old 07-12-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,130
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|'
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 11:58 AM.


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