shell function, system, awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell function, system, awk
# 1  
Old 06-02-2008
shell function, system, awk

Hi all,

I am calling a shell function within awk using system. I am struggling to get my expected output:

Code:
#!/bin/sh

set -o nounset
BEG=44
END=55

shfun()
{
  echo "1|2|3"
}

export -f shfun
typeset -F > /dev/null

awk 'BEGIN {OFS="|"; print "'"$BEG"'",system( "shfun" ),"'"$END"'"}'

Code:
$ ./funcawk
1|2|3
44|0|55

But, I want the output as: (in the same line)
Code:
44|1|2|3|55

Please help
# 2  
Old 06-02-2008
The function prints its output, including a trailing newline, when it is invoked. Then awk prints the result of its own print command. You need to capture the output from the shell function in awk, like with backticks in the shell. My first question would be whether you couldn't avoid this somehow.

If the output from the function is just a static string, it's really simple:

Code:
awk -v fun=`shfun` -v beg="$BEG" -v end="$END" 'BEGIN{OFS="|"; print beg fun end }'

... or if your awk doesn't understand -v, with direct interpolation of shell variables in the script itself like you already had.
# 3  
Old 06-02-2008
era, thanks a lot for your valuable reply.

I have a query related to this

Code:
$ cat file.txt
AA
BB
CC
DD

Code:
$ cat my.sh
set -o nounset

diff=26
start=1

#Print random number between 1-30
f_random () {
        firstran=`echo $((RANDOM%$diff+$start))`
        second=`echo $((RANDOM%$diff+$start))`
        echo "$firstran|$second"
}

export -f f_random
typeset -F > /dev/null

awk -v fun=`f_random` 'BEGIN{OFS="|"}; {print $0,fun}' file.txt

Code:
$ ./my.sh
AA|14|3
BB|14|3
CC|14|3
DD|14|3

I expected it to print different set of random numbers for each record AA,BB,CC, DD , but its not printing.

Basically what I expected, execute my function f_random for each row in file.txt. As the volume of file.txt is huge, I can't make a normal for loop for this purpose.
# 4  
Old 06-02-2008
It's only invoking the backtics once, so no surprise there. In this case, the text you want is not static, so the workaround doesn't work.

If your awk script is no more complex than this, why do you need awk in the first place? Also note that newer versions of awk have a built-in rand() function. Or if you don't have that, but are on a platform which offers /dev/random maybe you can use that from within awk instead.
# 5  
Old 06-02-2008
awk can do more than simple one liners.
Code:
awk 'BEGIN{
 OFS="|"
 srand()
 top=26 
}
{
  print $0,int((rand() * top )),int((rand() * top ))
  
}' file

output:
Code:
# ./test.sh
AA|8|17
BB|5|10
CC|12|7
DD|3|5

# 6  
Old 06-02-2008
era and ghostdog74 thanks a lot for your replies.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

System function in awk

Hello Friends, I have written a script like below, I aimed to move some CDR files (call data record) whose the last field is "1" (NF=1 ) from a spesific directory to a new directory Field Seperator is pipe. If the directory does not exitst i should create it. I will give the script two... (5 Replies)
Discussion started by: EAGL€
5 Replies

3. Shell Programming and Scripting

Awk: How to get an awk variable out to the shell, using system() ?

I am reasonably capable with awk and its quirks, but not with shell weirdness. This has to be Bourne Shell for portability reasons. I have an awk program that is working just fine; it handles multiple input streams and produces several reports, based on the request (-v Variables). In addition... (3 Replies)
Discussion started by: DerekAsirvadem
3 Replies

4. Shell Programming and Scripting

Call shell script function from awk script

hi everyone i am trying to do this bash> cat abc.sh deepak() { echo Deepak } deepak bash>./abc.sh Deepak so it is giving me write simply i created a func and it worked now i modified it like this way bash> cat abc.sh (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

5. UNIX for Dummies Questions & Answers

passing coloumn value to system function in awk

Hi all, I have a file which contain data like 1|2009-12-12 2|2010-02-28 3|2009-02-29 i have to validate that second coloumn have proper date i tried like awk -F "|" ' !system("touch -d $2 file1 2> /dev/null" ) ' but its not printing any thing but when i try using hard... (2 Replies)
Discussion started by: max_hammer
2 Replies

6. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

7. Shell Programming and Scripting

How to use system function in awk

Could any one tell me how to use the system function in awk? I want to use it to print the system date. I have been trying like this : yes |head -1|awk '{ system("date")}' When I execute the above it always returns back to the prompt. Your help would be much appreciated. regards,... (3 Replies)
Discussion started by: srikanth_ksv
3 Replies

8. Shell Programming and Scripting

awk function from shell

Hi all. I have a selection of awk functions which I have written and tested in a main awk program. From the command line I use nawk -f function -f main to run main and call my functions from within it. I now wish to use these awk function again but want to call them from inside an awk section of... (1 Reply)
Discussion started by: pxy2d1
1 Replies

9. UNIX for Dummies Questions & Answers

How to use ${?} and system() function???

Hi All, I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode' commands for the same and those worked fine. Please find below the same. X " ( cat /sastemp/body.txt; uuencode test.xls test.xls ) | mailx -s 'testing'... (1 Reply)
Discussion started by: manas6
1 Replies

10. Programming

system function in c

Hai Friends I have used the function system() to execute a command. My requirement is that i have to list the files in a directory applying some wildcard paterns. For example if i want to list *.c files i go with the function system("ls *.c"); and the output gets printed on the monitor.... (1 Reply)
Discussion started by: collins
1 Replies
Login or Register to Ask a Question