Question: Add string to a column of a file and retain the rest
0
gravatar for Inquisitive8995
6 days ago by
Inquisitive899530 wrote:

Hi, I have a file with multiple rows which look like

1101:4422:200A   gi|12345|ref|NC_021085.1|   100     N/A     bacterium AC A78261

I want to add /1 after the first column and keep all the other columns intact. so that the output looks like

1101:4422:200A/1   gi|12345|ref|NC_021085.1|   100     N/A     bacterium AC A78261

I tried using awk. awk '{$1 = $1"/1"; print}' file > output though it gives me the additional "/1" , the output file does not retain the same delimiters as the input. The input file has multiple spaces as a delimiter and does not have tab or single space. Is there any other way to do this ? Thanks in advance.

awk linux • 67 views
ADD COMMENTlink modified 4 days ago • written 6 days ago by Inquisitive899530

try this:

$ sed 's/\s/\/1 /' test.txt 
1101:4422:200A/1   gi|12345|ref|NC_021085.1|   100     N/A     bacterium AC A78261
ADD REPLYlink written 6 days ago by cpad01129.3k

Thanks for your reply but this joined the first and second column into 1.

ADD REPLYlink modified 6 days ago • written 6 days ago by Inquisitive899530
0
gravatar for Pierre Lindenbaum
6 days ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum112k wrote:
sed 's/\([\t ]\)/\/1\1/' file > output
ADD COMMENTlink written 6 days ago by Pierre Lindenbaum112k

Thanks for your reply. But this added the \t between column 1 and 2 but did not add "\1"

ADD REPLYlink written 6 days ago by Inquisitive899530
0
gravatar for Inquisitive8995
4 days ago by
Inquisitive899530 wrote:

I think I answered my own question. It works when I use awk as given below:

awk -v FS="\t" -v OFS="\t" '{print $1"/1",$2,$3,$4,$5,$6} inputfile.txt > output.txt
ADD COMMENTlink written 4 days ago by Inquisitive899530
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1857 users visited in the last hour