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
# 8  
Old 03-14-2012
Can you cat part of the generated userson.txt and pipe it through od -c ?
Code:
sed -n 1,9p userson.txt | od -c


Last edited by Scrutinizer; 03-14-2012 at 09:40 AM..
# 9  
Old 03-14-2012
Ok, removing CR made it work:

Code:
while read AuxLine
do
  user_search=`echo $AuxLine | tr -d "\r"`
  awk -v idusr=${user_search} 'BEGIN { FS = "," } ; $0 ~ idusr { print $4}' users.txt
done < userson.txt

I still don't understand why a CR was bothering nor why a file I create using VI does not have it while the same file created with grep does have.

Looks like there's much more to learn the basics Smilie
# 10  
Old 03-14-2012
That is because the original file came from a windows platform and was not transferred in a correct way to your unix platform. The testfiles you created correctly on your unix platform, so they do not contain the CR's.
# 11  
Old 03-14-2012
Hey Scrutinizer, I didn't read your message when I last posted. Thanks for your help

The output of the cat was:

Code:
0000000   A   U   0   9   2   2  \r  \n   A   U   0   9   0   1  \r  \n
0000020   A   U   0   9   1   3  \r  \n   A   U   0   9   1   0  \r  \n
0000040   A   U   0   9   0   3  \r  \n
0000050

# 12  
Old 03-14-2012
OK, there are the CR's.. Do you need to use a while loop by the way? You could also use only awk, for example, like so:
Code:
awk 'NR==FNR{A[$1];next}$1 in A{$2="off"}1' FS=, OFS=, userson.txt users.txt

once you converted userson.txt to unix format..
This User Gave Thanks to Scrutinizer For This Post:
# 13  
Old 03-14-2012
Oh .. that's what I was looking for all along: I do not need the loop it's just that I'm at level 0 in shell scripting Smilie thanks again.

And BTW, the file was created in linux, but with PHP and formatted for html. Maybe the CR came from there?

In conclusion: when reading lines I should always get rid of CR?
# 14  
Old 03-14-2012
OK, indeed, if the PHP is coded to produce DOS type files for use on Windows platforms, the CR's will be there too.. Normally you would not need to check or get rid of CR's on normal unix type files...
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