Reputation: 1404
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?
Upvotes: 2
Views: 5861
Reputation: 185126
Using awk:
awk 'NR%2{printf "%s\t", $0;next}1' file
123123 string A
456456 string B
789789 string C
Using perl:
perl -pe 'chomp; $_ = ($. % 2) ? sprintf "%s\t", $_ : sprintf "%s\n", $_;' file
Using bash:
c=0
while read a; do
((c++))
if ((c%2)); then
printf "%s\t" "$a"
else
echo "$a"
fi
done < file
Upvotes: 0
Reputation: 274612
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.
Upvotes: 5
Reputation: 488
Yo can try the following:
paste <(grep -E '^[[:digit:]]+' myFile) \
<(grep -E '^[[:alpha:]]+' myFile) \
Upvotes: 2
Reputation: 123508
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
Upvotes: 1