Awk: looping problem!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk: looping problem!
# 1  
Old 06-09-2005
Awk: looping problem!

I am having a problem with awk when I run it with a loop. It works perfectly when I echo a single line from the commandline. For example:

echo 'MFG009 9153852832' | awk '$2 ~ /^[0-9][0-9][0-9][0-9][0-9]0-9][0-9][0-9][0-9][0-9]$/{print $2}'


The Awk command above will print field 2 if field 2 matches 10 digits, but when I run the loop below in a script, nothing get printed.



Phonelist.txt:
MFG003 9159123578,009
MFG008 948354259398
MFG007 9038931566,006
MFG003 9159123578
MFG008 9483542593
MFG007 9038931566
GWI022 9048744185
MFG002 9194824190
MFG004 9152217465
MFG009 9183920264
MFG009 9153852832





cat phonelist.txt | while read line
do
echo $line | awk '$2 ~ /^[0-9][0-9][0-9][0-9][0-9]0-9][0-9][0-9][0-9][0-9]$/{print $2}'
done

Last edited by cstovall; 06-09-2005 at 10:23 AM.. Reason: typo
# 2  
Old 06-09-2005
use vi or cat -vT to show the file nonprinting characters.
i think here is the reason.
# 3  
Old 06-09-2005
be careful typing...
Code:
awk '$2 ~ /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/{print $2}'

# 4  
Old 06-09-2005
Quote:
Originally Posted by vgersh99
be careful typing...
Code:
awk '$2 ~ /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/{print $2}'


Instead of typing [0-9] 10 times, isnt there something like this ?

Code:
'/^[0-9]\{10\}$/{print $2}'

Vino
# 5  
Old 06-09-2005
There were ^M characters at the end of each line. After removing them the script worked perfectly. Thank you for your help!
# 6  
Old 06-09-2005
Quote:
Originally Posted by vino
Instead of typing [0-9] 10 times, isnt there something like this ?

Code:
'/^[0-9]\{10\}$/{print $2}'

Vino
not for all awk's..... Solaris 8 doesn't support it, old gawk doesn't either.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping problem

I need help. I am trying to get this script to send out only one email not multiple emails of the abend. Currently it will send me one then and ther with the first and second one then another email with the first second and third abend and so on. I only want one email sent. ... (2 Replies)
Discussion started by: bbcarosi
2 Replies

2. Shell Programming and Scripting

Help on looping using awk

I have the data like this: PONUMBER,SUPPLIER,LINEITEM,SPLITLINE,LINEAMOUNT,CURRENCY IR5555,Supplier1,1,1,83.1,USD IR5555,Supplier1,1,3,40.4,USD IR5555,Supplier1,1,6,54.1,USD IR5555,Supplier1,1,8,75.1,USD IR5556,Supplier2,1,1,41.1,USD IR5556,Supplier2,1,3,43.1,USD ... (3 Replies)
Discussion started by: jeffreybsu
3 Replies

3. Shell Programming and Scripting

Problem with Looping

Hi, guys, What I want is exactly shown below (I modified the former image and it looks like clearer.) https://lh6.googleusercontent.com/-EG8SKkrWEvc/Ube9e-jDiHI/AAAAAAAAAOM/hFNT0UqQPWE/s512/Linux_Study_20130611_001.jpg And with some guys' help, I made it. My script is below: #!/bin/bash #... (20 Replies)
Discussion started by: franksunnn
20 Replies

4. Shell Programming and Scripting

perl: problem in looping! how to get rid

Hi i just want to open 2 files and find difference between cond1 and cond2 and if the difference is greater than or equal to some number say 2 print the lines again in 2 different files. file 1 (1.txt) aqw dfr 34 poilo ggg 98 file 2 (2.txt) qww asd 28 poilo ggg 97 open FILE1,"1.txt" or... (2 Replies)
Discussion started by: anurupa777
2 Replies

5. Shell Programming and Scripting

looping problem

I have been trying to come up with a program that can do this: Say I have a file named "sir" with a single field; 10 229 288 35 83 47 3 I want to create a file "gen" with three fields with the data in file "sire" listed in field 1 while field 2 and 3 are just 1 each like this: SPARSE... (1 Reply)
Discussion started by: iconig
1 Replies

6. Shell Programming and Scripting

looping in awk

How do I remove last comma? echo "xx yy zz" | awk 'BEGIN{FS=" "}{for (i=1; i<=NF; i++) printf "%s,", $i}'output: xx,yy,zz, required output: xx,yy,zz or (ideally!): xx, yy & zz many thanks in advance! (4 Replies)
Discussion started by: euval
4 Replies

7. UNIX for Dummies Questions & Answers

Help with AWK looping

I'm trying to parse a configuration text file using awk. The following is a sample from the file I'm searching. I can retrieve the formula and recipe names easily but now I want to take it one step farther. In addition to the formula name, I would like to also get the value of the attribute... (6 Replies)
Discussion started by: new2awk
6 Replies

8. Shell Programming and Scripting

Problem with looping the directories

Hi all, I have a directory which has many sub-directories. Now, I want to check the space of each dir and redirect the output in a file if space exceeds the limit. I have already done it, but the way I did is not very good. I just listed the directories and awked the last column to get the... (5 Replies)
Discussion started by: naw_deepak
5 Replies

9. Shell Programming and Scripting

Problem with looping construct

Hi all I have tried to search for this, but keep getting a MySQL db connect error, so am posing the question here, and taking a risk of incurring the wrath of the mods with my first post... I have the following test script: #!/bin/bash HTTPD=`/bin/ps -axcu | /usr/bin/grep httpd... (6 Replies)
Discussion started by: mikie
6 Replies

10. UNIX for Advanced & Expert Users

Looping in awk

Can somebody give me a cleaner way of writing the following script. I was thinking that I could use a loop in the awk statement. It works fine the way it is but I just want the script to be cleaner. #!/usr/bin/sh for r in 0 1 2 3 4 5 6 do DAY=`gdate --date="${r} days ago" +%m\/%d\/%y`... (3 Replies)
Discussion started by: keelba
3 Replies
Login or Register to Ask a Question