sed replace nth characters with string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed replace nth characters with string
# 1  
Old 03-22-2015
sed replace nth characters with string

Hi,

I hope you can help me out please?

I need to replace from character 8-16 with AAAAAAAA and the rest should stay the same after character 16

Code:
gtwrhtrd11111111rjytwyejtyjejetjyetgeaEHT
wrehrhw22222222hytekutkyukrylryilruilrGEQTH
hrwjyety33333333gtrhwrjrgkreglqeriugn;RUGNEURGU

Code:
gtwrhtrdAAAAAAAArjytwyejtyjejetjyetgeaEHT
wrehrhwAAAAAAAAhytekutkyukrylryilruilrGEQTH
hrwjyetyAAAAAAAAgtrhwrjrgkreglqeriugn;RUGNEURGU

Thank you very much

Last edited by Don Cragun; 03-22-2015 at 04:58 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 03-22-2015
Any attempts from your side?
# 3  
Old 03-22-2015
Yes but adds the the string infront

Code:
sed -r 's/^(........)./\1AAAAAAAA/' test

result

gtwrhtrdAAAAAAAA1111111rjytwyejtyjejetjyetgeaEHT
# 4  
Old 03-22-2015
You need to give the search pattern something to replace. With BREs:
Code:
sed 's/\(.\{8\}\).\{8\}/\1AAAAAAAA/' file
gtwrhtrdAAAAAAAArjytwyejtyjejetjyetgeaEHT
wrehrhw2AAAAAAAAytekutkyukrylryilruilrGEQTH
hrwjyetyAAAAAAAAgtrhwrjrgkreglqeriugn;RUGNEURGU

If your sed handles EREs, try:
Code:
sed -E 's/(.{8}).{8}/\1AAAAAAAA/' file
gtwrhtrdAAAAAAAArjytwyejtyjejetjyetgeaEHT
wrehrhw2AAAAAAAAytekutkyukrylryilruilrGEQTH
hrwjyetyAAAAAAAAgtrhwrjrgkreglqeriugn;RUGNEURGU

These 2 Users Gave Thanks to RudiC For This Post:
# 5  
Old 03-22-2015
superb thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Outputting characters after a given string and reporting the characters in the row below --sed

I have this fastq file: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGGGGGGGGGGGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCA +test-1 GGGGGGGGGGGGGGGGGCCGGGGGFF,EDFFGEDFG,@DGGCGGEGGG7DCGGGF68CGFFFGGGG@CGDGFFDFEFEFF:30CGAFFDFEFF8CAF;;8... (10 Replies)
Discussion started by: Xterra
10 Replies

2. Shell Programming and Scripting

How to search and replace string from nth column from a file?

I wanted to search for a string and replace it with other string from nth column of a file which is comma seperated which I am able to do with below # For Comma seperated file without quotes awk 'BEGIN{OFS=FS=","}$"'"$ColumnNo"'"=="'"$PPK"'"{$"'"$ColumnNo"'"="'"$NPK"'"}{print}' ${FileName} ... (5 Replies)
Discussion started by: Amit Joshi
5 Replies

3. Shell Programming and Scripting

sed : replace Nth match in a file

I have a situation where a file "config.txt" looks like this Servername: OS: Serername: OS: Servername: OS: .... .... ... Servername: OS: looking for the sed syntax to replace the "Nth" occurrence of Servername (i would apply the same logic to OS as well), want to replace the Nth... (4 Replies)
Discussion started by: alldbest
4 Replies

4. Shell Programming and Scripting

Help to replace the string with special characters

{"name":"alR_pl-ENVIRONMENT_192_168_211_123_sDK_PROVISION_7","description":"aLR_pl-ENVIRONMENT_192_168_211_123_sDK_PROVISION_7","json_class":"Chef::Role","default_attributes":{},"override_attributes":{"yoapp":{"jboss":"5.1.0","port":"2243","warname":"soap","datacenter":"alR","ip":"192.168.211.123","... (3 Replies)
Discussion started by: nikhil jain
3 Replies

5. Shell Programming and Scripting

How to insert file contents after nth occurrence of a string using sed?

Hi, I would like to know how, using sed, be able to insert contents of file2 in file1 after say the second occurrence of a given string? e.g. > cat file1 banana apple orange apple banana pear tangerine apple > cat file2 I don't like apples What would be the sed command to insert... (5 Replies)
Discussion started by: dimocn
5 Replies

6. Shell Programming and Scripting

Replace a value of Nth field of nth row

Using Awk, how can I achieve the following? I have set of record numbers, for which, I have to replace the nth field with some values, say spaces. Eg: Set of Records : 4,9,10,55,89,etc I have to change the 8th field of all the above set of records to spaces (10 spaces). Its a delimited... (1 Reply)
Discussion started by: deepakwins
1 Replies

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

8. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies

9. Shell Programming and Scripting

sed script to remove nth characters from end of filename

Hi all, I have this basic script to remove, in this case 9 characters from the end of a file name. This is what I have so far, for file in *.mov do newname=`echo $file | sed 's/\(.*\)........./\1/' ` mv "$file" "$newname" done The problem is that it removes the file extension as well.... (2 Replies)
Discussion started by: Monkey Dean
2 Replies

10. Shell Programming and Scripting

replace nth instance of string

Hi all, I have file with following content ........................... ..........TEST.......... ..........TEST.......... ..................... .....TEST.......... ..................... ..................... .....TEST.......... I want to replace nth "TEST" with "OK" using... (4 Replies)
Discussion started by: uttamhoode
4 Replies
Login or Register to Ask a Question