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
    #1  
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

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
Chubler_XL Chubler_XL is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 21 September 2014, 6:57 PM EDT
Posts: 2,869
Thanks: 107
Thanked 908 Times in 849 Posts
Use '&' to insert matched string:


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

Sponsored Links
    #3  
Old 02-07-2013
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 15 July 2014, 11:14 AM EDT
Location: Houston, Texas, USA
Posts: 675
Thanks: 0
Thanked 203 Times in 195 Posts
try also:

Code:
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 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 12:12 PM.