Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by shadowsong (Pilgrim)
on Mar 24, 2016 at 15:29 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1158725]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-18 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found