Function's return value used inside awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Function's return value used inside awk
# 1  
Old 12-09-2008
Function's return value used inside awk

I have a file with the record of person:

Code:
cat > $TMP/record.txt
John Torres M Single 102353 Address
Mark Santos M Maried 103001 Address
Carla Maria F Maried 125653 Address

Code:
#!/bin/ksh
ManipulateID(){ 
	...
	return 0;
	... #or
	return 1;
}

cat $TMP/record.txt | awk 'BEGIN {printf ("%s", "'"`ManipulateID "$5"`"'")}'

The challenge I am facing is that I have to manipulate the 5th column in my file (record.txt) inside awk, then I have to pass the 5th column value to a
function in my script, the function will do some manipulation and test. The function will return 0 if the test is good and 1 if not. The returning
value of 0 and 1 will be return back to awk and awk will test it. If the returning value of my function is 0 awk will display the whole row ($0)
and do nothing if returning value of function is 1.

Anyone who can help me?
# 2  
Old 12-09-2008
The use of cat with awk is redundant.
To call your script you can use the system() function.
Try this, if the returncode of your script is 0 (not true, hence the "!" before the system command) awk should print the whole line:

Code:
awk '!(system("ManipulateID "$5))' $TMP/record.txt

Regards
# 3  
Old 12-09-2008
It's returning an error to me.

Code:
/tmp/myscript[2]: syntax error at line 3 : `(' unexpected

The line 3 here is the ManipulateID() {

Code:
#!/bin/ksh

ManipulateID() {
 some codes here
}

awk '!(system("ManipulateID "$5))' $TMP/record.txt

I though only unix commands (ex. date, ls, etc.) will only do inside the system() in awk
# 4  
Old 12-09-2008
Quote:
Originally Posted by Orbix
I though only unix commands (ex. date, ls, etc.) will only do inside the system() in awk
You can call programs or scripts with the system() command but not a shell function with awk within a script like this.
If you want to manipulate the data with a shell script, you have to use separate scripts, one to manipulate the data and one with the awk command, but why don't you manipulate the data within awk?
Anyhow, if you want to play around with it, the scripts should look like:

awk script (you can also type the awk command at the prompt):

Code:
#!/bin/sh

awk '!(system(".\ManipulateID "$5))' $TMP/record.txt

The ManipulateID script:

Code:
#!/bin/sh

echo $1        # This is the 5th field of awk passing as a parameter

# Manipulate the data here

if [ .. ]; then
  exit 0
else
  exit 1
fi

Regards
# 5  
Old 12-09-2008
Something like this would work:

Code:
#! /bin/ksh

ManipulateID() {
  TEST=something
}

ManipulateID

echo  | awk -v var=$TEST '{printf "%s\n", var}'

Not sure if that helps you or not.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

awk - function to return permutations of n items out of m

Hi, I'm trying to write an awk function that returns all possible permutations of n items chosen in a list of m items. For example, given the input "a,b,c,d,e" and 3, the function should return the following : a a a a a b a a c a b a a b b ... c a a c a b ... e e c e e d e e e (125... (21 Replies)
Discussion started by: cjnwl
21 Replies

3. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

4. 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

5. Shell Programming and Scripting

Return value inside isql to a shell variable in ksh

Hello, I have a shell script where I am doing an isql to select some records. the result i get from the select statement is directed to an output file. I want to assign the result to a Shell variable so that I can use the retrieved in another routine. e.g. "isql -U${USER} -P${PASSWD} -S${SERVER}... (1 Reply)
Discussion started by: RookieDev
1 Replies

6. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

7. Shell Programming and Scripting

return in function

I am using ksh. I want to know how can we make any function to return string or double value. I dont want to use the global variables. (5 Replies)
Discussion started by: PRKS
5 Replies

8. Shell Programming and Scripting

calling function inside awk

Hi All, My 1.txt contains some functions fun1() .... .... fun2() .... .... I can call these fun from 2.txt inside awk as below value="`fun1 "argument1"`" awk 'BEGIN {printf ("%s", "'"$value"'")}' I need to modify the above code so that without using the variable to store... (2 Replies)
Discussion started by: jisha
2 Replies

9. Shell Programming and Scripting

Return an array of strings from user defined function in awk

Hello Friends, Is it possible to return an array from a user defined function in awk ? example: gawk ' BEGIN{} { catch_line = my_function(i) print catch_line print catch_line print catch_line } function my_function(i) { print "echo" line= "awk" line= "gawk"... (2 Replies)
Discussion started by: user_prady
2 Replies

10. Shell Programming and Scripting

return value of a function

Hi I have a doubt in the way the variables inside a function are treated . if a function is called from the main script directly, the variables inside them act as global variables. however if the return value of the function is stored to some other variable in the main script as shown,... (3 Replies)
Discussion started by: prez
3 Replies
Login or Register to Ask a Question