开发者

Concatenating multiple lines with a discriminator

I have the input like this

Input:

a,b,c
d,e,f
g开发者_JAVA百科,h,i
k,l,m
n,o,p
q,r,s

I wan to be able to concatenate the lines with a discriminator like "|"

Output:

a,b,c|d,e,f|g,h,i 
k,l,m|n,o.p|q,r,s

The file has 1million lines and I want to be able to concatenate lines like the example before.

Any ideas about how to approach this?


@OP, if you want to group them for every 3 records,

$ awk 'ORS=(NR%3==0)?"\n":"|"' file
a,b,c|d,e,f|g,h,i
k,l,m|n,o,p|q,r,s

with Perl,

$ perl -lne 'print $_ if $\ = ($. % 3 == 0) ? "\n" : "|"' file
a,b,c|d,e,f|g,h,i
k,l,m|n,o,p|q,r,s


Since your tags include sed here is a way to use it:

sed 'N;N;s/\n/|/g' datafile


gawk:

BEGIN {
  state=0
}

state==0 {
  line=$0
  state=1
  next
}

state==1 {
  line=line "|" $0
  state=2
  next
}

state==2 {
  print line "|" $0
  state=0
  next
}


If Perl is fine, you can try:

$i = 1;
while(<>) {
        chomp;
        unless($i % 3)
        { print "$line\n"; $i = 1; $line = "";}
        $line .= "$_|";
        $i++;
}

to run:

perl perlfile.pl 1millionlinesfile.txt


$ paste -sd'|' input | sed -re 's/([^|]+\|[^|]+\|[^|]+)\|/\1\n/g'

With paste, we join the lines together, and then sed dices them up. The pattern grabs runs of 3 pipe-terminated fields and replaces their respective final pipes with newlines.

With Perl:

#! /usr/bin/perl -ln

push @a => $_;
if (@a == 3) {
  print join "|" => @a;
  @a = ();
}

END { print join "|" => @a if @a }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜