How to print an nth column in a file using awk?
You could do the whole thing in a BEGIN
statement using getline
awk '
BEGIN {
while(1) {
line = sep = ""
for (i = 1; i < ARGC; i++) {
if ((getline < ARGV[i]) <= 0) exit
line = line sep $2
sep = OFS
}
print line
}
}' input{1..n} > out
You could construct a paste
command to put all the second columns together:
cmd="paste"
for x in input{1..n}; do
cmd="$cmd <(awk '{print \$2;}' $x)"
done
echo $cmd
eval $cmd
I would use the pr
tool, which is designed to columnize data:
awk '{print $2}' input{1..n} | pr -t --columns=n > out
This assumes each file has the same number of lines.