bash - Pipe a list or array in awk as variable (to be executed as parameter in other command) -


i trying execute 1 liner below part of bash script.

command1 |grep -id|grep -v + | awk '{print "command2" $2}'|bash 

the first part of pipe prints info below:

root@system:~# command1 |grep -v id|grep -v + | id                                   | name                     | mac_address       | fixed_ips                                                                            | | 00277225-34fa-48f5-9a2a-ee5f1c5b1dcb | dummy                    | fa:18:3e:c4:85:94 | {"subnet_id": "0cd4d824-4420-4049-87c3-ed33c3addbf5", "ip_address": "11.170.1.121"}  | : : | ff9a6ed5-9694-45bc-bf71-59565f96d809 | bat-t0-a2-0-7-tport      | fa:18:3e:62:70:fb | {"subnet_id": "f9ae81ed-3b1a-45a7-96fd-c417ed32 

so, $2 in awk command2 "00277225-34fa-48f5-9a2a-ee5f1c5b1dcb". e.g.

command2 00277225-34fa-48f5-9a2a-ee5f1c5b1dcb 

the whole purpose of one-liner execute number of "command2" instances different parameter values printout above. e.g.

command2 00277225-34fa-48f5-9a2a-ee5f1c5b1dcb command2 ff9a6ed5-9694-45bc-bf71-59565f96d809 : : 

but can not make $2 recognized way below

command1 |grep -id|grep -v + | awk '{print "command2" $2}'|bash 

i think missing few syntax tricks here (as newbie).

p.s: if copy / paste whole line in command line, works fine.

you seem looking for

command1 | awk '!/id/ && !/\+/ {print $2}' | xargs -n 1 command2 

i refactored ugly useless greps awk script; real beef here xargs. reads parameters standard input , passes them on command supply in positional parameters.

the option -n 1 says accept 1 additional argument @ time; if command2 well-written standard unix command, can accept arbitrary number of arguments, , loop on them. in case, removing -n 1 lot more efficient.

incidentally, original attempt close; should have added space after command2 in print statement. hope solution see how "think unix".


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -