LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 04-08-2013, 05:59 AM   #1
sakamboh
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
12345097532577854

I have one command:
Code:
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.

12345098675643135
12345097532577854

Your urgent help will be highly appreciated.

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

Rep: Reputation: 131Reputation: 131
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:
Code:
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
12345254121269856

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


Code:
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 06:52 AM.
 
1 members found this post helpful.
Old 04-08-2013, 07:14 AM   #3
sakamboh
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, 07:15 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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:
Code:
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, 08:52 AM   #5
gangadhar402
Member
 
Registered: Mar 2013
Location: the India
Distribution: Ubuntu,Debian and CentOS
Posts: 35

Rep: Reputation: 2
Hi,



@sakamboh

here u found more about sed ...

http://www.grymoire.com/Unix/Sed.html

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


Reply


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 07: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 12:05 PM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 02:34 PM
How to replace string containing / in a text file tikit Linux - Newbie 4 09-05-2008 09:48 AM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 10:33 PM


All times are GMT -5. The time now is 08:47 AM.

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