Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Join function 1

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
US
I am missing something here, what's wrong with this?

awk '
function strjoin(a1,b1,js, finished) {

finished = a1jsb1
return finished
}

BEGIN {

printf "New separator: "
getline N < &quot;-&quot;

}

/[a-z]/ {
if ($0 ~ /pat/) {
x = $0
} else if ($0 ~ /pat2/) {
o = $0
}

strjoin(x,o,N)

printf &quot;Now reading: %s&quot;, finished
}' file

Function returns nothing. Do I need sprintf first?

TIA
 
Hi,

I see nany problems in yor program :

1) finished = a1jsb1

You assign to 'finished' the variable 'a1jsb1'. This variable is not initialized so it's value is &quot;&quot;.
The operator for concatenation is ' '.
The correct syntax for your assigment is :

finsihed = a1 js b1

2) return finished

You don't need to use the variable 'finished'.
You can write :

return a1 js b1

3) strjoin(x,o,N) ; printf &quot;Now reading: %s&quot;, finished

The variable 'finished' is local to the strjoin function and doesn't exist outside the function.
The correct use of the function is :

printf &quot;Now reading: %s&quot;, strjoin(x,o,N)

4) else if ($0 ~ /pat2/) {

The test will never be true, because if $0 doesn't match /pat/, $0 will never match /pat2/.
You must invert the 2 tests on $0 :

if ($0 ~ /pat2/) {
o = $0
} else if ($0 ~ /pat/) {
x = $0
}

The final verion of your awk program :

function strjoin(a1,b1,js) {
return a1 js b1
}

BEGIN {
printf &quot;New separator: &quot;
getline N < &quot;-&quot;
}

/[a-z]/ {
if ($0 ~ /pat2/) {
o = $0
} else if ($0 ~ /pat/) {
x = $0
}
printf &quot;Now reading: %s\n&quot;, strjoin(x,o,N)
}
Jean Pierre.
 
Thanks Aigles, thats a big help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top