Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe Command Output into Awk

Tags:

pipe

awk

cat

I want to pipe the output of a command into awk. I want to add that number to every row of a new column in an existing .txt file. The new column should be at the end, and won't necessarily be column 2.

$command 1

4512438

$ input.txt

A
B
C
D

$ desired_ouput.txt

A   4512438
B   4512438
C   4512438
D   4512438

I think I need to do something along the lines of the following. I'm not sure how to designate that the pipe goes into the new column - this awk command will simply add integers to the column.

$ command1 | awk -F, '{$(NF+1)=++i;}1' OFS=, input.txt > desired_ouput.txt

like image 211
JVGen Avatar asked Apr 25 '26 17:04

JVGen


1 Answers

It doesn't seem like you really want to pipe the value to awk. Instead, you want to pass it as a parameter. You could read it from the pipe with something like:

cmd1 | awk 'NR==FNR{a=$0} NR!=FNR{print $0,a}' - input.txt

but it seems much more natural to do:

awk '{print $0,a}' a="$(cmd1)" input.txt
like image 69
William Pursell Avatar answered Apr 27 '26 21:04

William Pursell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!