0

I have a function in bash say parse which takes one argument and function name is f. My file to be processed is somewhat like

a@b@c@
a@d@e@g@
m@n@
t@

I want to give the output as

a@f(b)@f(c)@
a@f(d)@f(e)@f(g)@
m@f(n)@
t@

That is apply function f to all except the first. Any clues so how can I do this?

4 Answers 4

3

This should do it:

sed 's/@\([^@]*\)/@f(\1)/g'

If all the fields are single characters, you can simplify it a bit (say, using . rather than \([^@]*\)).

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

1 Comment

For t@ it is giving me t@f() And I have to APPLY the function on the values and not print the statement. That is if the function is square which takes x, then i should print the value of x^2.
1

maybe this is what you want?

e.g. you have a script called sqr.sh:

kent$  cat sqr.sh
#!/bin/bash
echo $(($1*$1))

now you want to apply the function above on your input:

kent$  echo "foo@2@3@4@
0@10@20@
x@"|awk -F'@' -v OFS=@ '{for(i=2;i<=NF;i++) if($i) "./sqr.sh "$i|getline $i; print}'

foo@4@9@16@
0@100@400@
x@

3 Comments

this worked like charm except that if there are any spaces in $i then it does some bad things.. i tried combinations like "./sqr.sh $i", "./sqr.sh ${i}", "./sqr.sh ""$i", "./sqr.sh ""${i}" but these didn't work..
then remove the spaces before you pass it to your script(sqr.sh for example).
but there are large number of special characters which I am needed to handle.. anyway thanks a lot :-)
1

You could use sed:

sed -e 's/@\(.\)/@f(\1)/g'

6 Comments

For t@ it is giving me t@f() And I have to APPLY the function on the values and not print the statement. That is if the function is square which takes x, then i should print the value of x^2.
I don't see that result: $ echo "t@" | sed -e 's/@\(.\)/@f(\1)/g' gives me "t@" as a result.
If you need to evaluate the result (and it's valid bash) you can simply enclose it in $(...) to cause the output to be executed.
It simply echoes %%%$(parse2(cigar))%%%$(parse2(jump))%%%$(parse2()) rather than applying the function and return the result.
It's a little unclear what you're actually trying to accomplish here. Application of bash functions doesn't use parentheses, so I'm not sure that these are bash functions you are trying to call. However, enclosing the entire sed operation with $(...) will cause the output of sed to be executed by bash. HTH.
|
1
awk 'BEGIN {OFS=FS="@"} {for (i=2; i<=NF-1; i++) $i="f("$i")"; print}'

2 Comments

query=echo "ab@cd@ef%%%" | awk 'BEGIN {OFS=FS="@"} {for (i=2; i<=NF-1; i++) i=$(func $i); print}' This just hangs on the terminal (or wait for the input though I couldn't really think why). I want to apply function f on the element and not just print it in the output.
Your question was unclear. Where is the function defined? in awk/shell/...? Note that $() is shell syntax, not bash. For awk, you'd need to use system()

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.