Using sed to replace a string in a specific position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed to replace a string in a specific position
# 1  
Old 09-28-2011
Using sed to replace a string in a specific position

I asked this before, but my problem got more complicated. Heres what I am trying to do:

I'm trying to replace a string at a certain location with another string.

Heres the file I'm trying to change:

Code:
\E[0;40m \E[0;40m \E[0;40m \E[0;40m 
\E[0;40m \E[0;40m \E[0;40m \E[0;40m 
\E[0;40m \E[0;40m \E[0;40m \E[0;40m 
\E[0;40m \E[0;40m \E[0;40m \E[0;40m 
\E[0;40m \E[0;40m \E[0;40m \E[0;40m

I want to replace the escape code at the 3rd line, 2nd column with this escape code "\E[5;47m".

Heres what I tried:

Code:
sed "3 s/.;40m /5;47m /2" inputfile

The code above works, but heres the issue:

I want to be able to make sed replace any escape code with "\E[5;47m", but the sed program only detects the escape code "\E[0;40m". How can I make sed detect every escape code including "\E[0;40m"?

Like detect "\E[3;43m"? Does sed have an option like "3 s/.*;*m /5;47m /2", the "*" asterisk detects numbers 0-9?
# 2  
Old 09-28-2011
for any escape code at 3rd line, 2nd column..
Code:
# sed '3s|\E\[[0-9][0-9]*;[0-9][0-9]*m|E[5;47m|2' infile
\E[0;40m \E[0;40m \E[0;40m \E[0;40m
\E[0;40m \E[0;40m \E[0;40m \E[0;40m
\E[0;40m \E[5;47m \E[0;40m \E[0;40m
\E[0;40m \E[0;40m \E[0;40m \E[0;40m
\E[0;40m \E[0;40m \E[0;40m \E[0;40m

regards
ygemici
This User Gave Thanks to ygemici For This Post:
# 3  
Old 09-28-2011
Code:
sed '3s/[^ ]*/\\E[5;47m/2' infile

Code:
sed "3s/[^ ]*/\\\E[5;47m/2" infile

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 09-29-2011
This is the best forum of all time Smilie

Both solutions worked. Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

2. Shell Programming and Scripting

Find and replace with 0 for characters in a specific position

Need command for position based replace: I need a command to replace with 0 for characters in the positions 11 to 20 to all the lines starts with 6 in a file. For example the file ABC.txt has: abcdefghijklmnopqrstuvwxyz 6abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz... (4 Replies)
Discussion started by: thangabalu
4 Replies

3. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

4. Shell Programming and Scripting

position specific replace in file

How to replace the position specific values in the file.. i searched a lot the forums but i couldn't able to do... i have file like below 576666666666666666666666666 7878 897987 121 0asdas Y12 5900fbb 777 09JJJ 78798347892374 234234234364 234232898 89HJHIHIGIUG989902743748327khjkhkjlh... (6 Replies)
Discussion started by: greenworld123
6 Replies

5. Shell Programming and Scripting

Sed position specific replace

I'm drawing a blank on how to use sed to replace selectively based on position in the string (vs nth occurence): hello.|there.|how.|are.|you.| I want the period removed in the 3rd item (as defined by the pipe delimiter) if a period is present. So the result in this case would be: ... (2 Replies)
Discussion started by: tiggyboo
2 Replies

6. Shell Programming and Scripting

substitute a string on a specific position for specific lines

I woud like to substitue a string on a specific position for specific lines I've got a file and I would like to change a specific string from "TOCHANGE" to "ABCABCAB" For every line (except 1,2, 3 and the last one) , I need to check between the 9th and the 16th digits. For the 3rd line, I... (7 Replies)
Discussion started by: BSF
7 Replies

7. Shell Programming and Scripting

AWK or SED to add string at specific position

Greetings. I don't have experience programing scripts. I need to insert a string in a specific position of another string on another file (last.cfg), for example: File last.cfg before using script: login_interval=1800 lcs.machinename=client04 File last.cfg after using script:... (4 Replies)
Discussion started by: vanesuke
4 Replies

8. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

9. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

10. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies
Login or Register to Ask a Question