0

I have a file (data.txt) like following (with more data)

chr1    11106936    11107192    MTOR
    7   6   256 0.0234375
chr1    11106936    11107192    MTOR
    8   14  256 0.0546875
chr1    11106936    11107192    MTOR
    9   7   256 0.0273438
chr1    11106936    11107192    MTOR
    11  1   256 0.0039062
chr1    11106936    11107192    MTOR
    13  26  256 0.1015625
chr1    11106936    11107192    MTOR
    18  1   256 0.0039062
chr1    11106936    11107192    MTOR
    19  11  256 0.0429688
chr1    11106936    11107192    MTOR
    20  11  256 0.0429688
chr1    11106936    11107192    MTOR
    22  13  256 0.0507812
chr1    11106936    11107192    MTOR

I need to align data on even line number to the above odd line as follows

chr1    11106936    11107192    MTOR     7   6   256 0.0234375
chr1    11106936    11107192    MTOR     8   14  256 0.0546875
  • 1
    What did you try to solve this problem? Can you share the code that you are having a problem with? Would you like to solve it using `bash` or `python` or both? – Dima Chubarov Mar 06 '20 at 06:33

5 Answers5

0

cat data.txt | paste - - > output.txt

Rafaf Tahsin
  • 7,652
  • 4
  • 28
  • 45
  • 2
    You don't need the cat, here `paste - - < data.txt` – Jetchisel Mar 06 '20 at 06:40
  • 1
    i tried the above code. But output is same as input –  Mar 06 '20 at 06:40
  • This should be the best approach. Perhaps you don't want to wrap 2 lines, but you have long lines that are wrapped or something with an `\r`. Can you check `head -2 data.txt | hexdump -c` ? – Walter A Mar 06 '20 at 10:20
0

Using a while read loop in bash.

#!/usr/bin/env bash

counter=0

while IFS= read -r line; do
  even=$((counter%2))
  if ((even)); then 
    printf '%s\n' "$line"
  else  
    printf '%s' "$line"
  fi    
  ((counter++))
done < data.txt
  • It will be very slow on large file/data size
Jetchisel
  • 7,493
  • 2
  • 19
  • 18
0

Would you try the following:

while read -r first; do
    read -r second
    echo "$first" "$second"
done < data.txt
tshiono
  • 21,248
  • 2
  • 14
  • 22
0

Using awk:

$ awk '{
    sub(/\r$/,"")      # just in case, remove windows line-endings
    if(NR%2)           # every odd record ...
        b=$0           # buffer them 
    else               # even records ...
        print b $0     # output buffered and current record
}' file

Some output:

chr1    11106936    11107192    MTOR    7   6   256 0.0234375
chr1    11106936    11107192    MTOR    8   14  256 0.0546875
chr1    11106936    11107192    MTOR    9   7   256 0.0273438
James Brown
  • 36,089
  • 7
  • 43
  • 59
-1

Read txt:

file = open('in.txt')
lines = file.readlines()

Seprate odd-even lines

odd_lines = []
even_lines = []

for i,line in enumerate(lines):
    if i%2==0:
        even_lines.append(line.strip('\n'))
    else:
        odd_lines.append(line.strip('\n'))

Write output text:

with open('out.txt','a') as out:
    for e,o in zip(even_lines,odd_lines):
        out.write(e+o+'\n')
Himanshu
  • 666
  • 1
  • 8
  • 18