[TriLUG] vi or sed or perl question - joining lines

Craig Taylor ctalkobt at gmail.com
Fri Jun 1 16:53:08 EDT 2007


[ Sorry for the quick follow up to my own... ]

Optimized and simplified, try:

cat tmp.txt | awk '{ if (NR%12) printf("%s",$0); else printf("%s\n",$0); }'

This shouldn't have affected your results but the initial posting by me was
assuming that awk was reading the linefeed in as part of $0 when it wasn't
hence the additional length+1 value.

This should work for all ASCII files, binary files may need to look at the
awk man page for specification on how to set the line seperator.

On 6/1/07, Craig Taylor <ctalkobt at gmail.com> wrote:
>
> For the awk script,
> I'd be curious to know the type of data you're feeding into it. I tested
> it with a file containing the numbers 1 to 25 each on their own line.
>
> This wouldn't happen to be binary data would it?
>
> On 6/1/07, Mike Norwood <norwoodm at earthlink.net> wrote:
> >
> >
> >
> > Thanks for all the different ideas.  I tried the awk first (it looked
> > shortest) but something is not working right, it appears to be giving me
> > the right number of records, but is leaving out some of the data.  The
> > vi
> > would work but there are over 20000 lines which should combine to around
> > 1800 records, so it will take too long.  So, about the perl, would I
> > just
> > take what you have below into a script, then execute it with "scriptname
> >
> > myfile" > out.file?
> >
> > Or is there any easy way to get vi to repeat the 12J command 1800 times?
> >
> > Thanks again for the suggestions, Mike
> >
> > On Fri, 1 Jun 2007, Andrew Perrin wrote:
> >
> > > Boy, that's icky. I would probably do something like this:
> > >
> > > #!/usr/bin/perl
> > >
> > > my $line;
> > > my $i=1;
> > > while (<>) {
> > >       chomp;
> > >       $line .= $_;
> > >       $i++;
> > >       if ($i == 12) {
> > >               print "$line\n";
> > >               $i = 0;
> > >               $line = "";
> > >       }
> > > }
> > > print "$line\n" if $line;
> > >
> > > ----------------------------------------------------------------------
> >
> > > Andrew J Perrin - andrew_perrin (at) unc.edu -
> > http://perrin.socsci.unc.edu
> > > Assistant Professor of Sociology; Book Review Editor, _Social Forces_
> > > University of North Carolina - CB#3210, Chapel Hill, NC 27599-3210 USA
> > > New Book: http://www.press.uchicago.edu/cgi-bin/hfs.cgi/00/178592.ctl
> > >
> > >
> > >
> > > On Fri, 1 Jun 2007, Mike Norwood wrote:
> > >
> > > >
> > > > Hello,
> > > >
> > > > I have a big goofy text file I need to get into a more usable
> > format.
> > > > Basically what I need to do is join every 12 lines into 1 line, down
> >
> > > > through the entire file.  Does anyone have a good suggestion on how
> > to do
> > > > this?  I know that I must have done this same thing 2 years ago,
> > because I
> > > > found an earlier version of this same data from May 2005 that I
> > somehow
> > > > converted, but I now have no idea how it was done.
> > > >
> > > > Thanks, Mike
> > > >
> > > > --
> > > > TriLUG mailing list        :
> > http://www.trilug.org/mailman/listinfo/trilug
> > > > TriLUG Organizational FAQ  : http://trilug.org/faq/
> > > > TriLUG Member Services FAQ : http://members.trilug.org/services_faq/
> > > >
> > >
> >
> > --
> > TriLUG mailing list        :
> > http://www.trilug.org/mailman/listinfo/trilug
> > TriLUG Organizational FAQ  : http://trilug.org/faq/
> > TriLUG Member Services FAQ : http://members.trilug.org/services_faq/
> >
>
>



More information about the TriLUG mailing list