sed optimized for replace substring


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed optimized for replace substring
# 1  
Old 10-25-2012
sed optimized for replace substring

Hi All,
I have this text file

Code:
 IR7013903069171012INT10171211864
 Axxxxxxxxxx12345dddddddddddd
 Byyyyyyyyy99999999ddddddddd
 IR7013903069171012TOS10171211865
 Cffffffffff5745747ehdefffffffffgggggggg
 Dyyyyyyyyy99999999ddddddddd

I need to
1) find lines where the substring IR70139 is present
2) only in the lines selected by the point 1, I need to replace the substring INT and TOS by SFD

I have used this command

Code:
cat <input filename>| grep IR70139 | sed s/INT/SFD/  > output_file

Is there a better command line to execute my command?

For example
Code:
sed /search string1/,/search string2/ {  s/INT/SFD/  s/TOS/SFD/ } < inputfile > outputfile

I have tried but without success.

Any help will be well appreciated.

Thanks in advance for your kind support.

Regards,

Giovanni

Moderator's Comments:
Mod Comment Please use code tags for code and data, thanks

Last edited by Scrutinizer; 10-25-2012 at 08:51 AM.. Reason: code tags
# 2  
Old 10-25-2012
Try:
Code:
sed '/IR70139/{s/INT/SFD/g; s/TOS/SFD/g;}' infile

# 3  
Old 10-25-2012
Try like...

Code:
awk '/^IR70139/' test.txt|sed 's/INT/SFD/g;s/TOS/SFD/g'

# 4  
Old 10-25-2012
Quote:
Originally Posted by bmk
Try like...

Code:
awk '/^IR70139/' test.txt|sed 's/INT/SFD/g;s/TOS/SFD/g'


Why awk when a single command with sed would have taken care of the same condition you have used in awkSmilie...like scrutinizer has put across
# 5  
Old 10-25-2012
Per my knowledge i posted.. scrutinizer solution is better compared to me..
# 6  
Old 10-25-2012
Good!

Thank You very much indeed!

Regards,

Giovanni
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace substring by longest string in common field (awk)

Hi, Let's say I have a pipe-separated input like so: name_10|A|BCCC|cat_1 name_11|B|DE|cat_2 name_10|A|BC|cat_3 name_11|B|DEEEEEE|cat_4 Using awk, for records with common field 2, I am trying to replace all the shortest substrings by the longest string in field 3. In order to get the... (5 Replies)
Discussion started by: beca123456
5 Replies

2. Shell Programming and Scripting

Replace substring from a string variable

Hi, Wish to remove "DR-" from the string variable (var). var="DR-SERVER1" var=`echo $var | sed -e 's/DR-//g'` echo "$var" Expected Output: However, I get the below error: Can you please suggest. (4 Replies)
Discussion started by: mohtashims
4 Replies

3. Shell Programming and Scripting

Substring Problem with sed . . .

Greetings. I'm looking to isolate the first occurrence of an arbitrary substring which may be present at any particular line in a given file. The enclosing end markers for the target in our thought problem are string" and ". The complete string and surrounding text could look something like... (3 Replies)
Discussion started by: LinQ
3 Replies

4. Shell Programming and Scripting

Advanced AWK Regexp substring to int & Replace

Hi! I have a difficult problem, to step up a unknown version number in a text file, and save the file. It would be great to run script.sh and the version gets increased. Example the content of the textfile.txt hello version = x bye This include three steps 1. First find the char after... (2 Replies)
Discussion started by: Beachboy72
2 Replies

5. Shell Programming and Scripting

Making substring with sed

Input: You can easily do this by highlighting your code. How can i get the substring from index 9 to 12 using sed? (6 Replies)
Discussion started by: cola
6 Replies

6. Shell Programming and Scripting

Sed and SubString

Hi Folks, I am here with a simple doubt. I am having a flat file in which I want to replace the characters from say 5 to 15 as some text. Flat file contains a single line. For example 01MRRAJESH21000RAJESH INDUSTRIES In the above line pos 16-21 is Rajesh, I want to search for the... (4 Replies)
Discussion started by: dinesh1985
4 Replies

7. AIX

AIX Replace a substring

Hi, I have a very simple problem but I am new to scripting and I cannot find a way around it. I am using KSH. I have a date say 001200AM But i want to replace the first 00 to 12 so that the format will now be: 121200AM Can anyone help me? (2 Replies)
Discussion started by: 3vilwyatt
2 Replies

8. Shell Programming and Scripting

Substring using sed or awk

I am trying to get a substring from a string stored in a variable. I tried sed with a bit help from this forum, but not successful. Here is my problem. My string is: "REPLYFILE=myfile.txt" And I need: myfile.txt (everything after the = symbol). My string is: "myfile.txt.gz.20091120.enc... (5 Replies)
Discussion started by: jamjam10k
5 Replies

9. Shell Programming and Scripting

Using sed to get a substring

Hi, I have looked all over for this. I am attempting to get a the substring of a string using sed since it seemed the best solution for this. For example my string is: "zzz foo to you and bar123 or foo" I would like to extract the text between "and" and "or" (it could be anything, but... (2 Replies)
Discussion started by: CentaurAtlas
2 Replies

10. Shell Programming and Scripting

Sed extract substring on (OS X)

On OS 10.4.11 I have filenames like: 670711 SA T2 v1-1_DS_EF.doc CT_670520 AM T1 v1-2_DS_EF.doc CT_670716 - 2 SA T4 v1-2_DS_EF.doc CT_670713 SA T3 v1-1_DS_EF.doc 670421 PA DYP1 v1-1_DS_EF.doc CT_670425 PA DYP2 v1-1_DS_EF.doc CT_670107 RA T3 v1-2_DS_EF.doc CT_670521 AM T2 v1-2_DS_EF.doc... (3 Replies)
Discussion started by: mlommel
3 Replies
Login or Register to Ask a Question