1834395 Members
1669 Online
110066 Solutions
New Discussion

Re: Script

 
SOLVED
Go to solution
hpuxsa
Frequent Advisor

Script

Hi,
I have a text file which has a BEGIN and END for each entry in it. I am trying to extract a field (enclosed in +++) from each entry and for that corresponding entry i am trying to extract another two lines (enclosed in ---) and pass it to another command. Could someone please tell me how I can do this. (Note: the +++ and --- do not exist in the original file it is only for identifying the field)

Rgds,
Gen
Please find the text file below.

???BEGIN +++ 702076283 +++???
??? AA=123A,VV=PQR7,SS=CAT067B ???
??? --- ALARM 299 O1/SW_DEV "CBC71C0021A07E0"A 020925 1115???
???DIGITAL PATH QUALITY SUPERVISION---???
???BES???
???WIP WIPPART SESL2 QSV SECTION DATE TIME???
???RBLT18 1 2 020925 111500???
???END ???

???BEGIN +++702086280 +++???
??? NW=TSS,NE=TRS7,EQ=TAX067A ???
??? --- ALARM 297 A2/SW_DEV "BSC71C0021A07E0"A 020925 0741???
???RADIO X-CEIVER ADMINISTRATION---???
???MANAGED OBJECT FAULT???
???MO RSITE ALARM SLOGAN???
???RXOTS-6-0-5 TAX067A TS SYNC FAULT???
???END???
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: Script

Hi:

Crudely:

# awk '/BEGIN/,/END/ {print $0}' filein | awk '/---/,/---/ {print $0}'

Regards!

...JRF...
Rodney Hills
Honored Contributor

Re: Script

If "---" is not part of the data, how do you identify the 2 lines you want.

Here is an awk script to search for BEGIN and save the number following it. It then looks for ALARM and prints the saved number and the line.

awk -e '/^BEGIN/{sav=$2};/^ALARM/{print sav,$0}' yourfile

hope this helps...

-- Rod Hills

There be dragons...
hpuxsa
Frequent Advisor

Re: Script

Hi James,

I could manage to get the first field the one enclosed in +++. But I am having trouble with the second one (the text enclosed in ---) because it is more than one line. The real file does not have +++ and --- on it.

Thank you for the help.
harry d brown jr
Honored Contributor

Re: Script

how about this nastiness in vi

:1
/^BEGIN
f (that's an f, then a space ( ) VERY IMPORTANT)
:1
:map x /ctrlVctrlM;li+++ctrlV$a+++ctrlVjj^i---ctrlVj$a---ctrlVkJ


then type in x for every "block"

note: ctrlV is a control v and then an escape

live free or die
harry
Live Free or Die
Robin Wakefield
Honored Contributor

Re: Script

Hi,

As Rod says, it's not clear how to identify the lines you're after. If we assume they're always two lines long, you want them joined together, and they're always the 2nd/3rd lines after the BEGIN line, then:

awk '/BEGIN/{getline;getline;s=$0;getline;print s$0}' file

would be one way of doing it.

Rgds, Robin.
Jean-Luc Oudart
Honored Contributor
Solution

Re: Script

I assumed you have a space between +++ or --- and next field.

awk '
{
if($1=="BEGIN") item1=$3;
if($1=="---") {
item2=substr($0,5);
getline;
item3=$0;
gsub("-","",item3);
}
if($1=="END") print item1,item2,item3;
}' filein

result :
702076283 ALARM 299 O1/SW_DEV "CBC71C0021A07E0"A 020925 1115 DIGITAL PATH QUAL
ITY SUPERVISION
702086280 ALARM 297 A2/SW_DEV "BSC71C0021A07E0"A 020925 0741 RADIO XCEIVER ADM
INISTRATION

Jean-Luc
fiat lux
Tom Maloy
Respected Contributor

Re: Script

Your file formatting really got hosed when it was included in the message. Could you include the data file as an attachment?

Carpe diem!
Sridhar Bhaskarla
Honored Contributor

Re: Script

As I didn't understand the criteria for the lines that you are looking for, the following simple script may help you if you modify it to your needs.


DATA=data
ENTRIES=`cat $DATA |grep "^BEGIN" |awk '{print $2}'`

for ENTRY in $ENTRIES
do
echo "ENTRY: $ENTRY"
sed '1,/'$ENTRY'/d' $DATA |head -3|tail -2
done


-Sri
You may be disappointed if you fail, but you are doomed if you don't try
hpuxsa
Frequent Advisor

Re: Script

Hi,
Thanks to all for the help.

Thank you Jean-Luc your solution worked perfect.

Rgds,
Dinesh.