Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Replace first blank line in a text file with headers.

by shadowsong (Monk)
on Mar 24, 2016 at 15:29 UTC ( #1158725=note: print w/replies, xml ) Need Help??


in reply to Replace first blank line in a text file with headers.

If you find that the only place your text file could possibly have an empty line and you don't mind changing the file in place, feel free to use this one-liner (where filename.txt is the name of your text file):

perl -i.tmp -lpe "s/^\s*$/Name,City,State,Zip/g" filename.txt

Cheers,
Shadowsong

Replies are listed 'Best First'.
Re^2: Replace first blank line in a text file with headers.
by FreeBeerReekingMonk (Deacon) on Mar 24, 2016 at 17:49 UTC
    You can always change it ONLY on the first line, with linenumber $.

    perl -pi -le 's/^\s*$/Name, City, State, Zip/ if $.==1' filename.txt

    edit: Sorry, forgot the -l (without it, the line is replace-inserted without newline, and the next line with data does not start at the beginning of the next line)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1158725]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2019-05-26 17:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (153 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!