Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tlug] line filtering (was Re: Unidentified subject!)




On 8/7/2002, "big0" <big0@example.com> wrote:
>>I have a file that contains lines like this:
>
>>1 some
>>text with
>>C/R's
>>in annoying places
>>2 more text
>>with CRs
>>3 text text text
>>4 text text
>
>>I'd like to concat the lines that don't have leading numbers in to one
long line
>
>Use gawk or mawk, because awk have some limitations:
>
> ------------->8-----------------
>
>gawk -F" " 'BEGIN { numexp = "[[:digit:]]+"; tmp="" }
>NR > 0 {
> if ($1 ~ numexp && tmp != "") { print tmp; tmp="" }
>}
>{ tmp = tmp" "$0; }'
>
> ------------->8-----------------

Oh, that's gross. And it's also wrong. (Try it! It doesn't work.)

Here's a better one.

------------->8-----------------
perl -ne 'chomp; print unless /^\d/;'
------------->8-----------------

If you want a separator between each line, do this instead:

------------->8-----------------
perl -ne 'chomp; print $_, " " unless /^\d/;'
------------->8-----------------

If that extra space at the end really bothers you, you can try this: 

------------->8-----------------
perl -e 'print join(" ", map {chomp;$_} grep(!/^\d/,<>));'
------------->8-----------------

That one keeps the whole file in memory, though. If you have a really
big file and you still want that separator, try this instead:

------------->8-----------------
perl -e 'while(<>) {
    unless (/^\d/) {
        if ($needspace > 0) { print " " } else { $needspace = 1 }
        chomp; print $_;
    }
}'
------------->8-----------------

Shimpei.


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links