sed to insert a character | Unix Linux Forums | Shell Programming and Scripting

  Go Back    

Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

sed to insert a character

Shell Programming and Scripting

Closed Thread    
Thread Tools Search this Thread Display Modes
Old 02-06-2013
kaav06 kaav06 is offline
Registered User
Join Date: Apr 2012
Last Activity: 27 March 2014, 2:11 AM EDT
Location: geelong, australia
Posts: 38
Thanks: 3
Thanked 0 Times in 0 Posts
sed to insert a character

Hi all,

I have a file like this


I want to insert a character or space if the line starts with a number and I used the command

sed 's/^[0-9]/#/'

But in the output file, when it inserts this character, first digit in the number is deleted. How can I change the code so that I dont lose any digits. The output I am getting is given below


Sponsored Links
Old 02-06-2013
Chubler_XL's Avatar
Chubler_XL Chubler_XL is offline Forum Staff  
Join Date: Oct 2010
Last Activity: 29 January 2015, 9:57 PM EST
Posts: 3,093
Thanks: 117
Thanked 1,004 Times in 941 Posts
Use '&' to insert matched string:

sed 's/^[0-9]/#&/'

Sponsored Links
Old 02-07-2013
rdrtx1 rdrtx1 is offline
Registered User
Join Date: Sep 2012
Last Activity: 13 January 2015, 10:34 AM EST
Location: Houston, Texas, USA
Posts: 681
Thanks: 0
Thanked 206 Times in 198 Posts
try also:

sed '/^[0-9]/s/^/#/'

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
insert character in particular position. Jairaj Shell Programming and Scripting 7 12-02-2010 09:48 AM
In Sed how can I replace starting from the 7th character to the 15th character. mohullah Shell Programming and Scripting 5 09-22-2010 09:39 PM
Insert character in the line swat UNIX for Dummies Questions & Answers 1 06-30-2009 12:12 PM
How to insert a character in line in Special condition? bhaskar_m Shell Programming and Scripting 8 04-24-2009 09:45 AM
How can I insert character to end of file? umen UNIX for Dummies Questions & Answers 1 01-29-2008 07:47 AM

All times are GMT -4. The time now is 03:40 PM.