awk file redirection issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk file redirection issue
# 1  
Old 12-11-2017
awk file redirection issue

So I'm writing a script which tries to parse human-readable addresses. Part of it is this:

Code:
print $2, implode(A,1,AN," "), CITY, PROV, POST, COUNTRY, CITYCOUNT>2;

CITYCOUNT is a variable between 0 and 3 counting the number of words in a city name. I'm trying to prnt 1 wherever that's greater than two, so I know which rows just might need manual intervention. The issue is that it doesn't compare CITYCOUNT and 2, it prints into a file named 2!

That awk promote an unquoted value into a string without being asked is pretty unusual, isn't it? What's going on?
This User Gave Thanks to Corona688 For This Post:
# 2  
Old 12-11-2017
I can confirm the behaviour - precedence of the redirection operator - both on FreeBSD (awk version 20110810 (FreeBSD)) as well as on Ubuntu 17.10 (mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan).
What works well is enclosing the logical expression in parantheses.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-11-2017
Thanks, that workaround works great. Just one of those weird syntax corners it seems.
# 4  
Old 12-12-2017
Quote:
Originally Posted by Corona688
So I'm writing a script which tries to parse human-readable addresses. Part of it is this:
Code:
print $2, implode(A,1,AN," "), CITY, PROV, POST, COUNTRY, CITYCOUNT>2;

CITYCOUNT is a variable between 0 and 3 counting the number of words in a city name. I'm trying to prnt 1 wherever that's greater than two, so I know which rows just might need manual intervention. The issue is that it doesn't compare CITYCOUNT and 2, it prints into a file named 2!
That awk promote an unquoted value into a string without being asked is pretty unusual, isn't it? What's going on?
Hello Corona688,

Apologies, if I got it wrong here(though I know you need with print trying to suggest it with printf), I believe we could do this by printf like(examples here only):
Code:
 awk -v CITYCOUNT=0 'BEGIN{printf("%d\n",CITYCOUNT>2)}'
 0

OR
Code:
 awk -v CITYCOUNT=3 'BEGIN{printf("%d\n",CITYCOUNT>2)}'
 1

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-12-2017 at 01:48 AM..
# 5  
Old 12-12-2017
Using printf isn't a magic bullet here; as RudiC said in post #2, the way to get rid of the ambiguity between the logical expression CITYCOUNT > 2 and the output redirection > 2 is to use parentheses. Note that even with printf this can still be an issue. The following script:
Code:
awk '
BEGIN {
    for(CITYCOUNT = 1; CITYCOUNT <= 3; CITYCOUNT++) {
	printf "1 CITYCOUNT:%d CITYCOUNT>2:%d\n", CITYCOUNT, CITYCOUNT>2
	printf "2 CITYCOUNT:%d CITYCOUNT>2:%d\n", CITYCOUNT, (CITYCOUNT>2)
	printf("3 CITYCOUNT:%d CITYCOUNT>2:%d\n", CITYCOUNT, CITYCOUNT>2)
    }
}'

produces the following output on standard output (from the last two printfs in the loop:
Code:
2 CITYCOUNT: 1 CITYCOUNT > 2: 0
3 CITYCOUNT: 1 CITYCOUNT > 2: 0
2 CITYCOUNT: 2 CITYCOUNT > 2: 0
3 CITYCOUNT: 2 CITYCOUNT > 2: 0
2 CITYCOUNT: 3 CITYCOUNT > 2: 1
3 CITYCOUNT: 3 CITYCOUNT > 2: 1

(which both have a logical expression inside parentheses), and produces the following output from the first printf in the loop in a file named 2:
Code:
1 CITYCOUNT: 1 CITYCOUNT > 2: 1
1 CITYCOUNT: 2 CITYCOUNT > 2: 2
1 CITYCOUNT: 3 CITYCOUNT > 2: 3

# 6  
Old 12-12-2017
What also would work:
Code:
2 < CITYCOUNT

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to place value at 24 field in a flat file issue

I am trying to add 0393 value at 24th feild using the below command, but its adding at all the lines including header and trailer Input file: ZHV|2657|D0217001|T|TXU|Z|PAN|20131112000552||||OPER| 754|52479| 492|489|SP40|1014570286334|20131111|20131201|14355334|CHAMELON... (1 Reply)
Discussion started by: Aditya_001
1 Replies

2. Shell Programming and Scripting

awk issue while reading from file in while do

Hi Friends, I am trying to scan line by line using awk and pull the values and pass it in variables and then will use the variables but doesn't work. Please see below for details. #more dbtest.sh ---------------------------------- #!/bin/bash . $HOME/.bash_profile while read line do... (6 Replies)
Discussion started by: narunice
6 Replies

3. UNIX for Dummies Questions & Answers

little problem of file redirection (awk)

I almost reach my objective (Youhouuu !!!!) But I really don't understand why it doesn't work until the end... :wall: For clarity's sake I am taking a very simple example. The operations I am doing in the script (gsub and print) really don't have any importance !!! I just matter about... (10 Replies)
Discussion started by: beca123456
10 Replies

4. Shell Programming and Scripting

Issue with output redirection

Hi, I am using AIX server. I have a korn shell script where in I am redirecting a line to a file in a while loop: while read line do outputline=$line; echo $outputline >> "./temp/exec_list_"$ETL_JOB_RUN"_temp" done < ./temp/exec_list_$ETL_JOB_RUN Sometimes, when the CPU... (2 Replies)
Discussion started by: pmonika
2 Replies

5. Shell Programming and Scripting

awk redirection

Hi everyone i am facing a very strange problem . see below is my code #awk <do something> file 1 > file2 Now the problem is whenever i am redirecting the output to file2 it creates the file2 but of 0 size. i don't know what is the reason but it is very important to me to redirect the... (11 Replies)
Discussion started by: aishsimplesweet
11 Replies

6. Shell Programming and Scripting

awk output redirection to file

I have a system stat command running which generates data after 5 sec or so. I pass this data to awk and do some calculation to present the data differently. Once done now I want to pass this data to file as and when generated but doesn't work..unless the first command completes successfully.... (6 Replies)
Discussion started by: learnscript
6 Replies

7. Shell Programming and Scripting

Error with redirection in awk

I'm trying to find average of values in 2nd column in some files. Filenames have following pattern eg: tau_2.54_even_v1.xls tau_2.54_odd_v1.xls tau_1.60_v1.xls tau_800_v1.xls #!/bin/bash for file in pmb_mpi tau xhpl mpi_tile_io fftw ; do for f in "2.54" "1.60" "800" ;do if... (2 Replies)
Discussion started by: vishwamitra
2 Replies

8. Shell Programming and Scripting

awk print redirection to variable file name

Hello, i need to redirect the output of print to a variable file name: #This is normal awk '{ print $17 > "output.txt" }' input #I need something like this awk '{ print $17 > "output_${25}.txt" }' input how to format the output file name to contain a variable? (6 Replies)
Discussion started by: nazeeb
6 Replies

9. Shell Programming and Scripting

awk two file redirection

Hi, i use awk -F to print three variable delimited by comma $1 $2 $3 if $2=="" i want to extract this information missing from another file using awk -v + some process. but the problem i can't use the two awk together cause of redirection there's a solution. note: i can't use another... (1 Reply)
Discussion started by: kamel.seg
1 Replies

10. Shell Programming and Scripting

Tail -f, awk and redirection

I want to run tail -f to continuously monitor a log file, outputing a specific field to a second log file. I can get the first portion to work with the following command: tail -f log | awk '{if ($1 == "Rough") print $5}' also: awk '{if ($1 == "Rough") print $5}' <(tail -f log) The... (2 Replies)
Discussion started by: mfajer
2 Replies
Login or Register to Ask a Question