Mikko
Mikko

Reputation: 29

How to copy second column from all the files in the directory and place them as columns in a new text file

I have 150 tab delimited text files, I want to copy the 2nd column of each file and paste next to another in a new text file. the new file will have 150 columns of 2nd column from each file. Help me guys. This code worked but placed each column under the other, forming one loooong column.

for file in *.txt
do
   awk '{print $2}' *.txt > AllCol.txt
done

Upvotes: 0

Views: 1800

Answers (2)

karakfa
karakfa

Reputation: 67507

Here is another approach without looping

$ c=$(ls -1 file*.tsv | wc -l); cut -f2 file*.tsv | pr  -$c -t

Upvotes: 2

peak
peak

Reputation: 116780

#!/bin/bash

# Be sure the file suffix of the new file is not .txt
OUT=AllColumns.tsv
touch $OUT

for file in *.txt
do
  paste $OUT <(awk -F\\t '{print $2}' $file) > $OUT.tmp
  mv $OUT.tmp $OUT
done

One of many alternatives would be to use cut -f 2 instead of awk, but you flagged your question with awk.

Since your files are so regular, you could also skip the do loop, and use a command-line utility such as rs (reshape) or datamash.

Upvotes: 0

Related Questions