- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to insert a string to the head of every line o...
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
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
04-19-2006 07:02 AM
04-19-2006 07:02 AM
How can I insert such column to the file?
Should I use "sed"?
Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2006 07:17 AM
04-19-2006 07:17 AM
Re: How to insert a string to the head of every line of a file
#!/usr/bin/sh
typeset COMMENT=${1}
shift
typeset X=""
while read X
do
echo "${COMMENT} ${X}
done
exit 0
Use it like this:
clay.sh "error:" < infile > outfile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2006 07:33 AM
04-19-2006 07:33 AM
Re: How to insert a string to the head of every line of a file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2006 07:43 AM
04-19-2006 07:43 AM
Re: How to insert a string to the head of every line of a file
awk '{print "error: ",$0}' < inputfile > outputfile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2006 07:43 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2006 07:47 AM
04-19-2006 07:47 AM
Re: How to insert a string to the head of every line of a file
Now execute it as a one-limer:
my.sh "This is a comment" < infile > outfile
NOTE: I cannot believe I have to explain this!
----------------------------------------
Another one-liner approach using awk:
awk '{ print "error:", $0 }' < infile > outfile
It's also possible with Perl or sed but I've done enough.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2006 08:25 AM
04-19-2006 08:25 AM
Re: How to insert a string to the head of every line of a file
How can I use a variable to replace "Comment" in your "sed" command?
sed 's/^/Comment: /' file
Because different line should have different header.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2006 08:32 AM
04-19-2006 08:32 AM
Re: How to insert a string to the head of every line of a file
sed "s/^/${DUMMY}/" testfile
Jeff Traigle
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2006 08:38 AM
04-19-2006 08:38 AM
Re: How to insert a string to the head of every line of a file
sed 's/^/'$Comment': /' file
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2006 10:17 AM
04-19-2006 10:17 AM
Re: How to insert a string to the head of every line of a file
# awk '{str="error:";if(NR==1)printf("%s\n%s\n",str,$0);else print $0}' inp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2006 10:24 AM
04-19-2006 10:24 AM
Re: How to insert a string to the head of every line of a file
Misunderstood your post, thought you wanted a string inserted on the top of every file...and here's the sed construct for that:
# sed 's/\(.*\)/error: \1/p' inp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2006 08:14 PM
04-19-2006 08:14 PM
Re: How to insert a string to the head of every line of a file
string="error:"
awk -v S=$string '{print S $0}' infile >outfile
HTH,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2006 08:30 PM
04-19-2006 08:30 PM