Say I have a column of data like this:
sample123
sample456
samplexyz
I can easily add two new columns using:
sed -i "s/$/\t1.1\tC/" <file.txt>
Now it's:
sample123 1.1 C
sample456 1.1 C
samplexyz 1.1 C
Now say I append some new data at the bottom:
sample123 1.1 C
sample456 1.1 C
samplexyz 1.1 C
sampleNew1
sampleNew2
sampleNew3
If I try to append the new columns for this data, it appends to all rows, so now when I run:
sed -i "s/$/\t1.2\tA/" <file.txt>
It looks like:
sample123 1.1 C 1.2 A
sample456 1.1 C 1.2 A
samplexyz 1.1 C 1.2 A
sampleNew1 1.2 A
sampleNew2 1.2 A
sampleNew3 1.2 A
How do I add the columns where there are no other columns except the first one?
sed -i '/\t/!s/$/\t1.2\tA/'
with the\t
at the end rather than the space. Thanks – Luther_Blissett Mar 09 '20 at 10:38