Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing part of patterns from sed to shell
    primarykey
    data
    text
    <p>I have a file which contains lines of the following format:</p> <blockquote> <p>w1#1#x w2#4#b w3#2#d ...</p> </blockquote> <p>Each word (token) in the line (e.g. w1#1#x) is made of 3 parts, the first showing some index (w1 in this case), the second is an integer (1 in this case) , and the third is a character (x in this case)</p> <p>Now, for each word (token), I need to print an additional field which will be calculated based on value of second and third part (<em>i.e.</em>, the 4th part will be a function of 2nd and 3rd part), and the output file should look like:</p> <blockquote> <p>w1#1#x#f1 w2#4#b#f2 w3#2#d#f3 ...</p> </blockquote> <p>where</p> <blockquote> <p>f1 = function(1,x), f2 = function(4,b), f3 = function (2,d)</p> </blockquote> <p>Now, using the sed patterns I can identify the components in every word (token), <em>e.g.,</em></p> <blockquote> <p>echo $line | sed "s/([^#]<em>)#([^#]</em>)#([^# ]*) /\1#\2#\3 /g"</p> </blockquote> <p>where \2 and \3 are <em>parts of the pattern</em> (I am calling them <em>parts of the pattern</em> because of <a href="http://www.grymoire.com/Unix/Sed.html#uh-4" rel="noreferrer">this link</a>)</p> <p>Now, I need to compute the 4th part using \2 and \3. I have defined a shell function getInfo() which takes 2 arguments and does the required computation and gives me back the 4th part. The problem is inserting this function in the sed command. I tried following:</p> <blockquote> <p>echo $line | sed "s/([^#]<em>)#([^#]</em>)#([^# ]*) /\1#\2#\3`getInfo \2 \3` /g"</p> </blockquote> <p>but this is not working. Shell is not receiving the <em>parts of the pattern</em> as arguments.</p> <p>So the question is:</p> <blockquote> <p>How to pass the sed <em>parts of the pattern</em> to a shell (function)?</p> </blockquote> <p>I can easily write a shell script which would split the line word-by-word and do the required job and then stitch the file back, but I would really appreciate if shell can receive <em>parts of the pattern</em> as arguments from sed within the sed command.</p> <p>Regards,</p> <p>Salil Joshi</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload