Problem passing a search pattern to AWK inside a script loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem passing a search pattern to AWK inside a script loop
# 1  
Old 03-13-2012
Problem passing a search pattern to AWK inside a script loop

Learning, stumbling! My progress in shell scripting is slow. Now I have this doubt:

I have the following file (users.txt):

Code:
AU0909,on
AU0309,off
AU0209,on
AU0109,off

And this file (userson.txt)
Code:
AU0909
AU0209
AU0109
AU0309

I just want to set those users on userson.txt to "off" in the users.txt file. String substitution.
This is an exercise with AWK, I already know other methods to change it but I'm interested in AWK.

Now, I tried this in a script:

Script1.sh
Code:
 
AuxLine="AU0909"
awk -v idusr=${AuxLine} 'BEGIN { FS = "," } ; $0 ~ idusr { print $2}' users.txt

That works for that given string, but I for hours I can't see why this does not work:

Code:
while read AuxLine
do
  awk -v idusr=${AuxLine} 'BEGIN { FS = "," } ; $0 ~ idusr { print $2}' users.txt
done < userson.txt

The variable is passed correctly (read from usersons.txt) as I already tried printing it with no search pattern and it did print. I just can't see how the loop is preventing this simple command to work.

Any clues?

Last edited by quinestor; 03-13-2012 at 04:45 PM..
# 2  
Old 03-13-2012
First something really basic. In the second part you are using a file with a different name? Does it exist?
# 3  
Old 03-13-2012
Which file is different?

I was typing myself the names of the files on the post so if there is any mistake with it I am sure the files do exist (they're being read properly)
# 4  
Old 03-13-2012
I mean users.txt and UserUpdateList . The two examples operate on different files.
# 5  
Old 03-13-2012
Oh yes, users.txt = UserUpdateList.

I changed the name in the post just to make it less confusing. I'll edit now.


But to add somethig: I just discovered this:

Code:
while read AuxLine
do
   echo $AuxLine "is it this?: AU0922"
   awk -v idusr=${Auxa} 'BEGIN { FS = "," } ; $0 ~ idusr { print $4}' users.txt
done < userson.txt

That echo DOES NOT show the value of $AuxLine on the result of the script. the output is:

Code:
 is it this?: AU0922
 is it this?: AU0922
 is it this?: AU0922
 is it this?: AU0922
         ...

However, if I echo the value alone (echo $AuxLine) it does show.

Maybe this is related to my issue?
# 6  
Old 03-13-2012
This is strange. What OS are you using and what shell?
Is userson.txt exactly like in you sample?
If you are on Solaris, use /usr/xpg4/bin/sh and /usr/xpg4/bin/awk instead of /bin/sh and awk

Last edited by Scrutinizer; 03-13-2012 at 05:34 PM..
# 7  
Old 03-14-2012
I am using ubuntu 10.10: #!/bin/bash

Scrutinizer , you are leading me closer to the answer Smilie
The file userson.txt "looked" exactly like in the code I wrote, but it was generated like this:

Code:
grep "0 AU.*" usersin.txt | cut -d' ' -f2 >> userson.txt

The file usersin.txt :

Code:
 Issued: message 1
1 AU0922
1 AU0905
1 AU0912
1 AU0923
0 AU0922
0 AU0900
0 AU0901
0 AU0902

I just tried the previous codes with a manually generated userson.txt and IT DID work..

Any idea what is happening after that grep? My guess is some encoding issue..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. UNIX for Beginners Questions & Answers

Search a string inside a pattern matched block of a file

How to grep for searching a string within a begin and end pattern of a file. Sent from my Redmi 3S using Tapatalk (8 Replies)
Discussion started by: Baishali
8 Replies

3. Linux

Problem with my loop and awk script

Sorry if this is a super simple issue, but am extremely new to this and am trying to teach myself as I go along. But can someone please help me out? I have a data file similar to this for many samples, for all chromosomes Sample Chr bp p roh Sample1 1 49598178 0 1... (14 Replies)
Discussion started by: vuvuzelo
14 Replies

4. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

5. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

7. Shell Programming and Scripting

AWK inside For loop

Hi, awk -F"|" 'BEGIN{sum=0}{sum+=$2}END{printf("%d\n", sum)}' css.txt awk -F"|" 'BEGIN{sum=0}{sum+=$3}END{printf("%d\n", sum)}' css.txt awk -F"|" 'BEGIN{sum=0}{sum+=$4}END{printf("%d\n", sum)}' css.txt awk -F"|" 'BEGIN{sum=0}{sum+=$5}END{printf("%d\n", sum)}' css.txt awk -F"|"... (3 Replies)
Discussion started by: thulasidharan2k
3 Replies

8. Programming

PERL, search and replace inside foreach loop

Hello All, Im a Hardware engineer, I have written this script to automate my job. I got stuck in the following location. CODE: .. .. ... foreach $key(keys %arr_hash) { my ($loc,$ind,$add) = split /,/, $arr_hash{$key}; &create_verilog($key, $loc, $ind ,$add); } sub create_verilog{... (2 Replies)
Discussion started by: riyasnr007
2 Replies

9. UNIX for Dummies Questions & Answers

problem in reading inside a while loop

I am not able to read inside a while though i get the message "inside read" the cursor doesnt prompt from the console cat file | while read ln_new_engine_dirs do echo "inside $ln_new_engine_dirs" if then read nn echo "inside read" fi done Thanks in advance (3 Replies)
Discussion started by: ssuresh1999
3 Replies

10. Shell Programming and Scripting

passing variables to sed inside script

I am trying to pass a regular expression variable from a simple script to sed to remove entries from a text file e.g. a='aaaa bbbb cccc ...|...:' then executing sed from the script sed s'/"'$a"'//g <$FILE > $FILE"_"1 my output file is always the same as the input file !! any... (5 Replies)
Discussion started by: Daniel234
5 Replies
Login or Register to Ask a Question