Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

sed to insert a character

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 02-06-2013
Chubler_XL's Unix or Linux Image
Chubler_XL Chubler_XL is offline Forum Staff  
Moderator
 
Join Date: Oct 2010
Last Activity: 26 May 2015, 11:42 PM EDT
Posts: 3,171
Thanks: 121
Thanked 1,050 Times in 983 Posts
Use '&' to insert matched string:


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

Sponsored Links
    #3  
Old Unix and Linux 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:

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

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 01:54 PM.