How to Paste Columns from Separate Files Using Bash

How to paste columns from separate files using bash?

You were on track with paste(1):

$ paste -d , date1.csv date2.csv 
Bob,2013-06-03T17:18:07,2012-12-02T18:30:31
James,2013-06-03T17:18:07,2012-12-02T18:28:37
Kevin,2013-06-03T17:18:07,2013-06-01T12:16:05

It's a bit unclear from your question if there are leading spaces on those lines. If you want to get rid of that in the final output, you can use cut(1) to snip it off before pasting:

 $ cut -c 2- date2.csv | paste -d , date1.csv -
Bob,2013-06-03T17:18:07,2012-12-02T18:30:31
James,2013-06-03T17:18:07,2012-12-02T18:28:37
Kevin,2013-06-03T17:18:07,2013-06-01T12:16:05

How to use paste to append one column from file2 to file1

Use cut and paste like so:

paste file1.txt <(cut -f2 file2.txt) > out_file.txt

Your error relates to your disk being full. Delete extra files to free up some space.

How to paste columns from multiple files in an orderly way?

Normalize the filenames first.

for f in file_?.dat ; do
mv "$f" "${f/_/_0}"
done

It replaces _ by _0 in all the files with single-digit numbers (? matches a single character).

How to paste multiple columns of two different files with paste?

Your files have \r\n line endings, so the first line is actually:

8.0\t175.0\r\t8.0\t188.5\r\n
#^^^^^^^^^^^..^^^^^^^^^^^^
# file1 file2
# tab added by paste

run dos2unix or sed -i.bak 's/\r$//' on your files.

Paste two files in bash (columns side by side)

Remove the DOS line endings from the data files (file*.txt?) before combining them.

copying columns from different files into a single file using awk

Given the input

$ cat a.txt
a 1
b 2
c 3
$ cat b.txt
a I
b II
c III
$ cat c.txt
a one
b two
c three

you can loop:

cut -f1 a.txt > result.txt
for f in a.txt b.txt c.txt
do
cut -f2 "$f" | paste result.txt - > tmp.txt
mv {tmp,result}.txt
done
$ cat result.txt
a 1 I one
b 2 II two
c 3 III three

How to copy unique columns from multiple txt files to form one new txt file in unix

output without order or any relationship:

1, $4"}' there is a extra ", should remove it in your command.

2, the last FileC.txt should change FileB.txt.

3, the paste following but its not working's code maybe has some error format? could your upload a screenshot for that ?

4, just only merge the column without something relationship judgement ?

If yes, try this( only some format fixed by your code to one-line ):

paste <(awk '{print $1"\t"$2}' FileC.txt) <(awk '{print $2}' FileA.txt)  <(awk '{print $2"\t"$3"\t"$4}' FileB.txt) > output.txt

get what you want before me edit your question:

$ cat output.txt 
Subject_ID Data_ID sample_ID age sex smok
1869793 2253798 20481 11 2 1
1869585 2253793 20483 7 1 3
1870238 2253791 20488 9 2 1

output with order and the relationship:

join -1 2 -2 1 <(join -1 2 -2 1 <(sort -k2 FileC.txt) <(sort -k1 FileA.txt) | sort -k2) <(sort -k1 FileB.txt) | tac | awk 'NR==1 {line=$0; next} 1; END{print line}' | tac

get the output:

Subject_ID Data_ID sample_ID age sex smok
1869585 2253793 20483 7 1 3
1869793 2253798 20488 11 2 1
1870238 2253791 20481 9 2 1

1, use join to merge two file's line by the same column which had been sort.

2, use tac file | awk 'NR==1 {line=$0; next} 1; END{print line}' | tac move the header to the top.



Related Topics



Leave a reply



Submit