The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Unix Script to parse a CSV RJ17 Shell Programming and Scripting 16 09-10-2008 11:07 PM
Need help in creating a Unix Script to parse xml file Anil.Wmg Shell Programming and Scripting 10 07-29-2008 07:58 AM
Unix shell script to parse the contents of comma-separated file KrishnaSaran Shell Programming and Scripting 11 06-20-2008 06:43 AM
How to parse through a file and based on condition form another output file sivasu.india UNIX for Advanced & Expert Users 6 02-28-2008 04:59 AM
Parse Unix Mail its-ashish Shell Programming and Scripting 4 01-18-2007 04:48 AM

Reply
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-20-2009
natalie23 natalie23 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 15
how to parse this file in unix

Hi all,
I need to parse a file which is having this format:

Details: 1
Name{
first=james
second=steven
}
Sibling{
first=2
second=3
}
Age{
first=13
second=14
}
Friend{
jessy
}

Details: 2
Name{
first=lily
second=tracy
}
Sibling{
first=2
second=3
}
Age{
first=13
second=14
}
Hobby{
hobby1=swimming
}

The output is
Name_first,Name_second,Sibling_first,Sibling_second,Age_first,Age_second,Friend,Hobby1
james,steven,2,3,13,14,jessy,
lily,tracy,2,3,13,13,,swimming


At first, I hardcode the header and use awk to retrieve the value line by line. e.g:
A=`awk 'NR==3 {
for(i=1;i<=NF;i++){
if ( $i == "first" ) {
print $3 $4
}
}
}' file`


However, the details: 1 and and details: 2 is different.Any idea on how to parse this file? Need it urgently. Thanks for help!
  #2 (permalink)  
Old 07-20-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
awk-syntax, something like:
Code:
BEGIN { FS="=" 
           object=""
           print "Name_first,Name_second,Sibling_first,Sibling_second,Age_first,Age_second,Friend,Hobby1"
           OFS=","   # print use this delimeter, if you have comma between arguments in print command
           }

/^Detail/ { # start new record, print out previous
                print Name_first,Name_second,...
                # init variables
                Name_first=""
                Name_second=""
                ...
             }
/^Age/ { object="age" ; continue }
/^Name/ { object="name" ; continue }
...
/^first/ { if (object == "age" ) { Age_first=$2 ; continue } 
             if (object == "name" ) { Name_first=$2 ; continue } 
             ...
           }
/^second/ { # like first
           }

END  {  # last record out
           print Name_first,Name_second,...
       }

Last edited by kshji; 07-20-2009 at 05:54 AM..
  #3 (permalink)  
Old 07-20-2009
natalie23 natalie23 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 15
hi kshji,

I am a newbie in unix and also awk syntax.
Can you explain in more detail? e.g: I do not understand
END { # last record out
print ....
}

Thanks again for your help
  #4 (permalink)  
Old 07-20-2009
natalie23 natalie23 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 15
Hi kshji,

I have try the code,

but the output is
Details 1:


Nothing is display after Details 1.

thanks.
  #5 (permalink)  
Old 07-20-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
When you have Detail line, previous record end => print variables.
After last record you have not Detail line, so after everything END block you must print last record variables.
  #6 (permalink)  
Old 07-20-2009
mphanikumars mphanikumars is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 6
Hi kshji,

Good Script... I just got an interest to learn awk scripting. Can you please post any URL or PDF related awk, which would help me..
  #7 (permalink)  
Old 07-20-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
I can't see your code ? How did you try it to use ? My code was only "proto", idea of parsing input and make output. Learning awk, not only cut&paste. Giving idea of scripting, in this case using awk. I think that in this forum we are waiting that you have basic knowledge from tool what you are using/asking helps.

Subject is "how to parse this file in unix", better subject is
"how to parse this file using awk", if you like to know/learn solution using awk.
Good subject help me( us ) to browse all subject and which are maybe for just for me. Too generally subjects make this "hoppy" hard - too much noice.

Awk basic:
Part 1
Part II
Part III
Reply

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 12:08 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