View the Most Wanted LQ Wiki articles.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 04-08-2013, 04:59 AM   #1
LQ Newbie
Registered: Apr 2013
Posts: 2

Rep: Reputation: Disabled
Replace all string in a text file starting with 12345 and total length is 17

Dear All,

I have a text file in linux which contains text along with some string of numbers starting from 12345 and total length of each is 17. I want to replace all such strings with 12345############. Can anyone let me know some way to do it.

Date will be like this.

abcdef NO: 12345098675643135 test string

I have one command:
sed -e 's/^12345...........$/12345###########/g' < input > output
This is only replacing the strings if there is only the numeric strings in each line of file as mentioned below.


Your urgent help will be highly appreciated.

Last edited by sakamboh; 04-08-2013 at 05:00 AM. Reason: edited the code
Old 04-08-2013, 05:49 AM   #2
Registered: Mar 2013
Posts: 612

Rep: Reputation: 135Reputation: 135
You are using ^string-to-be-replaced$, it means a line starts with the string and ends when string ends. If your line contains more than one string, it will not be replaced. Suppose your line contains:
1234526589745862 any-text-here 12345652145874589 12345254121269856 some-more text
Your code will not replace this, because this line starts with your string pattern but not end with it.
Do not use ^$ in your code, it means it will replace a string which starts with 12345 and it has 17 characters and it is the only string in the line.
Ex- a line contains

That's it. No more character before it and no more after it.

sed 's/12345.........../12345###########/g' /path/of/your/input/file
If you want to change in the file direct, use sed -i but it is dangerous first take back up and then change directly.

Last edited by eklavya; 04-08-2013 at 05:52 AM.
1 members found this post helpful.
Old 04-08-2013, 06:14 AM   #3
LQ Newbie
Registered: Apr 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks alot bro, this solved my problem and cleared my understanding as well.
Old 04-08-2013, 06:15 AM   #4
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381
You might want to make the search string more restrictive.

This 12345........... looks for 12345 followed by any character. So a string like this: 12345abcdefghijkl wills also be changed.

Have a look at this:
sed -r 's/12345[0-9]{12}/12345############/g' infile
Thsi looks for a string that starts with 12345 followed by 12 numbers (the [0-9]{12} part).
2 members found this post helpful.
Old 04-08-2013, 07:52 AM   #5
Registered: Mar 2013
Location: the India
Distribution: Ubuntu,Debian and CentOS
Posts: 35

Rep: Reputation: 2


here u found more about sed ...

Last edited by gangadhar402; 04-08-2013 at 07:54 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace a string in a text file jpan Linux - General 3 10-14-2012 06:17 PM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 11:05 AM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
How to replace string containing / in a text file tikit Linux - Newbie 4 09-05-2008 08:48 AM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 09:33 PM

All times are GMT -5. The time now is 01:45 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration