Reputation: 401
I have some data in the format below:
Pin|add
jtyjg
Kolk|aaa||
Kawr|wht u
Disnce
Djhdb|bbb||
I want to convert this to the format below:
Pin|add jtyjg Kolk|aaa||
Kawr|wht u Disnce Djhdb|bbb||
How can I do this?
Upvotes: 1
Views: 153
Reputation: 342
It's not quite clear what you want. This one-liner should work your example, though:
tr -d '\n' < oldfile | sed 's/||/||\n/g' > newfile
Depending on your system, you may need to use literal newlines for sed substitution like so:
tr -d '\n' < oldfile | sed 's/||/||\<RETURN>/g' > newfile
Upvotes: 5
Reputation: 897
I've made the assumption that the original file doesn't have blanks before the end-of-line characters...
This is fairly basic Perl and works for v5.8.9
#!/usr/bin/perl
open( IN, '<', 'text.txt' ); # the input file
open( OUT, '>', 'text2.txt' ); # the output file
while( <IN> ) {
chomp; # get rid of the end-of-line characters
$out .= $_; # add the current input to the output string
if ( /\|\|/ ) { # does this contain the output signal characters "||"?
print( OUT "$out\n" ); # output the built string
$out = ''; # clear the output string
}
else {
$out = $out . ' '; # append a space to the end
}
}
print( OUT $out ); # output anything left over...
Upvotes: 2
Reputation: 2497
Try this..
Input.txt
Pin|add
jtyjg
Kolk|aaa||
Kawr|wht u
Disnce
Djhdb|bbb||
Code
cat Input.txt | tr '\n' ' ' | sed 's/|| ./||~~/g' | tr '~~' '\n'| sed '/^$/d' > Output.txt
Output.txt
Pin|add jtyjg Kolk|aaa||
awr|wht u Disnce Djhdb|bbb||
Upvotes: 2
Reputation: 753625
On the face of it, you want groups of three input lines to be combined into one with spaces in place of the original newlines. Given that the question does not restrict the tool-set, then a Perl solution is moderately apposite:
#!/usr/bin/env perl
use strict;
use warnings;
my($l1, $l2, $l3);
while (defined($l1 = <>) && defined($l2 = <>) && defined($l3 = <>))
{
chomp($l1, $l2);
print "$l1 $l2 $l3";
}
If the number of lines in the input is not a multiple of three, the extra lines are omitted. The code does not handle each input file separately; it just combines them all together. For the given input data, the output is:
Pin|add jtyjg Kolk|aaa||
Kawr|wht u Disnce Djhdb|bbb||
This seems to be correct.
Upvotes: 1