adding blank lines to text file 
Author Message
 adding blank lines to text file

I'd like to add 1 blank line above each URL in the following text file:

FooBar http://www.*-*-*.com/
For all of your foo needs
Google http://www.*-*-*.com/
World's best search engine - really, really, really
great
Yahoo http://www.*-*-*.com/
Google Groups
etc...

Can anyone please tell me how this can be accomplished using awk?



Wed, 22 Dec 2004 07:38:06 GMT  
 adding blank lines to text file


X I'd like to add 1 blank line above each URL in the following text file:
X
X FooBar http://www.foobar.com/
X For all of your foo needs
X Google http://www.google.com/
X World's best search engine - really, really, really
X great
X Yahoo http://groups.google.com/
X Google Groups
X etc...
X
X Can anyone please tell me how this can be accomplished using awk?

awk '
    match($0,"http://") { print "" }
    match($0,"ftp://")  { print "" }
    { print }
' input.file

You can enhance the Regular Expression to detect URLs besides the ones
that begin with http://.  You could add additional match lines for
ftp://, or match($0,"<[^.]+\.[^.]+\..*>") to get <www.xyz.***> type
notation.

                                        Bob Harris



Wed, 22 Dec 2004 08:22:55 GMT  
 adding blank lines to text file

Quote:
> awk '
>     match($0,"http://") { print "" }
>     match($0,"ftp://")  { print "" }
>     { print }
> ' input.file

Many thanks, Bob! Just what I was looking for.

Yours Gratefully,

John



Wed, 22 Dec 2004 09:00:19 GMT  
 adding blank lines to text file

Quote:


> X I'd like to add 1 blank line above each URL in the following text file:
> X
> X FooBar http://www.foobar.com/
> X For all of your foo needs
> X Google http://www.google.com/
> X World's best search engine - really, really, really
> X great
> X Yahoo http://groups.google.com/
> X Google Groups
> X etc...
> X
> X Can anyone please tell me how this can be accomplished using awk?

> awk '
>     match($0,"http://") { print "" }
>     match($0,"ftp://")  { print "" }
>     { print }
> ' input.file

> You can enhance the Regular Expression to detect URLs besides the ones
> that begin with http://.  You could add additional match lines for
> ftp://, or match($0,"<[^.]+\.[^.]+\..*>") to get <www.xyz.***> type
> notation.

>                                         Bob Harris

A little late, but...

Awk does support patterns in place of match($0,re).  match() is often
reserved for instances where RSTART and RLENGTH (set as side effects) are
used.  Otherwise, for non-$0 matches, the "~" operator is more compact.

More tersely:

    awk '
        /http:\/\/|ftp:\/\// { print "" }
        { print }
    ' input.file

Also, "print $0", or more succinctly, "print", is the default action of a
"true" pattern.  If you eliminate the line break in the awk source code, and
add such a true pattern (say, the expression "1"), you can get to:

    awk '/http:\/\/|ftp:\/\// { print "" } 1' input.file

Of course, this obfuscation only gets you tersity, and questions from those
that come after you.

One last trick is to use an uninitialized variable in place of the blank
string.  In gawk (and maybe other awks), the underscore character is a
convenient, but odd-enough looking, valid variable name.

    awk '/http:\/\/|ftp:\/\// { print _ } 1' input.file

The only benefit of this obfuscation is that it can eliminate internal
quotes in some cases, which is only really useful on Windows platforms that
use (") as their only quote.  And, of course, it gets you some
attention--your coworkers will always be coming to you--"How does this
work?"

    - Dan

input.file
----------
    FooBar http://www.foobar.com/
    For all of your foo needs
    Google http://www.google.com/
    World's best search engine - really, really, really
    great
    Yahoo http://groups.google.com/
    Google Groups

(output) - Win98, gawk
----------
    >awk "/http:\/\/|ftp:\/\// { print _ } 1" input.file

    FooBar http://www.foobar.com/
    For all of your foo needs

    Google http://www.google.com/
    World's best search engine - really, really, really
    great

    Yahoo http://groups.google.com/
    Google Groups



Fri, 24 Dec 2004 04:44:26 GMT  
 adding blank lines to text file

Quote:
> > awk '
> >     match($0,"http://") { print "" }
> >     match($0,"ftp://")  { print "" }
> >     { print }
> > ' input.file
> More tersely:

>     awk '
>         /http:\/\/|ftp:\/\// { print "" }
>         { print }
>     ' input.file

Thanks very much for the reply, Dan. It is always
a pleasure to learn different approaches to a
problem.

Yours Gratefully,

John



Mon, 27 Dec 2004 16:18:46 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How do I read lines of Text from Text File and add them to Array

2. Detecting blank lines in text file read

3. Replacing blank lines with text.

4. Regexp - split text by blank line.

5. Text Widget: removing blank lines

6. inserting a blank line in a file

7. tell me how to read input from file with comments and blank line in it

8. How to read blank lines in a file?

9. blank line at top of file?

10. Want to remove multiple occurances of blank line from a file

11. Reading a file containing blank lines

12. how to read text files line by line?

 

 
Powered by phpBB® Forum Software