Solved

How to generate list files ? (sed & awk)

Posted on 2002-06-18
18
322 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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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 video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now