Domanda

I have an input file like this:

Apr 24 2014;
is;
a;
sample;
;
Jun 24 2014 123;
may 25 2014;
is;
b;
sample;
;
Dec 21 2014 987

... and I want output like this:

Apr 24 2014;is;a;sample;;Jun 24 2014 123
may 25 2014;is;b;sample;;Dec 21 2014 987

(I'm using ksh)

È stato utile?

Soluzione

For a data file containing following content:

$ cat file
Apr 24 2014;
is;
a;
sample;
;
Jun 24 2014 123
may 25 2014;
is;
b;
sample;
;
Dec 21 2014 987

You can do:

$ awk '{ORS=(NR%6?"":RS)}1' file
Apr 24 2014;is;a;sample;;Jun 24 2014 123
may 25 2014;is;b;sample;;Dec 21 2014 987

We set the Output Field Separator to print a new line (default value of RS) after every 6th record.

You can redirect the output to another file by saying

awk '{ORS=(NR%6?"":RS)}1' file > newfile

or do the following to make changes to current file

awk '{ORS=(NR%6?"":RS)}1' file > tmp && mv tmp file

Altri suggerimenti

Although the title suggests a strict awk solution, there is also a ksh tag and since it appears to be the only thing the program should do, perhaps this would do too:

paste -d'\0' - - - - - - < file

Just like with the awk version, there is still a trailing semicolon in the first line, that is not present in the output sample. I do not know if that would be a problem?

perl -lne 'undef $a if($.%7==0);
           $a.=$_;
           if($.%6==0 ||(eof))
           {$a=~s/\;$//;print $a};' Your_file

Tested here

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top