Solved

How to generate list files ? (sed & awk)

Posted on 2002-06-18
18
342 Views
Last Modified: 2013-12-26
Hi everybody !

I use sed & awk utilities in my work. Prefered shell : tcsh. OS: SunOs 5.8

How to split a big file with many different list inside to separate files ?

Example are bellow:

Input file:
BEGIN CLOCK_DOMAIN Clk1 ;
    GT  MUX21P "/p/do/db5" ;
    GT  MUX21P "/p/do/db5" ;
END CLOCK_DOMAIN

BEGIN CLOCK_DOMAIN Clk2 ;
    GT  MUX22H "/p/m3/m3" ;
    GT  MUX22H "/p/m3/m3" ;
END CLOCK_DOMAIN


Output files:
 File No1 (Clk1.list):
    GT  MUX21P "/p/do/db5" ;
    GT  MUX21P "/p/do/db5" ;

File No2(Clk2.list):
    GT  MUX22H "/p/m3/m3" ;
    GT  MUX22H "/p/m3/m3" ;

 
Thank you in advance
Kug.
0
Comment
Question by:kug
  • 8
  • 8
  • 2
18 Comments
 
LVL 20

Expert Comment

by:tfewster
ID: 7089916
awk '/BEGIN CLOCK_DOMAIN/ { OUTPUTFILE = $3".list"
  getline
  print $0 >  OUTPUTFILE
  getline
  print $0 > OUTPUTFILE
}' /tmp/inputfile

If the "record" format (BEGIN..END) varies, e.g. number of lines in the "record", you need additional code it there
0
 
LVL 20

Expert Comment

by:tfewster
ID: 7089976
e.g.
awk '/BEGIN CLOCK_DOMAIN/ { OUTPUTFILE = $3".list"
  getline
  while  ( $1 == "GT" ) {
        print $0 >  OUTPUTFILE
        getline
  }
}' /tmp/inputfile
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 200 total points
ID: 7090303
awk '($1=="BEGIN"){f=$3;next}($1=="GT"){print $0 > f;}' inputfile
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:kug
ID: 7098353
Hi guys,

thank you for the help.
Unfortunately, something going wrong.

See my script bellow:

#! /usr/lsi/dist/bin/gawk -f

{
 if ( $0 ~/BEGIN CLOCK_DOMAIN/ ){
     OUTPUTFILE = $3".list" ;

 }
  else
      print $0 > OUTPUTFILE
}


When I tried to use following command under tcsh I got error:

gawk -f list_gen.awk input/tmp

gawk: list_gen.awk:10: (FILENAME=input/Elan4.vpedmn FNR=1) fatal: expression for `>' redirection has null string value


Any suggestion ?


regards
Kug

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7098627
your
     if ( $0 ~/BEGIN CLOCK_DOMAIN/ ){
did not match, somehow ...
0
 

Author Comment

by:kug
ID: 7098667
Ahoffman, I think you on wrong way.
This line is right one, you can mark else commands in coment, run my script and see a result.

A tool complain on redirection sign.
When you will use a string instead a variable in this line - script will wortk fine, but only one file will be created.

My target is to create an individual file for each "CLOCK_DOMAIN" list.


regards
Kug.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7098694
strange.
gawk works exactly this way. Please check again that OUTPUTFILE variable is set correctly.
Which gawk version do you have?
Works for me on Solaris <=5.6
0
 

Author Comment

by:kug
ID: 7098710
My gawk version is 3.0.3, OS: SunOs 5.8

OUTPUTFILE is not a standard gawk variable. The is a variable of the script.

Can you please run my script on your side and see a result.

best regards
Kug.

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7098724
it works for me.
PLease verify that OUTPUTFILE is set, somply with

   print OUTPUTFILE;
0
 

Author Comment

by:kug
ID: 7098818
I did, the is not a problem.
OUTPUTFILE variable have right value, updated every time when new clock domain find.

A problem is in redirection of print command to variable name.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7102107
strange, very strange ...
Can you please try if tit works with awk or nawk?
If not please post
   ls -s /bin/awk /bin/nawk
0
 

Author Comment

by:kug
ID: 7102933
I tried to use different kind of awk: gawk, nawk, awk.
From my point of view a problem when you try to redirect to variable, such OUTPUTFILE.
When insteade a variable you will try to redirect to constant filename (string) it works.

....
 print $0 > OUTPUTFILE
doesn't work


....
 print $0 > "Clk1.list"
it works.


When you run may example on your ,machine how many files created ?  
My target is to have a separate list per clock domain list.


best regards
Michael.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7103008
> .. how many files created ?
as many as BEGIN ... lines match from input file
0
 

Author Comment

by:kug
ID: 7103129
Yes, for my short example above it should be 2 files.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7104186
to get to a solution I suggest to install another gawk, best you compile your own. Then test with the awk program.
0
 

Author Comment

by:kug
ID: 7110938
What version of gawk, nawk you use ?
My current version of gawk is 3.0.3 are it not good ?

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7111798
gawk 3.0.4, or /bin/awk bundled with Solaris
0
 

Author Comment

by:kug
ID: 7112911
Well, I found a problem in our script.
A problem was - undefined variable in start of the script.
My original input file have some "header" lines.
So from the beginning of input file if condition is not meet, and script gone to else lines. In those lines OUTPUTFILE was undefined yet.

New, good script bellow(type or version of awk is doesn't metter):

Ahoffmann, thank you a lot for cooperation.


Kug :))

---------------------------------------------
#! /usr/bin/nawk -f


BEGIN{
 OUTPUTFILE = "tmp.txt"
}

{
 if ( $0 ~/BEGIN CLOCK_DOMAIN/ ){
     OUTPUTFILE = $3"_clock_domain.list" ;
      print OUTPUTFILE ;
      print $0 > OUTPUTFILE
     
 }
  else
      print $0 > OUTPUTFILE
}
------------------------------------------
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Rewrite URL With Encoded Characters 6 78
Named range not carried over 10 63
sum67 challenge 35 97
My project did see openJDK that I installed. What could be the problem 7 140
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question