1820707 Members
2629 Online
109627 Solutions
New Discussion юеВ

awk script help!

 
SOLVED
Go to solution
Ionut Grigorescu_2
Super Advisor

awk script help!

Hi all,

attached a file from which I want to print out/extract only the BTS numbers under BTS column - at the end of the file, in this case 002,003,006. I want to filter out the BTS names - CITY2, METRO2 and all the other lines.It should be possible with awk but I'm still not that good :-)

Any idea?
If it weren't for STRESS I'd have no energy at all
9 REPLIES 9
john korterman
Honored Contributor

Re: awk script help!

Hi,
how about a semi-awk construction:
# grep -e "[0-9]*\ *[0-9]*CITY2\ *[0-9]*\ *METRO2" | awk '{print $1,","$2",",$4}'


regards,
John K.
it would be nice if you always got a second chance
Mark Grant
Honored Contributor

Re: awk script help!

This works for me but you'll need to change the name of the datafile.

#!/usr/bin/perl

@DATA=`cat datafile`;

while($i=shift @DATA){
if($i =~ /BTS NAME/){
shift @DATA;
$line=shift @DATA;
($num1,$num2,$num3,$num4)=split " ",$line;
print "$num1 $num2 $num4\n";
exit 0;
}
}
Never preceed any demonstration with anything more predictive than "watch this"
Ionut Grigorescu_2
Super Advisor

Re: awk script help!

1. The grep/awk combination doesn't help me - The CITY2 and METRO2 are only an example, the name of the BTS can be any combination of alpha-numeric characters but is always longer than 5 characters/digits, whreas BTS number is a 3 digit number between 0..248

2. The perl script doesn't work - at line 8, next 2 tokens split " " - compilation error
If it weren't for STRESS I'd have no energy at all
john korterman
Honored Contributor
Solution

Re: awk script help!

Hi,
perhaps you can modify this awk to your needs:


/BTS NAME/ { getline;getline; print $1,","$2",",$4}

Run it like this:
# awk -f

regards,
John K.


it would be nice if you always got a second chance
F. X. de Montgolfier
Valued Contributor

Re: awk script help!

Hi,

if only the BTS columns have a 3 digits format, here is a solution using sed:

# grep ^[0-9][0-9][0-9][^0-9] test|sed -e 's/[0-9][0-9][0-9]/\
/g'

gives the result:


CITY2
METRO2


Please note that the substitution is on two lines, and that the \ on the first line indicates that the substitution is to continue on next line: it is
sed -e 's/[0-9][0-9][0-9]/\
/g'

and not
sed -e 's/[0-9][0-9][0-9]/\/g'


Hope this helps,

FiX

Mark Grant
Honored Contributor

Re: awk script help!

That's odd,

I just downloaded your example and did a cut and paste of the code from my browser, ran the script and this is what I got.

bash-2.05a$ mv 17329.null datafile
bash-2.05a$ ./script.pl
002 003 006

Perhaps your cut and paste put a newline somwhere in this line

($num1,$num2,$num3,$num4)=split " ",$line;
Never preceed any demonstration with anything more predictive than "watch this"
Graham Cameron_1
Honored Contributor

Re: awk script help!

From what I understand, you want to start capturing lines after "BTS NAME" followed by dashes, and stop at the next blank line, and for each line found, print each 3 digit number.
If that's your requirement, then this will do it.
Save to a file (eg my.awk), and use
awk -f my.awk 17329.null

BEGIN {btsfound=0}
/^BTS NAME/ {btsfound=1}
/^--/ {next}
/^[:space:]*$/ {print ; btsfound=0} # Look for whitespace
(btsfound==1) {
for (i=1; i<=NF; i++) {
if (match($i,"^[0-9][0-9][0-9]$") != 0)
printf ("%s ", $i)
}
}

They will all be printed on one-line, space separated. Adjust the printf statements to suit your taste...

Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Ionut Grigorescu_2
Super Advisor

Re: awk script help!

Thank you John, that was good, I shall assign points to everybody, I need just time to test all the solutions.
If it weren't for STRESS I'd have no energy at all
Ionut Grigorescu_2
Super Advisor

Re: awk script help!

Mark,

I have re-tried your perl script - I have tried with copy and paste I have written it by hand in vi - nothing! I got the same compilation error in line 8. My Perl is 5.005_03 I have to use this version!
If it weren't for STRESS I'd have no energy at all