blaCkninJa
blaCkninJa

Reputation: 455

Pass one command output to other command

I wanted to extract column 3 through awk '{print $3}' mysql and add command[column 3 output]= at start of that file(mysql) and so on. I am unable to do it using sed:

sed "s/^/command[$(awk '{print $3}' mysql)]=/" mysql  

File Content:

/usr/local/check_abc -a Handler_read_rnd -A Handler_read_rnd -w ">49000000" -c ">50000000"
/usr/local/check_abc -a Handler_delete -A Handler_delete -w ">520000" -c ">530000"

Output Should be like:

command[Handler_read_rnd]=/usr/local/check_abc -a Handler_read_rnd -A Handler_read_rnd -w ">49000000" -c ">50000000"
command[Handler_delete]=/usr/local/check_abc -a Handler_delete -A Handler_delete -w ">520000" -c ">530000"

How can I make that command work?

Thanks in advance,

Upvotes: 1

Views: 288

Answers (3)

hek2mgl
hek2mgl

Reputation: 157992

Just use awk:

awk '{print "command[" $3 "]=" $0}' mysql.txt

Explanation:

$0 is a special variable it contains the whole line of input. $1 - $xxx contain the fields in the input. $3 is field 3.

Output:

command[Handler_read_rnd]=/usr/local/check_abc -a Handler_read_rnd -A Handler_read_rnd -w ">49000000" -c ">50000000"
command[Handler_delete]=/usr/local/check_abc -a Handler_delete -A Handler_delete -w ">520000" -c ">530000"

Upvotes: 2

potong
potong

Reputation: 58420

This might work for you (GNU sed):

sed -r 's/^((\S+)(\s+)?){3}/command[\2]=&/' file

Match the first three arguments of each line and using pattern matching and grouping manufacture the required string.

N.B. Iterations of groups within groups always remember the last match.

Upvotes: 0

Campiotti
Campiotti

Reputation: 215

A quite simple search ended up with me finding this: https://unix.stackexchange.com/questions/108782/pass-the-output-of-previous-command-to-next-as-an-argument

I quote:

To answer your question, it sounds like you want to capture the rate from the output of the first command, and then use the rate as a command line argument for the second command. Here's one way to do that:

rate=$(command1 | sed -ne 's/^rate..\([0-9]*\)%.*/\1/p')
command2 -t "rate was $rate"

check out that page for more info if this doesn't suffice.

Upvotes: 0

Related Questions