Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awk Help

Status
Not open for further replies.

beaster

Technical User
Aug 20, 2001
225
US
I have a file called bh1msc_stripped2. The contents look like below, but can vary:
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
DIGITAL PATH QUALITY SUPERVISION

The contents may change, but they will always be listed top to bottom. The file may have one line in it, or sometime 100.

I just need to cat the file and take each line and output it to a new file called bh1msc_sms1 for the first line, bh1msc_sms2 and so on.

Vlad and CaKiwi helped me yesterday and that portion worked, but this is something new.

Thanks for the help!
//beaster
 
What about

awk '{print > "bh1msc_sms"NR}' input_file

Greg.
 
That was it, Thank you very much Greg!!!!!!!

I get the exact output I want!

//beaster
 

my comment on previous/yesterday thread regarding the "deriving the name of the output file _on the fly_". In Solaris awk/nawk, you need to "build" the name of the output file _explicitly and then use it in the "print/printf" redirection:

outputFile="bh1msc_sms"NR
print > outputFile

vlad
 
Is that why I am getting....?:
beaster@wepmas1> alarm.sh
awk: too many output files 10
record number 11
awk: too many output files 10
record number 11
awk: too many output files 10
record number 11
awk: too many output files 10
record number 11
 

copy/paste your entire script here, pls!
and the sample data file as well.
Also, I'd recommend using nawk instead of plain awk.

vlad
 
#!/bin/sh
#Written by BeasteR to Notify Individuals of Critical and Major Alarms

today=`date +%m%d%y`

outputFile1="bh1msc_sms"NR
outputFile2="bh2msc_sms"NR
outputFile3="bh1bsc1_sms"NR
outputFile4="bh1bsc2_sms"NR
outputFile5="bh2bsc1_sms"NR
outputFile6="bh2bsc2_sms"NR
outputFile7="bhmscp1_sms"NR


#Remove previous files
#rm *stripped*

sed '/^[\t]*$/d' bh1msc_alarms > bh1msc_stripped1
sed '/^[\t]*$/d' bh1bsc1_alarms > bh1bsc1_stripped1
sed '/^[\t]*$/d' bh1bsc2_alarms > bh1bsc2_stripped1
sed '/^[\t]*$/d' bh2msc_alarms > bh2msc_stripped1
sed '/^[\t]*$/d' bh2bsc1_alarms > bh2bsc1_stripped1
sed '/^[\t]*$/d' bh2bsc2_alarms > bh2bsc2_stripped1
sed '/^[\t]*$/d' bhmscp1_alarms > bhmscp1_stripped1


fgrep -f msc_sms_grep bh1msc_stripped1 > bh1msc_stripped2
fgrep -f msc_sms_grep bh2msc_stripped1 > bh2msc_stripped2
/usr/xpg4/bin/fgrep -f bsc_sms_grep bh1bsc1_stripped1 > bh1bsc1_stripped2
/usr/xpg4/bin/fgrep -f bsc_sms_grep bh1bsc2_stripped1 > bh1bsc2_stripped2
/usr/xpg4/bin/fgrep -f bsc_sms_grep bh2bsc1_stripped1 > bh2bsc1_stripped2
/usr/xpg4/bin/fgrep -f bsc_sms_grep bh2bsc2_stripped1 > bh2bsc2_stripped2
/usr/xpg4/bin/fgrep -f bsc_sms_grep bhmscp1_stripped1 > bhmscp1_stripped2

#rm *stripped1*

awk '{print > outputFile1}' bh1msc_stripped2
awk '{print > outputFile2}' bh2msc_stripped2
awk '{print > outputFile3}' bh1bsc1_stripped2
awk '{print > outputFile4}' bh1bsc2_stripped2
awk '{print > outputFile5}' bh2bsc1_stripped2
awk '{print > outputFile6}' bh2bsc2_stripped2
awk '{print > outputFile7}' bhmscp1_stripped2

rm *stripped*


Data File example:
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
DIGITAL PATH QUALITY SUPERVISION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
DIGITAL PATH QUALITY SUPERVISION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
RADIO X-CEIVER ADMINISTRATION
DIGITAL PATH QUALITY SUPERVISION
 
beaster,

I'll try to do my best here, but.... you _really_ need to read up on shell programming and on awk/nawk syntax. It would be somewhat difficult to explain everything here starting with the _basics_. All my comment will be prefixed with the comment "#vlad "

I'd spend some time thinking what of the objective of what needs to be done _FIRST_. The I'd go though some sample shell/awk examples/books of how similar tasks can be achived.

sorry - but it's kinda hard to explain everything in "two words or less"

vlad

#!/bin/sh
#Written by BeasteR to Notify Individuals of Critical and Major Alarms

today=`date +%m%d%y`

# vlad
# this is NOT the place to do these assignments. My previous # comment in this thread was in regards to the _awk_ script. # You derive your file from _within_ awk and _NOT_ in the
# shell script wrapper.

#outputFile1="bh1msc_sms"NR
#outputFile2="bh2msc_sms"NR
#outputFile3="bh1bsc1_sms"NR
#outputFile4="bh1bsc2_sms"NR
#outputFile5="bh2bsc1_sms"NR
#outputFile6="bh2bsc2_sms"NR
#outputFile7="bhmscp1_sms"NR


#Remove previous files
#rm *stripped*

# vlad
# a better approach [not hardwire and create repetetive code]
# is to create an array of files [alarmsArra] and iterate
# through the array. The output files can be derived from the
# "source" file names [by concatenating a string "stripped1"]
sed '/^[\t]*$/d' bh1msc_alarms > bh1msc_stripped1
sed '/^[\t]*$/d' bh1bsc1_alarms > bh1bsc1_stripped1
sed '/^[\t]*$/d' bh1bsc2_alarms > bh1bsc2_stripped1
sed '/^[\t]*$/d' bh2msc_alarms > bh2msc_stripped1
sed '/^[\t]*$/d' bh2bsc1_alarms > bh2bsc1_stripped1
sed '/^[\t]*$/d' bh2bsc2_alarms > bh2bsc2_stripped1
sed '/^[\t]*$/d' bhmscp1_alarms > bhmscp1_stripped1

# vlad
# what is this?
# the comment above can be applied here as well. Why can't
# you do the "sed"-ing [as above] and the "grep"-ing in _ONE_
# contigues stream?
fgrep -f msc_sms_grep bh1msc_stripped1 > bh1msc_stripped2
fgrep -f msc_sms_grep bh2msc_stripped1 > bh2msc_stripped2
/usr/xpg4/bin/fgrep -f bsc_sms_grep bh1bsc1_stripped1 > bh1bsc1_stripped2
/usr/xpg4/bin/fgrep -f bsc_sms_grep bh1bsc2_stripped1 > bh1bsc2_stripped2
/usr/xpg4/bin/fgrep -f bsc_sms_grep bh2bsc1_stripped1 > bh2bsc1_stripped2
/usr/xpg4/bin/fgrep -f bsc_sms_grep bh2bsc2_stripped1 > bh2bsc2_stripped2
/usr/xpg4/bin/fgrep -f bsc_sms_grep bhmscp1_stripped1 > bhmscp1_stripped2

#rm *stripped1*

# vlad
# I've modified the _first_ awk - you change others - I'm
# simply lazy.
awk 'BEGIN {root=substr(FILENAME,1,index(FILENAME,"_")-1) "sms";} {outputFile=root"NR;print > outputFile1}' bh1msc_stripped2
awk '{print > outputFile2}' bh2msc_stripped2
awk '{print > outputFile3}' bh1bsc1_stripped2
awk '{print > outputFile4}' bh1bsc2_stripped2
awk '{print > outputFile5}' bh2bsc1_stripped2
awk '{print > outputFile6}' bh2bsc2_stripped2
awk '{print > outputFile7}' bhmscp1_stripped2

rm *stripped*


 
Thanks guys it is wroking fine now!

//beaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top