0

I have the following in my script :

function processurl
{
}

some_text%%%url1%%%url2%%%url3%%%

with number of urls varying between 0 to any finite number (in case of 0 the string is some_text%%%)

I want to apply processurl to the urls and give the output as :

some_text%%%processurl url1%%%processurl url2%%%processurl url3%%%

Any clues on how to do this?

2 Answers 2

1

this is actually the same question as this(also from you) Scripting in Bash

you could save your processURL function as a script, expecting the parameter, and in awk sending $i to this script, and get result. (you could use getline in awk).

see my answer in the other question, there is an example.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. Indeed they are the same, it was just that question there was not very clear. And after editing the post, I felt probably this might make the other suggested answers look irrelevant. Thanks again for the answer :)
Any idea of the "spacing" issue (or rather any special character in i)
1

in a language like awk you cannot pass a function as an argument. So you have to do it the other way round. First you have to split the input, the process it and join it again. You can do it like this:

This will split the input:

awk -F"%%%" '{for(i=2;i<=NF;i++){if (i==NF){print ""}else print $i}}' file

Now you can read it linewise and apply your function, which you should save in a shell script "processurl":

| while read url; do ./processurl $url; done

And join it again:

awk 'BEGIN{RS=""}{for (i=1;i<=NF;i++)
    {if (i==NF){printf "\s%%%%%%\n",$i}else{printf "%s%%%%%%",$i}}}'

HTH Chris

1 Comment

As suggested by @Kent you can avoid dividing and joining back by the proposed solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.