substitute a string on a specific position for specific lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting substitute a string on a specific position for specific lines
# 1  
Old 12-13-2010
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 need to check between the 12th and the 19th digits.
No checks for line 1,3 and the last one.

------------------------
------------------------
example 1:
Code:
line 3 
12345678901TOCHANGE12345
Line 25,.... 
12345678TOCHANGE12345678
123456789TOCHANGE1234567
TOCHANGE1234567890123456

Only the line 3 and 25 need to be amended

So I would like to get
Code:
line 3 
12345678901ABCABCAB12345
Line 25,...
12345678ABCABCAB12345678
123456789TOCHANGE1234567
TOCHANGE1234567890123456

-------------------------
-------------------------
example 2:
Code:
line 3 
123456789TOCHANGE1234567
Line 25,.... 
12345678TOCHANGE12345678
123456789TOCHANGE1234567
TOCHANGE1234567890123456

Only the line 25 needs to be amended

So I would like to get
Code:
Line 3
123456789TOCHANGE1234567 
Line 25,...
12345678ABCABCAB12345678
123456789TOCHANGE1234567
TOCHANGE1234567890123456

Do you have any idea about how I could do it?

I tried with the sed command but it works on the number of occurence.
I think it could be done via the awk command

Many thanks



Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples!

Last edited by Franklin52; 12-29-2010 at 09:19 AM..
# 2  
Old 12-13-2010
I'm editing my post because I just re-read your post and saw that you are only looking at specific locations on each line. Also, I see that you already tried sed. I believe awk will accept a period for a single character. You could try using a while loop to read each line (while line=$(line)) and checking for a successful status on an awk command like: awk '/^........TOCHANGE/' $line. If that is successful, you could then use a sed (newline = `sed -e 's/TOCHANGE/ABCABCAB' $line`).

My apologies for the confusion, but I hope it helps. By the way, I didn't have a file like this to try. So, these are untried suggestions.

Last edited by ducet8; 12-13-2010 at 01:31 PM.. Reason: wrong information
ducet8
# 3  
Old 12-15-2010
Hi

Thanks for this.
But I was wondering whether or not it could be possible to do it without a loop.
I'd like to use an 'awk' or a 'sed'.
My first thought was to use a sed but it works by occurence.

Do you have any other idea?

Cheers,

Bernard
# 4  
Old 12-15-2010
Code:
# cat tst
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345

Code:
# sed '4,'"$(( $(wc -l <tst)-1 ))"'s/TOCHANGE/XXXXXXXX/' tst
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901XXXXXXXX12345
12345678901XXXXXXXX12345
12345678901XXXXXXXX12345
12345678901XXXXXXXX12345
12345678901XXXXXXXX12345
12345678901XXXXXXXX12345
12345678901TOCHANGE12345


Last edited by ctsgnb; 12-15-2010 at 10:45 AM..
# 5  
Old 12-15-2010
@ctsgnb
Good way to get 2nd last line no but I guess above script needs to be modified as below:

Ignore line 1,2 and the last one (No change)
For the 3rd line, Replace only if TOCHANGE is in 12th to 19th position.
For other lines (4th - 2nd last), Replace only if TOCHANGE is in 9th to 16th position.
Code:
 
sed -e '3s/\(...........\)TOCHANGE\(.*\)/\1ABCABCAB\2/' -e '4,'"$(( $(wc -l < inputFile)-1 ))"'s/\(........\)TOCHANGE\(.*\)/\1ABCABCAB\2/' inputFile


Last edited by anurag.singh; 12-15-2010 at 10:31 AM..
# 6  
Old 12-15-2010
@anurag

Ooops, you are true, i totally forgot that position case ... here is an even better
- need caret ^ to be exact regarding the position otherwise the \(.....<n_times>\)TOCHANGE could also be match at a position greater than the expected one
- use the $! tip to avoid last line)
- therefore avoid the use of additionnal file opening & line counting of the wc -l
- no need of sed -e option

Code:
sed '3s/^\(...........\)TOCHANGE\(.*\)/\1ABCABCAB\2/;$!{4,$s/^\(........\)TOCHANGE\(.*\)/\1ABCABCAB\2/}' infile

Code:
# cat tst1
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345
# cat tst2
12345678TOCHANGE12345
12345678TOCHANGE12345
12345678TOCHANGE12345
12345678TOCHANGE12345
12345678TOCHANGE12345
12345678TOCHANGE12345
12345678TOCHANGE12345
# sed '3s/^\(...........\)TOCHANGE\(.*\)/\1ABCABCAB\2/;$!{4,$s/^\(........\)TOCHANGE\(.*\)/\1ABCABCAB\2/}' tst1
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901ABCABCAB12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345
12345678901TOCHANGE12345
# sed '3s/^\(...........\)TOCHANGE\(.*\)/\1ABCABCAB\2/;$!{4,$s/^\(........\)TOCHANGE\(.*\)/\1ABCABCAB\2/}' tst2
12345678TOCHANGE12345
12345678TOCHANGE12345
12345678TOCHANGE12345
12345678ABCABCAB12345
12345678ABCABCAB12345
12345678ABCABCAB12345
12345678TOCHANGE12345


Last edited by ctsgnb; 12-15-2010 at 11:52 AM.. Reason: forgot the caret ^
# 7  
Old 12-15-2010
@ctsgnb, Yes. I missed caret ^ and last one is much better. Tks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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: \E I want to replace the escape code at the 3rd line, 2nd column with this escape code... (3 Replies)
Discussion started by: tinman47
3 Replies

2. 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

3. 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

4. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (1 Reply)
Discussion started by: manaswinig
1 Replies

5. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (2 Replies)
Discussion started by: manaswinig
2 Replies

6. Shell Programming and Scripting

search a line and insert string into specific at position

Hi, guys. I have one question: How can I search for a line with certain string in it and then insert a string into this line? For example: There is a file called shadow, the contents of it are below: ************************** ... yuanz:VIRADxMsadfDF/Q:0:0:50:7:::... (9 Replies)
Discussion started by: daikeyang
9 Replies

7. Shell Programming and Scripting

Substitute specific lines with lines from another file

Hello All, I am new to this forum. I am currently facing a problem in manipulating files. I have two files called old-matter and new-matter # cat old-matter abc: this, is a, sample, entry byi: white board, is white in color rtz: black, board is black qty: i tried, a lot asd: no... (1 Reply)
Discussion started by: rahmathulla
1 Replies

8. Shell Programming and Scripting

check position of end of line for some specific lines

-------------------------------------------------------------------------------- Have to check in a file that the lines starting with 620 and 705 are ending at same posiotin. 82012345 62023232323 70523949558 62023255454 9999 In the above lines, i have to check the lines starting... (1 Reply)
Discussion started by: senthil_is
1 Replies

9. Shell Programming and Scripting

Print lines with search string at specific position

Hi Folks, I have a file with all fields defined by byte position, but any field can be empty so I cannot print lines based on a search of specific columns. I need to print all lines of this file where the string of two characters at byte position 100-101 contains the number 27. Any ideas? ... (4 Replies)
Discussion started by: HealthyGuy
4 Replies

10. Shell Programming and Scripting

How to add character in specific position of a string?

Hi All, I would like to use sed to add "-" between the following string: Value: 20060830 Result: 2006-08-30 Pls advice. Thx a lot Victor (5 Replies)
Discussion started by: victorlung
5 Replies
Login or Register to Ask a Question