26956

Scripting in Bash

Question:

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?

Answer1:

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@

Answer2:

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 \([^@]*\)).

Answer3:

You could use sed:

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

Answer4:

awk 'BEGIN {OFS=FS="@"} {for (i=2; i<=NF-1; i++) $i="f("$i")"; print}'

Recommend

  • CODEIGNITER how to insert user_ID to mySQL when that user_ID is logged in
  • php find distance of a point and a line segment not a line in 2D
  • After recent update vs code transpiling stopped working: tsc is not recognized as internal or extern
  • PHP Compare keys and change certain values from a multidimensional array with corresponding keys
  • How do I get GNU grep to match exactly “H” and not things that just start with “H”?
  • How to make Column to Row without an aggregate function in sql server 2005/8?
  • use preg_replace to replace whole words using associative array
  • Assign txt file data to struct node in linked list
  • How to use awk to delete rows from one csv, some values in a column of which matche the other csv co
  • how can i read a csv file containing some additional text data
  • Theano : Pass iterating index of scan to the called function
  • Unable to create Web Service Client from WSDL URL in NetBeans
  • HTML file fetched using 'wget' reported as binary by 'less'
  • What happened to Guava Constraints? [closed]
  • Syntax error near unexpected token 'elif'
  • Building jamvm 1.5.4 on OS X Lion
  • How can I get the full list of running processes on a Mac from a python app
  • bad substitution shell- trying to use variable as name of array
  • Reduction and collapse clauses in OMP have some confusing points
  • MongoDb aggregation
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Dialing with Intent.ACTION_CALL stopps at # in phone number
  • DomPDF {PAGE_NUM} not on first page
  • Is there a javascript serializer for JSON.Net?
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Master page gives error
  • Javascript simulate pressing enter in input box
  • Deserializing XML into class C#
  • Importing jscolor library in angular 2
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Why winpcap requires both .lib and .dll to run?
  • A cron job substitute?
  • WPF Applying a trigger on binding failure
  • How to set the response of a form post action to a iframe source?
  • python draw pie shapes with colour filled
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • Can't mass-assign protected attributes when import data from csv file
  • sending mail using smtp is too slow
  • How to Embed XSL into XML