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

Matching a line number exactly 1

Status
Not open for further replies.

polar1

MIS
May 14, 2001
31
0
0
US
I have a file with line numbers from 1 to 33 with a FS :. I'm using AWK to search through the file line by line. My problem is that when AWK gets to line 10 and above it prints line 1 & 10, 2 & 12 ,etc. Is there a way to match exactly? This is what I have for my script.

file1=/tmp/nsr_groups
file2=/tmp/groups
line_count=`cat $file1|wc -l`
count=1
until (( count > $line_count ))
do
group_name=`awk -F':' '/'"^$count:"'/ {printf $3}' $file1`
cat $file1 |awk '/'"$group_name"'/','name:/' |sort -ur
let count="count + 1"
done
 
Hi polar1,

The problem is with the shell and not awk!

Awk has a built-in number of record variable
called NR that keeps sequential count of the
records being processed.

So, I think you need to pipe out of the sort
command; into awk; and finally through sort
again.

However, we need a bit more info to understand
what the output needs to look like, such as:

1. What is name: used for in the second usage of awk?
Is name: in the record containing $group_name?

2. Does file2 play a part in the output of the script?

Without knowing any more than I do, you can try:

sort -n $file1 | sort -t : -ur -k 3,3

3. What platform are you running on?

Please post example input/output if you can.


flogrr
flogr@yahoo.com

 
Maybe one of the pros can help you a little more, but
if all you want to do is sort content by single or double digit line number and make the two exclusive, then here is one way.

awk ' {
FS = (whatever)
char = substr($1, 1, 1)
if ($1 == char) {
print $2
} else {
print $2
}
}' targetfile

This assumes that the 1st column is the line number.


props to flogrr and the masters.


 
File1 and File2 are made up of this:

File1 File2

Group: Servers group 1 1: Servers group 1
Name: server 1 2: Servers group 2
3: Servers group 3
Group: Servers group 2 ...etc
Name: server 4

Group: Servers group 1
Name: server 5
... Random

File1 consists of repeated "groups" with various "names".
File2 consists of just server groups and numbering lines.

My first AWK is getting the first line number from File2 then looking for the "Servers group x". My second AWK is searching File1 for "Servers group x" and the names associated with it. So for an example "Servers group 1" would look like this:

Group: Servers group 1
name: server 1
name: server 5
All works fine until I go past line number 9.

Thank You for any help.
 
Hi polar1-

I finally got this to do what you want!

I could not get the shell to pass the string
representing " Servers group [num]", so I
manipulated the process as you see here.

#!/bin/sh

awk '{print $4}' file2 | sort -un > nums

for i in `cat nums`
do

nawk 'BEGIN {FS=":"}

{

while (getline <&quot;file1&quot; > 0 ) {
if (( $2 ~ line ) && ( !flag )) {
print
getline
print
flag = 1
getline
}

if ( $2 !~ line ) {
getline

if (( $2 ~ line ) && (flag)) {
getline
print
getline
}
}
}

if (getline <&quot;file1&quot; <= 0 ){
print &quot;&quot;
close (&quot;file1&quot;)
}

} END {

if ( !flag) print &quot;No data for:&quot;line

}' line=&quot; Servers group $i&quot; file1

done


- OR THIS WAY -


Put nawk script in an executable file called &quot;polar&quot; and call:


#!/bin/sh

awk '{print $4}' file2 | sort -un > nums

for i in `cat nums`
do
nawk -f polar line=&quot; Servers group $i&quot; file1
done


Hope this helps!



flogrr
flogr@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top