How to replace row with another row in linux having NA and nucleotide bases
1
0
Entering edit mode
6.5 years ago
Kritika ▴ 260

I have file 1:

A1  1  NA
A1  2  NA
A1  3  NA
A1  4  A
A1  5  G
A1  6  T
A1  7  NA
A1  8  NA
A1  9  NA
A2  1  NA
A2  2  NA
A2  3  T
A2  4  NA

And file 2:

A1  4  A
A1  5  C
A1  6  T
A2  3  T

I want to replace row number 4,5,6 and 3 of A2 from file 1 with a value of 4,5,6 and 3 from file 2

Expected Output in new file3:-

 A1  1  NA
 A1  2  NA
 A1  3  NA
 A1  4  A
 A1  5  C
 A1  6  T
 A1  7  NA
 A1  8  NA
 A1  9  NA
 A2  1  NA
 A2  2  NA
 A2  3  T
 A2  4  NA

I want to try this in Linux. I tried googling but I could not find better answers.

I tried in linux.

diff file2 file1
join -a 2 file2 file1 | cut -d ' ' -f -2

But i am not getting answers

Linux Replace • 2.0k views
ADD COMMENT
0
Entering edit mode
6.5 years ago
Paul ★ 1.5k

you can use awk:

cat file2 file1 | awk '!seen[$1,$2]++' | sort -k 1V,1 -k 2n,2 > result

Output:

A1  1  NA
A1  2  NA
A1  3  NA
A1  4  A
A1  5  C
A1  6  T
A1  7  NA
A1  8  NA
A1  9  NA
A2  1  NA
A2  2  NA
A2  3  T
A2  4  NA
ADD COMMENT
0
Entering edit mode

No its not working it is not making any file ! No output

ADD REPLY
0
Entering edit mode

Hi, Are your files tab delimited? cat -T input is what result?

ADD REPLY
0
Entering edit mode

its giving me what is there in file value of file1

ADD REPLY
0
Entering edit mode

if you are try to print first column - awk '{print $1}' input - does it work? When I test my on-liner on your pasted data above it is works.

ADD REPLY
0
Entering edit mode

Yes its printing the first column

ADD REPLY
0
Entering edit mode

The above code is not making any file

ADD REPLY
0
Entering edit mode

The columns are seperated by 2 spaces

ADD REPLY

Login before adding your answer.

Traffic: 1137 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6