Replace based on an exact position


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace based on an exact position
# 1  
Old 08-23-2010
Replace based on an exact position

Trying to use sed - but having no luck.

I have a text file - I want to replace whatever character is in position 106, 157 and 237 w/ the string "xxx". Want this change for all lines w/in that text file.

I'm open to using awk or whatever command would be best for replacing characters based on the exact position. Seems like sed can do a lot, but for whatever reason, have had no luck figuring out how to replace on just position alone.

Thanks much!
# 2  
Old 08-23-2010
I know its Perl, and it looks ugly, but if you don't mind that then here it goes:
Code:
perl -pe 's/(.{105}).(.{50}).(.{79}).(.*)/\1xxx\2xxx\3xxx\4/' file > out.file

I hope I didn't mess numbering Smilie
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 08-23-2010
works great. thanks again!
# 4  
Old 08-24-2010
Code:
sed -n 's/\(.\{105\}\).\(.\{50\}\).\(.\{79\}\).\(.*\)/\1XXX\2XXX\3X\4/gp'  sample_file

# 5  
Old 08-24-2010
Code:
sed 's/./xxx/237;s/./xxx/157;s/./xxx/106' infile

# 6  
Old 08-26-2010
Code:
awk -F "" '{for (i=1;i<=NF;i++) if (i==106||i==157||i==237) $i="xxx"}1' OFS="" urfile

This User Gave Thanks to rdcwayx For This Post:
 
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 exact word by perl

my inputfile: This is a test and only a test for production - prod. echo "This is a test and only a test for production - prod" | perl -pi -e 's/prod/test/g' outputfile: This is a test and only a test for testuction - test I only want to replace prod, not production. I also... (3 Replies)
Discussion started by: loktamann
3 Replies

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

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

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

5. Shell Programming and Scripting

Exact Text Replace

I am currently working with a bash script to change some names around in 3 files. I am attempting to do this with sed but I haven't been able to get it so it won't replace partial matches. Below is an example of the files I am trying to edit. My main goal is to replace foo with test, but I... (4 Replies)
Discussion started by: Takau
4 Replies

6. Shell Programming and Scripting

search and replace exact string

Hello Everyone, Im trying to run a search and replace of exact strings and the strings that im using variables that are passed through an array in a while loop. Here is a snip of my code: USEROLD=`cat oldusers` USERNEW=`cat newusers` USEROLDARRAY=( $USEROLD ) USERNEWARRAY=( $USERNEW )... (4 Replies)
Discussion started by: skizim
4 Replies

7. Shell Programming and Scripting

Exact Search and Replace using AWK

Hello. I have written the following script to search and replace from one file into another. #awk script to search and replace from file a in file b NR == FNR { A=$2; next } { for( a in A ) sub(a, A)}1 file2 file1 While the function works pretty well, I want a. The word in File 2 to... (8 Replies)
Discussion started by: gimley
8 Replies

8. Shell Programming and Scripting

Urgent help needed !!!....to replace a exact string

Hi experts, As i am a novice unix player...so need help for the below query...banged my head from quite a while...:confused: i have a set of html files, in which i need to search for string "Page"(case sensitive) and then replace the same with some numeric code ,say, "XXX1234". Here in... (1 Reply)
Discussion started by: rahulfhp
1 Replies

9. Shell Programming and Scripting

Replace strings based on position and length?

Suppose i have a file which contains thousands of records. e.g adjgmptjadmwpgjmwmd i need to replace the string from 3rd to 8th position using awk script in entire file. And also the positions will be passed as parameter. (3 Replies)
Discussion started by: laknar
3 Replies

10. Shell Programming and Scripting

Replace exact word with blank

i have a file with the below content file1.txt ALERTADMIN.FIELD ALERTADMIN.TX_ALERTS_LOG i have another file file2 ALERTADMIN.FIELD ALERTADMIN.FIELD_WS ALERTADMIN.SECTION_FIELD_WS ALERTADMIN.TX_ACCT_PROCESSING_WORK_TABLE ALERTADMIN.TX_ACCT_REVIEW_EXEC_METRICS... (2 Replies)
Discussion started by: lavnayas
2 Replies
Login or Register to Ask a Question

Featured Tech Videos