Re: [gentoo-user] Re: script help - removing newlines

2021-12-08 Thread Adam Carter
>
> Hmm, maybe:
>
> $ awk '/^comment "[^"]*$/ { ORS=" " } /"$/ { ORS="\n" } { print }'
> yourfile.txt
>

Yes that works (after piping the file thru tr -d '\r' first). Thanks!


[gentoo-user] Re: script help - removing newlines

2021-12-08 Thread Tavis Ormandy
On 2021-12-08, Adam Carter wrote:
>
> I want to replace any newlines between 'comment "' and the next '"' with
> spaces so the whole comment is on a single line. How can it be done?


Hmm, maybe:

$ awk '/^comment "[^"]*$/ { ORS=" " } /"$/ { ORS="\n" } { print }' yourfile.txt

That means if a line starts with 'comment "' but doesn't end with ",
change the ORS (output record seperator) to a space. If it does end with
a ", change it to a newline.

Tavis.

-- 
 _o)$ lynx lock.cmpxchg8b.com
 /\\  _o)  _o)  $ finger tav...@sdf.org
_\_V _( ) _( )  @taviso




Re: [gentoo-user] script help - removing newlines

2021-12-08 Thread Michael Orlitzky
On 2021-12-08 17:15:43, Adam Carter wrote:
> 
> but sometimes there are newline characters in the comment field;
> 
> property "something"
> 
> comment "something
> 
> something else
> 
> a third thing"
> 
> I want to replace any newlines between 'comment "' and the next '"' with
> spaces so the whole comment is on a single line. How can it be done?

It depends on how complicated the format of your file can be. For the
one example shown, you could write a python script that looks for,

  comment "

at the beginning of a line, and then scans forward one character at a
time, looking for the ending quotation mark, but deleting any newlines
it finds along the way. Things like this work great until they meet
the real world:

  1. Are you sure there's always exactly one space between the word
 'comment' and the quotation mark?

  2. Can there be space before the word 'comment'?

  3. Are you sure nobody is using single quotes instead of double quotes?
 How about the fancy non-ascii quotes that you get sometimes when
 copy/pasting from a webpage or a Word document?

  4. What happens if a comment contains double-quotes?

etc. If you're the one creating the data or if you're sure that the
format will be exactly what you say it is, then you can get away with
a simple script. (Otherwise, the answer is basically "write a parser.")