1836599 Members
1894 Online
110102 Solutions
New Discussion

Re: Hail sed gurus

 
Glenn L. Stewart
Frequent Advisor

Hail sed gurus

I'm trying to write a script that takes the output of /opt/OV/bin/OpC/utils/opcdcode on
1. /var/opt/OV/conf/OpC/le
2. /var/opt/OV/conf/OpC/monitor
3. /var/opt/OV/conf/OpC/msgi
4. /var/opt/OV/conf/OpC/trapi

(not necessarily required to know if you don't have ITO or variant apart from knowing output of this).

Output produces multiple fields ($1) and field value "$2"

Example Output:

FIELD1 "value of some type"
FIELD2 "whatever"
FIELD3 "something else"

(indents are varied but still conform to $1 $2)

This output is particularly easy to work with. Unfotunately as per usual, some output from opcdcode is a little rogue in that for some particular fields, the "value" is found on the following line.

Example Output:

FIELD1 "value of some type"
FIELD2 "whatever"
FIELD3
"something else"

(indents are varied but still last line, the expected $2 becomes a $1)

I know there is a simple sed solution.
I am looking for a sed (or sed/awk combo) that will join any line in which the first non space characted is a non letter (i.e. above the first character is "), with the line preceding.

In effect making turning

FIELD1 "X"
FIELD2 "X"
FIELD3
"X"
FIELD4 "X"
FIELD5
"X"

into

FIELD1 "X"
FIELD2 "X"
FIELD3 "X"
FIELD4 "X"
FIELD5 "X"

Any help much appreciated and rewarded

Glenn
11 REPLIES 11
Steve Steel
Honored Contributor

Re: Hail sed gurus

Hi


If the file is not too long and each line starts with field try


echo $(cat outputfile)|sed -e 's/FIELD/\^JFIELD/g'

^j is formed by cntrlVcntrlM


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Vincent Stedema
Esteemed Contributor

Re: Hail sed gurus

I swear there must be an easier way to do this in awk, but this is all I could come up with:

awk 'BEGIN {RS="";a=1;b=2} {while (a < NF) {print $'a'" "$'b';a+=1;b+=1} }' inputfile

Grtz.

Vincent
Robin Wakefield
Honored Contributor

Re: Hail sed gurus

Hi Glenn,

Create an awk script containing:

BEGIN{FS=""}
$1 !~ "[a-zA-Z]"{printf(" %s",$0);next}
{printf("%s%s"),s,$0;s="\n"}
END{print}

then run with

awk -f scriptname filename

Rgds, Robin.
Carlos Fernandez Riera
Honored Contributor

Re: Hail sed gurus

The question is number of fields in each line?


awk ' NF == 2 { print $1 .... ; next}
NF == 1 { last=$0; getline; print last, $0 }'



### NF -> number of fields
unsupported
Glenn L. Stewart
Frequent Advisor

Re: Hail sed gurus

This webpage doesn't do my example justice. It aligns everything to the left, where the actual example isn't formatted that way.

I've attached the four text files in question.

Namely:
le, monitor, msgi and trapi (not as important)

The monitor file seems okay; all fields are short enough to remain on the same line.
That doesn't mean that in the future this file will always remain this way.

So, running the following command on the other three files, will highlight the entries not conforming to my script - hence requiring a sed/awk fix.

# cat le | awk '{print $1}' | grep -v ^[A-Z] | sort | uniq

Attaching the input, will help you all in providing me a suitable solution.
I will certainly reward 10 points for each and every solution, even if it is solved by a previous user. It's the least I can do for any help provided.
The smaller the solution, the better.

Explaining my logic in using the command above:
The file has varying tabs before $1. Therefore the awk portion eliminates this. The Fields (always starting with A-Z), are then removed by the grep, leaving values that should be joined with the previous line.

For example, in file msgi, the first occurance of "20m":

As per file (indents exist before each line but are taken out by this website).

SUPP_DUPL_IDENT
"20m" RESEND "1d"

Should instead read

SUPP_DUPL_IDENT "20m"
RESEND "1d"

In format
FIELD "Value value value"

Please note, that not all incorrect values start with "

The file trapi is a good example.

CONDITION
$e ".1.3.6.1.4.1.11.2.17.1"
$G 6
$S 58916865

trapi is certainly a harder one to tackle, and might need a varied solution because of format:

FIELD1
SUB_FIELD1 "Value"
SUB_FIELD2 "Value"

I might write something to output this in format.

FIELD1 SUB_FIELD1 "Value"
FIELD1 SUB FILED2 "Value"

Anyway....
Any help mainly on the " values being on the wrong line in le and msgi would be most helpful

Thanks heaps in advance






Robin Wakefield
Honored Contributor

Re: Hail sed gurus

Hi Glenn,

Can you please check the attachment, I can't get to it.

Rgds, Robin
Glenn L. Stewart
Frequent Advisor

Re: Hail sed gurus

Hi,

Having trouble getting to the attachment myself.
I'll re-attach it when I get back to work tomorrow morning (currently late Monday afternoon in Sydney)
Robin Wakefield
Honored Contributor

Re: Hail sed gurus

Hi Glenn,

Off topic - my son's there at the moment, he's just mailed me to say it's raining harder than he's ever seen before!! - hope you're not getting flooded...Robin
Glenn L. Stewart
Frequent Advisor

Re: Hail sed gurus

Robin, you're right. The rain is bucketing down. High on the scale of crappy weather, but nothing compared to some storms I've seen - a la April 1999 hail storm. Well over 1 billion worth of damage of a few small suburbs.
What's your son doing in Sydney?

Anyway...
I've attached the file again.

Thanks
Glenn L. Stewart
Frequent Advisor

Re: Hail sed gurus

Once again the attachement doesn't work...
I can't understand why. Take 3.
Glenn L. Stewart
Frequent Advisor

Re: Hail sed gurus

Okay... the attachments aren't working for me.
I'll put in a call through the response centre. We're always concerned about the response time from Australia of itrc. At least this is something else along the performance line that I can at least inform the itrc folks about.

If you wish, I can email the file to whomever wants to attempt this tricky problem.

Feel free to email mail - glenn@inodes.net

Thanks