Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-06-2013
Registered User
 
Join Date: Apr 2012
Location: geelong, australia
Posts: 24
Thanks: 2
Thanked 0 Times in 0 Posts
sed to insert a character

Hi all,

I have a file like this

Code:
Q8N302
21-84
Q8N157
15-45
Q99996
167-201
202-251
269-318

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

Code:
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


Code:
Q8N302
#1-84
Q8N157
#5-45
Q99996
#67-201
#02-251
#69-318
#20-321
#22-323

Sponsored Links
    #2  
Old 02-06-2013
Chubler_XL's Avatar
Registered User
 
Join Date: Oct 2010
Posts: 2,125
Thanks: 66
Thanked 615 Times in 593 Posts
Use '&' to insert matched string:


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

Sponsored Links
    #3  
Old 02-07-2013
Registered User
 
Join Date: Sep 2012
Location: Houston, Texas, USA
Posts: 571
Thanks: 0
Thanked 174 Times in 168 Posts
try also:

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

Sponsored Links
Reply

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 08: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 08:39 PM
Insert character in the line swat UNIX for Dummies Questions & Answers 1 06-30-2009 11:12 AM
How to insert a character in line in Special condition? bhaskar_m Shell Programming and Scripting 8 04-24-2009 08:45 AM
How can I insert character to end of file? umen UNIX for Dummies Questions & Answers 1 01-29-2008 06:47 AM



All times are GMT -4. The time now is 04:15 PM.