Search and replace is not working by sed or awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and replace is not working by sed or awk
# 1  
Old 05-08-2013
Search and replace is not working by sed or awk

Hi ,

I have one file and in this file i have one like
Code:
TEST1 KEY0=AAC040R1;AAC041R1ISE;AAC041R2ISE;AAC370R1;ADR0500;ADR0600;AME245R1;AME245R2;BAP0135;BAP0300;PPINVDTD*;PPJERPTD*;PPJERPT*;PRBSUMM*;:

i want to replace this line with the following line
Code:
TEST1 KEY0=AAC040R1;AAC041R1ISE;AAC041R2ISE;AAC370R1;ADR0500;ADR0600;AME245R1;AME245R2;BAP0135;BAP0300;PPINVDTD*;PPJERPTD*;PPJERPT*;PRBSUMM*;AAC185R1,BAR0111*;:

my code is not working , i m using following code;

Code:
Str1="TEST1 KEY0=AAC040R1;AAC041R1ISE;AAC041R2ISE;AAC370R1;ADR0500;ADR0600;AME245R1;AME245R2;BAP0135;BAP0300;PPINVDTD*;PPJERPTD*;PPJERPT*;PRBSUMM*;:"
Str2="TEST1 KEY0=AAC040R1;AAC041R1ISE;AAC041R2ISE;AAC370R1;ADR0500;ADR0600;AME245R1;AME245R2;BAP0135;BAP0300;PPINVDTD*;PPJERPTD*;PPJERPT*;PRBSUMM*;AAC185R1,BAR0111*;:"

Code:
awk -v s1="$Str1" -v s2="$Str2" '{sub(s1,s2)}1' test > test1

in the new file test1 having the same data as test.
I tried with sed as well but it is not working .

Please suggest how to do it.


Moderator's Comments:
Mod Comment Use code tags.

Last edited by zaxxon; 05-08-2013 at 05:14 AM..
# 2  
Old 05-08-2013
Looking at the string you want to replace I can see there is only small change at the end of line.. considering it remains same..

Code:
 
awk -F":" '/^TEST1 KEY0/{print $1"AAC185R1,BAR0111*;:"}' filename

# 3  
Old 05-08-2013
Quote:
Originally Posted by vidyadhar85
Looking at the string you want to replace I can see there is only small change at the end of line.. considering it remains same..

Code:
 
awk -F":" '/^TEST1 KEY0/{print $1"AAC185R1,BAR0111*;:"}' filename



in this example there are only two new entries, however it could me more than that, thats why i am passing it through variable.
# 4  
Old 05-08-2013
You could use \* instead of * for Str1, similarly escape other metacharacters as needed. I know that's a bother, but certainly doable. Not sure what else to suggest that would work.
Code:
$ cat input
TEST1 KEY0=AAC040R1;AAC041R1ISE;AAC041R2ISE;AAC370R1;ADR0500;ADR0600;AME245R1;AME245R2;BAP0135;BAP0300;PPINVDTD*;PPJERPTD*;PPJERPT*;PRBSUMM*;:

Code:
$ cat ./test-2.sh
Str1="TEST1 KEY0=AAC040R1;AAC041R1ISE;AAC041R2ISE;AAC370R1;ADR0500;ADR0600;AME245R1;AME245R2;BAP0135;BAP0300;PPINVDTD\*;PPJERPTD\*;PPJERPT\*;PRBSUMM\*;:"
Str2="TEST1 KEY0=AAC040R1;AAC041R1ISE;AAC041R2ISE;AAC370R1;ADR0500;ADR0600;AME245R1;AME245R2;BAP0135;BAP0300;PPINVDTD*;PPJERPTD*;PPJERPT*;PRBSUMM*;AAC185R1,BAR0111*;:"
sed "s/$Str1/$Str2/" input

Code:
$ ./test-2.sh
TEST1 KEY0=AAC040R1;AAC041R1ISE;AAC041R2ISE;AAC370R1;ADR0500;ADR0600;AME245R1;AME245R2;BAP0135;BAP0300;PPINVDTD*;PPJERPTD*;PPJERPT*;PRBSUMM*;AAC185R1,BAR0111*;:

# 5  
Old 05-08-2013
the value in these variable is not constant, it will change everytime and I do not know where i will have * . There is a command that fetching the value for these two variable.

Is there any way we can do it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Another sed/awk search=>replace question

Hello, Need a little bit of help. Basically I need to replace lines in a file which were calculated wrong as it would 12 hours to regenerate the data. I need to calculate values based on other files which I've managed to figure out with grep/cut but now am stuck on how to shove these new... (21 Replies)
Discussion started by: f77coder
21 Replies

2. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

3. Shell Programming and Scripting

awk/sed to search & replace data in first column

Hi All, I need help in manipulating the data in first column in a file. The sample data looks like below, Mon Jul 18 00:32:52 EDT 2011,NULL,UAT Jul 19 2011,NULL,UAT 1] All field in the file are separated by "," 2] File is having weekly data extracted from database 3] For eg.... (8 Replies)
Discussion started by: gr8_usk
8 Replies

4. Shell Programming and Scripting

How to use SED or AWK to search and replace an exact string

I have a file DS1 DDS DS I want to replace only "DS" to "DSmail.blah.com" in a lot of files. I tried sed 's/DS/DSmail.blah.com' but it changes all the lines . thanks in advance (2 Replies)
Discussion started by: gubbu
2 Replies

5. Shell Programming and Scripting

awk/sed string search and replace

Need help with either sed or awk to acheive the following file1 ----- In the amazon forest The bats eat all the time... mon tue wed they would eat berries In the tropical forest The bats eat all the time... on wed bats eat nuts In the rain forest The bats eat all the time... on... (2 Replies)
Discussion started by: jville
2 Replies

6. Shell Programming and Scripting

SED Search in YouTube file not working.

Hi I've succesfully downloaded a HTML file from youtube (for a specific video id) I'm trying to use the SED command to search for double and single quotes and convert them to a line break (new line). However i'm getting end of file errors when I run this script, can someone help? I've... (7 Replies)
Discussion started by: colmbell
7 Replies

7. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

8. Shell Programming and Scripting

using sed to replace a line is not working

This is what I have this far rsh server1 "cat /home/test.txt |sed s/01-jun-2009/01-aug-2009/ |sed s/ABCD/1234/" but it is not working is there something I am doing wrong in my syntax? The file test.txt is the same on all of my 15 servers it has the same length and contents only certain... (3 Replies)
Discussion started by: deaconf19
3 Replies

9. UNIX for Dummies Questions & Answers

Perl search and replace not working in csh script

I am using perl to perform a search and replace. It works at the command line, but not in the csh shell script perl -pi -e 's@/Pattern@@g' $path/$file I used the @ as my delimiter because the pattern contains "/" (3 Replies)
Discussion started by: NobluesFDT
3 Replies

10. Shell Programming and Scripting

search and replace with restriction (awk, sed)

Hello, i have a file like that foo A new bar A new bar B new I need to replace 'new' with 'done', but only in lines containing 'bar' AND 'A'. output file should then become foo A new bar A done bar B new Sorry im not able to figure it out, not even shure if i should take sed.... (10 Replies)
Discussion started by: knoxo
10 Replies
Login or Register to Ask a Question