Awk: Can anyone tell me why this doesn't work?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk: Can anyone tell me why this doesn't work?
# 1  
Old 11-10-2008
Awk: Can anyone tell me why this doesn't work?

If there exists a field in stdin, print it, otherwise, print hello.....

These print nothing:

Code:
cat /dev/null | awk '{if ( length > 0 )  print $1; else print "hello"}'

cat /dev/null | awk '{if ( $1 ) print $1; else print "hello"}'

But the scripts work if I run them directly in a terminal:

Code:
root@home# awk '{if ( $1 ) print $1; else print "hello"}'

hello

hello
dsfs
dsfs
sdfz
sdfz

Please help I am going insane trying to work this out!!
# 2  
Old 11-10-2008
cat /dev/null gives an empty string without a newline just like echo -n "" so you won't get any output.
awk gets a line in the buffer, only if it ends with a newline.

Regards
# 3  
Old 11-10-2008
You beat me to it. The file MUST have a newline

and you dont need to cat

Code:
awk '{if ( $1 ) print $1;else print "hello"}'  /path/to/file

# 4  
Old 11-10-2008
That makes sense. So awk chokes when it receives an empty string. That is a huge drawback. Thanks for letting me know.

BTW, I was trying to replace the following code with a one-liner:

Code:
temp=$(grep string /tmp/file | awk '{print $1}')
if [[ -z $temp ]]
	then
		printf "\n"
	else
		printf "$temp\n"
fi

Cheers guys Smilie
# 5  
Old 11-10-2008
You should get the same output like this command:

Code:
awk '/string/{print $1}' /tmp/file

# 6  
Old 11-10-2008
This will work with text files, not /dev/null, as it is not a text file.

Code:
awk '{if ( length > 0 )  print $1; else print "hello"}' /path/to/testfile.txt

# 7  
Old 11-10-2008
Hi.
Code:
[[ -z $temp ]] && printf "\n" || printf "$temp\n"

but not as readable in my opinion. I generally don't like one-liners in production code (but they are sometimes interesting from a puzzle viewpoint) ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

-ne 0 doesn't work -le does

Hi, I am using korn shell. until ] do echo "\$# = " $# echo "$1" shift done To the above script, I passed 2 parameters and the program control doesn't enter inside "until" loop. If I change it to until ] then it does work. Why numeric comparison is not working with -ne and works... (3 Replies)
Discussion started by: ab_2010
3 Replies

2. Shell Programming and Scripting

Why my awk doesn't work?

root@SDP_Wildcat_Pass-3-C1:~# cat /proc/driver/rtc rtc_time : 05:29:40 rtc_date : 2014-12-19 alrm_time : 01:51:53 alrm_date : 2014-12-20 alarm_IRQ : no alrm_pending : no update IRQ enabled : no periodic IRQ enabled : no periodic IRQ... (4 Replies)
Discussion started by: yanglei_fage
4 Replies

3. UNIX for Dummies Questions & Answers

Why doesn't this work?

find . -name "05_scripts" -type d -exec mv -f {}/'*.aep\ Logs' {}/.LogFiles \; Returns this failure: mv: rename ./019_0120_WS_WH_gate_insideTEST/05_scripts/*.aep\ Logs to ./019_0120_WS_WH_gate_insideTEST/05_scripts/.LogFiles/*.aep\ Logs: No such file or directory I don't know why it's trying... (4 Replies)
Discussion started by: scribling
4 Replies

4. Shell Programming and Scripting

HELP: If Doesn't Work in AWK

Hi! I have a somehow big file (almost 3000 lines long and thirteen columns). Some lines have no value at all or, at least, are incomplete. The columns' values that have no data are marked with a "-" and the corresponding line (the line that owns that value) should be discarded and not used. ... (5 Replies)
Discussion started by: Marcelo de Brit
5 Replies

5. Shell Programming and Scripting

Awk -- why doesn't my min work?

So, I have a files with entries in this format: servername,username,sub_username,useless_datapoint,mail_size So, a few sample lines: server_a,bob,jane,useless,112351 server_a,bob,jim,useless,421193 server_a,bob,bob,useless,0029385 server_a,karen,will,useless,112351... (3 Replies)
Discussion started by: treesloth
3 Replies

6. Shell Programming and Scripting

Awk split doesn't work for empty delimiter

Does anyone know how will I make awk's split work with empty or null separator/delimiter? echo ABCD | awk '{ split($0,arr,""); print arr; }' I need output like: A B C D I am under HP-UX (5 Replies)
Discussion started by: Orbix
5 Replies

7. Shell Programming and Scripting

awk -v -- Why doesn't my example work?

Hi. I've been playing around a bit. This isn't for any practical purpose-- it's really just a theoretical exercise. I wrote this little thing: foreach num ( 6 5 4 ) awk -v "number=$num" 'BEGIN{for(x=0;x<$number;x++) printf "-"; printf "\n"}' end I would expect the following output: ... (3 Replies)
Discussion started by: treesloth
3 Replies

8. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

9. Shell Programming and Scripting

Why doesn't this work?

cat .servers | while read LINE; do ssh jason@$LINE $1 done exit 1 ./command.ksh "ls -l ~jason" Why does this ONLY iterate on the first server in the list? It's not doing the command on all the servers in the list, what am I missing? Thanks! JP (2 Replies)
Discussion started by: jpeery
2 Replies
Login or Register to Ask a Question