- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: insert text before first line
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2004 01:43 PM
07-14-2004 01:43 PM
I am working on Sun OS. I need to enter a text before the first line in a file. How would i accomplish the task using sed.
Thanks,
Andy
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2004 02:04 PM
07-14-2004 02:04 PM
Re: insert text before first line
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2004 02:29 PM
07-14-2004 02:29 PM
Re: insert text before first line
Isn't it a little more tricky to get the newline there?
sed -e '1i\^Jtest^J' file > newfile
In this example the ^J stands for a linefeed.
You can enter this on the command line with a control-V followed by control-J
Alternative using replace instead of insert:
sed '1s/^/test\^J/' file > newfile
Again ^J is entered as control-v followed by control-j and a \ is needed to escape that.
You may want to check out the "Handy one-liners for SED" file. For example the one attached to:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=630972
hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2004 02:37 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2004 02:51 PM
07-14-2004 02:51 PM
Re: insert text before first line
I forgot to put in an obligatory perl alternative:
perl -e '@_=<>; print "test\n",@_' x
Here we tell perl to 'slurp' the input into an array @_, print the new line, and burp the array out again. This may be useful for more elaborated problems.
And just with the shell..
echo "test" > newfile; cat file >> newfile
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2004 03:16 PM
07-14-2004 03:16 PM
Re: insert text before first line
[too bad we can not edit replies]
before someone else comments, yes I know 'slurpling' in perl is not efficient and does not scale well for large files.
Same goes for sed.
The more efficient processing for large files is just read and write in a loop.
perl -pe '$i++||print "test\n"' file > newfile
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2004 04:43 PM
07-14-2004 04:43 PM
Re: insert text before first line
We can do it with sed as,
cat /input file/ | sed -e '1 s/^/
/g' > netoutput file
or we can simply do it without sed using temporary files as,
cat input-file /tmp/testfile.log
echo "
cat /tmp/testfile.log >> inputfile
Regards,
Muthukumar.