Replace nth to nth character?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace nth to nth character?
# 1  
Old 07-12-2015
Replace nth to nth character?

Hi

I got the following problem and I wonder if some could please help me out?
I'd like to replace character 8 - 16 , 16 - 24
Code:
cat file

4040AB50025D8843039E31F70000E5020046904444047FD76A029E8009E8B387C19C7FA44E2842B1D6229383502608435A552D1A5DD33EC7F13EC43F1A67B2B648F2E16D7122D39A89DA8D075644158C70048E5B92C2DECB00EA020051904F44049A16C9312C09E3ED1E0D792E923D1AF2C97FCAE2ED66DB003E505DAC0F09C31D024E10A497301D954F060033CD7C2D96B6729CE3620416FB2405519F70A2E32D86AFF28724D590BF7087557BB6

Code:
cat file | sed 's/\(.\{8\}\).\{8\}/\1AAAAAAAA/'

4040AB50AAAAAAAA039E31F70000E5020046904444047FD76A029E8009E8B387C19C7FA44E2842B1D6229383502608435A552D1A5DD33EC7F13EC43F1A67B2B648F2E16D7122D39A89DA8D075644158C70048E5B92C2DECB00EA020051904F44049A16C9312C09E3ED1E0D792E923D1AF2C97FCAE2ED66DB003E505DAC0F09C31D024E10A497301D954F060033CD7C2D96B6729CE3620416FB2405519F70A2E32D86AFF28724D590BF7087557BB6

Works ok and replaces characters, string is the same length

Code:
cat file | sed 's/\(.\{16\}\).\{16\}/\1AAAAAAAA/'
4040AB50025D8843AAAAAAAA0046904444047FD76A029E8009E8B387C19C7FA44E2842B1D6229383502608435A552D1A5DD33EC7F13EC43F1A67B2B648F2E16D7122D39A89DA8D075644158C70048E5B92C2DECB00EA020051904F44049A16C9312C09E3ED1E0D792E923D1AF2C97FCAE2ED66DB003E505DAC0F09C31D024E10A497301D954F060033CD7C2D96B6729CE3620416FB2405519F70A2E32D86AFF28724D590BF7087557BB6

Cuts some characters off????

Thanks for your help
# 2  
Old 07-12-2015
Code:
sed 's/\(.\{16\}\).\{16\}/\1AAAAAAAA/' file

The cat command is not necessary since sed can read a file on its own.
The red part is matching another 16 characters, and when you \1 those 16 matched chars are discarded and replaced by AAAAAAAA.
Code:
sed 's/\(.\{16\}\).\{8\}/\1AAAAAAAA/' file

The red part has to be the same length of the substitution if you do not want to truncate the original line any farther.

---------- Post updated at 05:25 PM ---------- Previous update was at 05:02 PM ----------

You can do it with many other programs, however I prefer Perl.

Code:
perl -pe 'substr($_, 16, length "AAAAAAAA") = "AAAAAAAA"' file

4040AB50025D8843AAAAAAAA0000E5020046904444047FD76A029E8009E8B387C19C7FA44E2842B1D6229383502608435A552D1A5D...


Last edited by Aia; 07-12-2015 at 08:45 PM..
This User Gave Thanks to Aia For This Post:
# 3  
Old 07-13-2015
Thank you very much all worked great Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

How to read the nth character from the line.?

I have Index Line and I tried to get the 9th character from the file and to check the character is "|" or not. Shell Scripting. Sample Index file. "91799489|K8E|188.004.A.917994892.1099R.c.01.pdf|2013|10/15/2014|002|B|C|C"... (3 Replies)
Discussion started by: pavand
3 Replies

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

3. Shell Programming and Scripting

awk script to replace nth character with comma

I have a requirement as below. In one of my column, I have data which may or may not be separted with coma always. Now I need to validate the length of these text within the coma (if available) and if the length is more than 30 characters, I need to insert a coma either at 30 characters if its... (3 Replies)
Discussion started by: aramacha
3 Replies

4. Shell Programming and Scripting

To find nth position of character in string

Hi guyz i want to know nth position of character in string. For ex. var="UK,TK,HK,IND,AUS" now if we see 1st occurance of , is at 3 position, 2nd at 6,..4th at 13 position. 1st position we can find through INDEX, but what about 2nd,3rd and 4th or may be upto nth position. ? In oracle we had... (2 Replies)
Discussion started by: Jonty Immortal
2 Replies

5. Shell Programming and Scripting

[Solved] Find and replace till nth occurence of a special character

Hi, I have a requirement to search for a pattern in each line in a file and remove the in between words till the 3rd occurrence of double quote ("). Ex: CREATE TABLE "SCHEMANAME"."AMS_LTV_STATUS" (Note: "SCHEMANAME" may changes for different schemas. Its not a fixed value) I need to... (2 Replies)
Discussion started by: satyaatcgi
2 Replies

6. Emergency UNIX and Linux Support

Replace nth position character of all the lines in file

I want to replace 150th character of all the lines in a file using sed or awk... searched the forums but didn't find exact answer (9 Replies)
Discussion started by: greenworld123
9 Replies

7. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

8. Shell Programming and Scripting

Using AWK to find top Nth values in Nth column

I have an awk script to find the maximum value of the 2nd column of a 2 column datafile, but I need to find the top 5 maximum values of the 2nd column. Here is the script that works for the maximum value. awk 'BEGIN { subjectmax=$1 ; max=0} $2 >= max {subjectmax=$1 ; max=$2} END {print... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

9. UNIX for Dummies Questions & Answers

Replace nth character in a file with a period

Hi all, If you look at the example below,I want to replace the 21st character (,) with a period (.). I have 1000 records in a file can someone help me how to do that. Thankyou all in advance. "2008-07-15... (3 Replies)
Discussion started by: blackhawk_123
3 Replies

10. UNIX for Dummies Questions & Answers

How do I get the nth character from a string?

How to I get the nth character from a string in shell script. For instance, I have a string London. I want to get, say the first character (L) from the string. How do I do this in unix shell? Thankx (4 Replies)
Discussion started by: toughman
4 Replies
Login or Register to Ask a Question