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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract data 2

Status
Not open for further replies.

mrn

MIS
Apr 27, 2001
3,993
GB
Hello all,

I have the following data

Code:
--------------------------------------
10.0.193.1
Name:    servername.company.com
--------------------------------------
10.0.193.2
No NS Record
--------------------------------------
10.0.193.3
No NS Record
--------------------------------------

I'm trying to do a couple of things

1. Delete all records which contain No NS Record
2. Join the line containing Name: with the previous IP

To get the following output

Code:
10.0.193.1     server.ourcompany.com
10.0.193.27    server2.ourcompany.com

What I've tried so far

awk '/^-/,/No NS Record/'
&
awk '/^-/{f=1}/No NS Record/{f=0}f'
&
BEGIN {
RS=FS=""
}
$1 ~ /^- +/ && $2 ~ /^Name +/ {print prev}
{prev=$NF}

Ran the above code thru nawk --- No Output...



Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Ok I've tried the following the following

sed -n -e '/Name/{p;}' -e 'h'
&
sed -n -e '/Name/x;p;}' -e -h'

First gives me Hostname
Second gives me IP

So

for file in `ls *.txt` ; do
sed -n -e '/Name/{p;}' -e 'h' $file > 1
sed -n -e '/Name/{x;p;}' -e 'h' $file > 2
paste 2 1 >> 3
tail 3
done

does the job, but it's very nasty looking, any one liners?

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
I'm no awk expert but a similar problem in the lemur book woked by setting the input record seperator to, in your case' '-------------'
So, to start with
Code:
awk 'BEGIN { FS = "\n"; RS = "--------------------------------------" }
! /No NS Record/ {print}' input_file
This will retrieve the records you want. I'll have to leave the next bit to the awk gurus (PHV, p5wizard, feherke?)

Ceci n'est pas une signature
Columb Healy
 
I've done some more work on this and you might want to try
Code:
awk -f awkscr input_file
where awkscr is
Code:
BEGIN { FS = "\n"; RS = "--------------------------------------" }
! /No NS Record/ {
  split ( $3, arr, " " );
  printf "%s %s\n", $2, arr[2]
  }
which gives the output you want but surrounded by lots and lots of blank lines.

Ceci n'est pas une signature
Columb Healy
 
And the working version (can you tell I'vew got a quiet and boring day today!)
Code:
gawk -f awkscr input_file
where awkscr is
Code:
BEGIN { FS = "\n"; RS = "--------------------------------------\n" }
! /No NS Record/ { split ( $2, arr, " " ); printf "%s %s\n", $1, arr[2] }
Tested with gawk on AIX 5.1

Ceci n'est pas une signature
Columb Healy
 
lol, cool

Thanks

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
I've done some more investigation into this and the reason that I had to use gawk is that all other awks see only the first character of RS. This is why I was getting so many blank lines, it was seeing '---------' as nine separate records.

The answer for awk is to turn the test around - instead of rejecting records with 'No NS Record' we could accept records with 'Name:' so awkscr becomes
Code:
BEGIN { FS = "\n"; RS = "-" }
/Name:/ { split ( $3, arr, " " ); printf "%s %s\n", $2, arr[2] }
which works fine with awk.

If 'Name' varies then it looks like the ':' is the flag so the script becomes
Code:
BEGIN { FS = "\n"; RS = "-" }
/:/ { split ( $3, arr, " " ); printf "%s %s\n", $2, arr[2] }
which works on your test data.

Ceci n'est pas une signature
Columb Healy
 
Thanks - Have a Star ;-)

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Well, you asked for a oneliner, so:

Code:
awk '/Name:/{print ip "\t" $2} {ip=$0}' /path/to/file

No fancy RS or gawk necessary.

HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top