Pregunta

I've got a file with numbers on odd lines and text on even lines like this:

123123
string A
456456
string B
789789
string C

I want to get it to this format (tab separated):

123123    string A
456456    string B
789789    string C

I tried to remove all newlines with

tr -s -d " " < myFile

then use sed with

sed -i 's/[0-9]\s/' myFile

but without great success.

Can you help me get this done?

¿Fue útil?

Solución

The simplest way is to use paste as follows:

paste - - < myFile

In this command, paste reads the file from stdin and combines every two lines (that's what the two "-" are for) into a single line separated by a tab.

Otros consejos

Try:

sed '$!N;s/\n/\t/' inputfile

This would join the lines separated by a TAB character.

Example:

$ seq 10 | sed '$!N;s/\n/\t/'
1   2
3   4
5   6
7   8
9   10

Yo can try the following:

paste <(grep -E '^[[:digit:]]+' myFile) \
      <(grep -E '^[[:alpha:]]+' myFile) \

Using :

awk 'NR%2{printf "%s\t", $0;next}1' file
123123  string A
456456  string B
789789  string C

Using :

perl -pe 'chomp; $_ = ($. % 2) ? sprintf "%s\t", $_ : sprintf "%s\n", $_;' file

Using :

c=0
while read a; do
    ((c++))
    if ((c%2)); then
        printf "%s\t" "$a"
    else
        echo "$a"
    fi
done < file
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top