sed Character match and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed Character match and replace
# 1  
Old 08-02-2013
sed Character match and replace

Hello All

I am struck in the issue which I want to share with all of you.
What I am trying to do is For every line in a file I have to replace a particular character from the given character in a file
For Example

Suppose the data is
Code:
1111x2222
1111x2222
2222y3333
1111x2222

I will pass three argument to shell script
./script 5 x y
It mean for Evey 5th character in line if it found x it should replace by y and if it found y it should replace with x

So output should be
Code:
1111y2222
1111y2222
2222x3333
1111y2222

---------- Post updated at 03:46 AM ---------- Previous update was at 03:45 AM ----------

So far what I wrote in shell script is
Code:
pos=$1
ppos=`expr $pos - 1`
pat1=$2
pat2=$3

sed "/^.\{$ppos\}$pat1/s/./$pat2/$pos" mdat

But it is doing at one way
# 2  
Old 08-02-2013
Have you attempted anything to solve this ?
# 3  
Old 08-02-2013
Using awk

Code:
pos=$1
ch=$2
sub=$3
awk -v p=$pos -v c=$ch -v s=$sub '{if(substr($0,p,1)==c) {print substr($0,1,p-1) s substr($0,p+2)}}' mdat

# 4  
Old 08-02-2013
No its not working
./mscr 5 x y
When I tried with data
Code:
1111x2222
1111x2222
2222y3333
1111x2222
2 22y3333
1111x2222
22 2y3333
2222y3333
 111x2222
2222y3333
1111x2222
2222y3333

it does not give expected result
# 5  
Old 08-02-2013
Sorry, I had omitted the else part. Corrected it as below

Code:
pos=$1
ch=$2
sub=$3
awk -v p=$pos -v c=$ch -v s=$sub '{if(substr($0,p,1)==c) {print substr($0,1,p-1) s substr($0,p+2)} else {print substr($0,1,p-1) c substr($0,p+2)}}' mdat

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed to print the character from the previous line after the regexp match

Hi All, I need to print the characters in the previous line just before the regular expression match Please have a look at the input file as attached I need to match the regular expression ^ with the character of the previous like and also the pin numbers and the output file should be like... (6 Replies)
Discussion started by: kshitij
6 Replies

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

3. Shell Programming and Scripting

Need help with sed to match and replace a string

friends I am struck in a situation where I need to comment a line start with space as below in a file root@LOCALHOST * rw LOCALHOST* r I should comment second line only Any help please (16 Replies)
Discussion started by: mallak
16 Replies

4. Shell Programming and Scripting

Remove/replace the very first character/symbol match

cat file.txt file 1123.x July 23:222 /cd/hh2/k39/ss2/f7d8d9d8e6r5t4s/dd2/e/s7a/s7a2afa5017d8b975-1.7-1395610245-b22e19bbc477b134 i wish to only extract out the 1.7 (anything within the first - -) i try to look for the sed command under match the first occurence of pattern but out of luck, my... (6 Replies)
Discussion started by: ctphua
6 Replies

5. Shell Programming and Scripting

Replace second match+awk/sed

I have a text file that looks like this: ----------------------------------------- sta WP00 time 10/23/2013 20:10:17 sensor trillium_240_2 0 583 add close sensor trillium_240_2 10/23/2013 20:10:17 sensor trillium_120 0 279 add close sensor trillium_120 10/23/2013 20:10:35... (11 Replies)
Discussion started by: klane
11 Replies

6. Shell Programming and Scripting

SED to replace exact match, not first occurrence.

Lets say I have file.txt: (Product:Price:QuantityAvailable) (: as delimiter) Chocolate:5:5 Banana:33:3 I am doing a edit/update function. I want to change the Quantity Available, so I tried using the SED command to replace 5, but my Price which is also 5 is changed instead. (for the Banana... (13 Replies)
Discussion started by: andylbh
13 Replies

7. Shell Programming and Scripting

how to replace specific character , if possible using sed

My script is extracting data from SQl session, however sometimes the result contains one or multiple space after/before any numerical value. e,g . "123","1 34","1 3 45", "43 5" How to remove these unwanted spaces..so that I can get the following result : "123","134",1345","435" (1 Reply)
Discussion started by: mady135
1 Replies

8. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

9. Shell Programming and Scripting

sed help,to replace the last character

cat input.txt agsbdafgd ertebrtreter ahrbrwerg The last character of a line that does not start with a would be changed to Z. Final output: agsbdafgd ertebrtreteZ ahrbrwerg Can anyone post the sed command to do that? (2 Replies)
Discussion started by: cola
2 Replies

10. UNIX for Dummies Questions & Answers

match a character in a line and replace

Hi, I have a file with large number of records. Sample below: 123456789QWERT2U 2 erter 987123678ZXCVB6Y 5 7689 934567123GHJKUI4O 7 - -- -- I want the 16th character in each record to be replaced with the below as follows;so 2 will become K, 6 will become O and 4 will become... (3 Replies)
Discussion started by: er_ashu
3 Replies
Login or Register to Ask a Question