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
To extract everything between two delimiters dowsed4u8 SUN Solaris 1 01-16-2008 02:49 PM
parse multiple lines? should be a easy answer... DeuceLee UNIX for Dummies Questions & Answers 4 01-04-2008 04:54 PM
Delimiters missing Indalecio Shell Programming and Scripting 2 02-23-2007 04:28 AM
Cut based on Two Delimiters at one go pbsrinivas Shell Programming and Scripting 4 01-18-2007 04:45 AM
awk - treat multiple delimiters as one peter.herlihy Shell Programming and Scripting 6 08-30-2002 01:12 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 05-16-2008
nathasha nathasha is offline
Registered User
  
 

Join Date: May 2008
Posts: 4
parse of lines with different delimiters

Hi,

I am having huge file with the following lines.

2007:10:01:00:00:49:GMT: subject=BMRA.BM.T_ABTH7.FPN, message={SD=2007:10:01:00:00:00:GMT,SP=5,NP=2,TS=2007:10:01:01:00:00:GMT,VP=0.0,TS=2007:10:01:01:30: 00:GMT,VP=0.0}
2007:10:01:00:00:49:GMT: subject=BMRA.BM.T_ABTH7G.FPN, message={SD=2007:10:01:00:00:00:GMT,SP=5,NP=2,TS=2007:10:01:01:00:00:GMT,VP=0.0,TS=2007:10:01:01:30: 00:GMT,VP=0.0}


I need to parse them into the following format.

2007-10-01,T_ABTH7,2007-10-0100:00:00,5,0.0
2007-10-01,T_ABTH7G,2007-10-0100:00:00,5,0.0

Is there a way to parse the entire file without reading a single line of file and formating the output.

Thanks in advance.
  #2 (permalink)  
Old 05-16-2008
penchal_boddu penchal_boddu is offline
Registered User
  
 

Join Date: Apr 2008
Location: Bangalore
Posts: 127
there are so many occurences of 2 and 0.0 in the input lines.

Highlight the portions of input line that u want to report


Thanks
Penchal
  #3 (permalink)  
Old 05-16-2008
nathasha nathasha is offline
Registered User
  
 

Join Date: May 2008
Posts: 4
Hi Penchal,

I want to parse the highlighted values.

2007:10:01:00:00:49:GMT: subject=BMRA.BM.T_ABTH7.FPN, message={SD=2007:10:01:00:00:00:GMT,SP=5,NP=2,TS=2007:10:01:01:00:00:GMT,VP=0.0,TS=2007:10:01:01:30: 00:GMT,VP=0.0}

First output column (2007-10-01): SD=2007:10:01:00:00:00

Second column (T_ABTH7): subject=BMRA.BM.T_ABTH7.FPN

Third Column (2007-10-0100:00:00): TS=2007:10:01:01:00:00

Fourth Column (5): SP=5

Fifth Column(0.0) : VP=0.0

Output for a single line :
2007-10-01,T_ABTH7,2007-10-0100:00:00,5,0.0

Please let me know if this clear.
  #4 (permalink)  
Old 05-16-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,930
One way is to use the pattern matching and substitution capabilities of ksh93 or bash
Code:
#!/usr/bin/ksh93

IFS=','
while read c1 c2 c3 c4 c5 c6 rest
do
   tmp1=${c2##*SD=}
   tmp1=${tmp1%%:00:00:00:GMT}
   tmp2=${c1##*=BMRA.BM.}
   tmp3=${c5##TS=}
   tmp3=${tmp3%%:GMT}
   tmp3A=${tmp3:0:10}
   printf "%s,%s,%s%s,%s,%s\n" ${tmp1//:/-} ${tmp2%%.FPN} ${tmp3A//:/-} ${tmp3:11} ${c3##SP=} ${c6##VP=}
done < file
Output:
Code:
2007-10-01,T_ABTH7,2007-10-0101:00:00,5,0.0
2007-10-01,T_ABTH7G,2007-10-0101:00:00,5,0.0
  #5 (permalink)  
Old 05-16-2008
nathasha nathasha is offline
Registered User
  
 

Join Date: May 2008
Posts: 4
Hi Murphy,

Thanks for the reply.

I am getting the following error message while runnig the script.

tmp3A=${tmp3:0:10}: bad substitution
  #6 (permalink)  
Old 05-16-2008
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
Solution with gawk:

Code:
#!/usr/bin/awk -f
BEGIN {FS=","; OFS=","}
{
          print \
                  gensub(/^.+([0-9][0-9][0-9][0-9]:[0-9][0-9]:[0-9][0-9]).+$/, "\\1", 1, $2),
                  gensub(/^.+subject=BMRA.BM.(.+).FPN/, "\\1", 1, $1),
                  gensub(/^TS=(.+):GMT/, "\\1", 1, $5),
                  gensub(/^SP=(.+)/, "\\1", 1, $3),
                  gensub(/^VP=(.+)/, "\\1", 1, $6)
}
If your version of awk doesn't support gensub(), there is a solution with substr() and match(). Let me know.
  #7 (permalink)  
Old 05-16-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,930
[QUOTE=nathasha;302195904
tmp3A=${tmp3:0:10}: bad substitution[/QUOTE]

Then you either are not using a recent version of ksh93 (later than 1999) or your data structure has changed from what you provided as a sample.
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 03:07 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