CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
unix command for inserting multiple lines into beggining of file

 



sparsh
Novice

Sep 8, 2015, 9:39 AM

Post #1 of 3 (832 views)
unix command for inserting multiple lines into beggining of file Can't Post

Hi,

This isn't really a perl question, but rather a question on the command: sed.

I need to add 3 lines to the beginning of a file, using the sed command. I tried this and it didn't work:

sed -i "1i '/************'a \ '// ABCD'a\\'************/'" text.txt

I need the appended lines to show up as :

/***********
// ABCD
*************/

when i try the code above, I get:

'/************'a ' // ABCD'a'************/'

I know I need to use \a somehow, but not sure what the correct syntax for the command will be.


Laurent_R
Veteran / Moderator

Sep 9, 2015, 2:21 PM

Post #2 of 3 (824 views)
Re: [sparsh] unix command for inserting multiple lines into beggining of file [In reply to] Can't Post

I frankly would not do it in sed, not that sed is no good for that, but because I have given up using sed (and awk) ever since I have been starting to use Perl, which can do these things in an easier way and can do much more.

However, a quick try with sed.

My test_dups.txt file:

Code
aa blah 
bb blah
bb blahblah
bb foo
cc dlqskjf
cc cfkqs
...


The sed command:


Code
$ sed  -e '1ifoo\nbar\nbaz' test_dups.txt

which outputs:

Code
foo 
bar
baz
aa blah
bb blah
bb blahblah
bb foo
cc dlqskjf
cc cfkqs
...


So the three lines are added at the beginning of the file. This is what you wanted, didn't you?


(This post was edited by Laurent_R on Sep 9, 2015, 2:29 PM)


Laurent_R
Veteran / Moderator

Sep 9, 2015, 2:39 PM

Post #3 of 3 (820 views)
Re: [sparsh] unix command for inserting multiple lines into beggining of file [In reply to] Can't Post

And, BTW, I forgot to mention, this command:

Code
$ sed -ie '1ifoo\nbar\nbaz' test_dups.txt

will do in-place editing of the file, so that my test_dups.txt is now modified as:

Code
foo 
bar
baz
aa blah
bb blah
bb blahblah
bb foo
cc dlqskjf
cc cfkqs
...



(This post was edited by Laurent_R on Sep 9, 2015, 2:41 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives