Home > OS >  how to concatenate two lines (one indented) in a txt in linux?
how to concatenate two lines (one indented) in a txt in linux?

Time:09-22

I have a txt file like this:

chr1    1300000 1350000
    chr1    1335278 1349418 -   14141   DVL1    0
chr1    1500000 1550000
    chr1    1335278 1349418 -   14141   DVL1    -150583
chr1    1950000 2000000
    chr1    1785285 1891117 -   105833  GNB1    -58884

And I would like to concatenate each two lines (like this)

chr1    1300000 1350000 chr1    1335278 1349418 -   14141   DVL1    0
chr1    1500000 1550000 chr1    1335278 1349418 -   14141   DVL1    -150583
chr1    1950000 2000000 chr1    1785285 1891117 -   105833  GNB1    -58884

I've been googled and I tried paste -s -d '\n' file but doesn't work as desired Any advice? Thank!

CodePudding user response:

I suggest to play around with...

#cat test.txt
chr1    1300000 1350000
    chr1    1335278 1349418 -   14141   DVL1    0
chr1    1500000 1550000
    chr1    1335278 1349418 -   14141   DVL1    -150583
chr1    1950000 2000000
    chr1    1785285 1891117 -   105833  GNB1    -58884
#printf "%s\n" "$(cat test.txt|grep -o -E 'chr1.*[0-9]{7}.*[-].*[A-Z].*')" >test.res
#cat test.res
chr1    1335278 1349418 -   14141   DVL1    0
chr1    1335278 1349418 -   14141   DVL1    -150583
chr1    1785285 1891117 -   105833  GNB1    -58884

CodePudding user response:

From terminal you can just do something like this:

$ echo -n "chr1    1300000 1350000 " >> file.txt 
$ echo -n "chr1    1335278 1349418 -   14141   DVL1    0" >> file.txt

The -n prevents the echo function from adding a new line.

CodePudding user response:

Try:

sed 'N;s/\n//' file

But you could just:

while IFS= read -r line1 && IFS= read -r line2; do
    echo "$line1 $line2"
done <file
  • Related