Passing part of the templates from sed to the shell

I have a file that contains lines of the following format:

w1 # 1 # x w2 # 4 # b w3 # 2 # d ...

Each word (token) in a string (for example, w1 # 1 # x) consists of 3 parts, the first shows an index (w1 in this case), the second an integer (in this case 1) and the third character (x in this case )

Now for each word (token) I need to print an additional field, which will be calculated based on the value of the second and third parts (i.e. the fourth part will be a function of the 2nd and 3rd parts), and the output file should look like this:

w1 # 1 # x # f1 w2 # 4 # b # f2 w3 # 2 # d # f3 ...

Where

f1 = function (1, x), f2 = function (4, b), f3 = function (2, d)

Now, using sed patterns, I can identify the components in each word (token), for example,

echo $line | sed "s/([^ #]) # ([^ #]) # ([^ #] *)/\ 1 #\2 #\3/g"

\2 \3 - ( - )

\2 \3. getInfo(), 2 . sed. :

echo $line | sed "s/([^ #]) # ([^ #]) # ([^ #] *)/\ 1 #\2 #\3`getInfo\2\3`/g"

. Shell .

, :

sed ()?

script, , , , sed sed.

,

+5
2

, sed . , ( . potong, , bash sed).

? , , , awk , . , , Perl, Python ( Ruby) . , .

sed , . sed, , () , , sed script, , script .

+3

:

func(){ echo "$1#$2#$3#$2$3"; }
export -f func
echo "w1#1#x w2#4#b w3#2#d" |
sed 's/\([^#]*\)#\([^#]*\)#\([^ ]*\) \?/echo -n "$(func \1 \2 \3) "; /g;s/$/echo ""/' |
sh
w1#1#x#1x w2#4#b#4b w3#2#d#2d 

, GNU sed:

func(){ echo "$1#$2#$3#$2$3"; }
export -f func
echo "w1#1#x w2#4#b w3#2#d" |
sed 's/\([^#]*\)#\([^#]*\)#\([^ ]*\) \?/echo -n "$(func \1 \2 \3) "; /ge;s/.$//'
w1#1#x#1x w2#4#b#4b w3#2#d#2d
+6

All Articles