system command within awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting system command within awk
# 1  
Old 04-08-2009
system command within awk

I was trying commands within awk. i got stuck here. i take 2 files.

Code:
cat input

first
second
third
fourth
fifth
sixth
seventh
eighth
ninht
tenth

Code:
cat add

***************
Nirbhay
***************

i run the command
Code:
awk '{ if ( NR == 5 ) { print system("cat add") "\n" $0 } else { print } }' input > output

cat output

first
second
third
fourth
***************
Nirbhay
***************
0
fifth
sixth
seventh
eighth
ninht
tenth

My query is that why does 0 appears in the output file, though it is neither in input, nor in add. & what can i do to get rid of it ?
# 2  
Old 04-08-2009
Hi Nirbhay
I think this "0" is the return from the system("cat add") command.

Try this:
awk '{ if ( NR == 5 ) { print system("cat add") 1>/dev/null } else { print }}' input

redirecting return code from system command to null.

Hope this helps.
# 3  
Old 04-08-2009
yes Angad, you're right. But then, using 1>/dev/null inside awk causes a few problems as i can see

Code:
awk '{ if ( NR == 5 ) { print system("cat add") 1>/dev/null "\n" $0 } else { print } }' input > output

this simply ignores everything else in the action. here "\n" $0 are ignored. Rest of the line is not printed.

Code:
awk '{ if ( NR == 5 ) { print (system("cat add") 1>/dev/null) "\n" $0 } else { print } }' input > output

enclosing in braces gives another problem. i get 1 in the output
Code:
first
second
third
fourth
***************
Nirbhay
***************
1
fifth
sixth
seventh
eighth
ninht
tenth

# 4  
Old 04-08-2009
no need to call system to invoke cat. do everything with awk.
Code:
# awk 'NR==5{while (( getline line <"add") > 0 ) {print line}}NR!=5' file
first
second
third
fourth
***************
Nirbhay
***************
sixth
seventh
eighth
ninht
tenth

or
Code:
# awk 'FNR==NR{a[++d]=$0;next}FNR==5{for(i in a)print a[i]}FNR!=5' add file
first
second
third
fourth
***************
Nirbhay
***************
sixth
seventh
eighth
ninht
tenth

# 5  
Old 04-08-2009
Or simply with sed:

Code:
sed '4r add' input

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sprintf and system command in awk

Hello Friends, I'm trying something hard (for me) to create a report script,normally the following script works: Echos are just for cosmetic touch, echo -n "\n-----\t----------\t-------------\t\t--------------\t\t--------\n COUNT\tEVENT_TYPE\tRESPONSE_CODE\t\tINTERNAL_ERROR\t\tFLOWNAME... (7 Replies)
Discussion started by: EAGL€
7 Replies

2. Shell Programming and Scripting

Awk: System command not working in awk

Hi, I have around 10 files in a folder in which I want to change the file format from tab(\t) to pipe(|) with some changes in the fields as well. Below is the code, while tmp file is getting generated but move command is not working, please help Following is the code awk -F"\t" '{print... (2 Replies)
Discussion started by: siramitsharma
2 Replies

3. Shell Programming and Scripting

How can I pass arguments to system command in a awk script?

Hi I need your help, please How can I pass arguments to system command in a awk script?... for example: byte=substr(cadena,pos,2); system("grep -n byte mapeo.txt"); Does it exist a way? Thanks for advance. (4 Replies)
Discussion started by: solaris21
4 Replies

4. Shell Programming and Scripting

Invoking system(cmd) inside awk command

Hi, I was searching for a way to grep 2 lines before and after a certain keyword, and I came across the following code.. awk "\$0 ~ /ORA-/ { cmd=\"awk 'NR>=\" NR-2 \" && NR<=\" NR+2 \"' init.ora\" system(cmd) }" input_file I could not understand how this works. What is system() ? what... (2 Replies)
Discussion started by: Kulasekar
2 Replies

5. Shell Programming and Scripting

awk - System command not working

dear All, my awk system command isn't working or rather I'm missing something in my command. Appreciated , if anyone can assist me what exactly I'm missing ?? awk ' /^/ { > c=split($3,a,"/") ;for(n=1; n<=c; ++n) > { > if (system("test -d" /home/cubedata/20120104/"$1"/"a")) { > print... (5 Replies)
Discussion started by: manas_ranjan
5 Replies

6. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

7. Shell Programming and Scripting

awk - Pre-populating an array from system command output

So, here's a scenario that requires the same logic as what I'm working on: Suppose that you have a directory containing files named after users. For awk's purposes, the filename is a single field-- something parse-friendly, like john_smith. Now, let's say that I'd like to populate an array in... (2 Replies)
Discussion started by: treesloth
2 Replies

8. Shell Programming and Scripting

Hide status value from awk system command

Hi, When i use the system( ) function inside a awk, i am getting the ouput with a 0 appended in a new line. Can someone guide me to eliminate the extra line containing 0. Ex : awk -F"|" '{print system("convert.sh" $1}' The output is displayed with 0 in a new line. ... (8 Replies)
Discussion started by: muruganksk
8 Replies

9. Shell Programming and Scripting

awk system() command not working

I am using Sun Solaris 5.8 I am trying to run a system command such as ls and echo inside awk, but when I run the following code system echo is not displayed. bash-2.03$ ls | awk 'BEGIN { print "first print" system("echo system echo") print "second print" ... (1 Reply)
Discussion started by: rakeshou
1 Replies

10. Shell Programming and Scripting

can I pass awk variable to system command?

I wanna use a system function to deal with several data. So I use awk variable FILENAME to transfer the file directory to system command, but it does not work. I use a shell function "out_function" to deal with data and save the result in another directory with the same file name. How can I... (2 Replies)
Discussion started by: zhynxn
2 Replies
Login or Register to Ask a Question