Find and Replace string in UNIX


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find and Replace string in UNIX
# 1  
Old 02-27-2015
Find and Replace string in UNIX

Hi All,

Greetings.

I have a .dat file which somewhere in its content contains symbol ""^ I want to replace it with "^

I tried with SED command but could not achieve what i wanted

sed -e "s/'""^'/'"^'/ig" filename.dat
# 2  
Old 02-27-2015
^ is a special character in regular expressions (meaning start of line/field, depending on context).

Try sed 's/""\^/"^/ig'.
# 3  
Old 02-27-2015
It gives me error like this

sed: command garbled: s/""\^/"^/ig
# 4  
Old 02-27-2015
This works for me

Code:
sed 's:""^:"^:g'

Code:
$ cat  tmp1
""^""^""^
""^
"^


$ sed 's:""^:"^:g' tmp1
"^"^"^
"^
"^

# 5  
Old 03-02-2015
Escaping control characters in search and replace.

Quote:
Originally Posted by CarloM
^ is a special character in regular expressions (meaning start of line/field, depending on context).

Try sed 's/""\^/"^/ig'.
shouldn't you have to escape it both times?

Try sed 's/""\^/"\^/ig'.
# 6  
Old 03-02-2015
Quote:
Originally Posted by MaddyS
It gives me error like this

sed: command garbled: s/""\^/"^/ig
It works for me with GNU sed. AIX & SunOS sed don't like the i option - try sed -e 's/""\^/"^/g'

Quote:
Originally Posted by os2mac
shouldn't you have to escape it both times?
It's not a special character in the replacement string (although it won't hurt it if you do escape it).

Last edited by CarloM; 03-02-2015 at 08:25 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. Shell Programming and Scripting

How to find and replace a string with spaces and / recursively?

Hi all, I wanted to find and replace an email id from entire directory structure on a Linux server. I found that find . -type f -print0 | xargs -0 sed -i 's/abc@yahoo.com/xyz@gmail.com/g' would do it perfectly. But my search criteria has extended and now I want to search for a string1 like... (2 Replies)
Discussion started by: pat_pramod
2 Replies

3. Solaris

How to find and replace a string?

Dear All I need to find and replace a string in a set of files. I try as : #find / -name "*"|xargs grep "Tektra"|grep -v "Tektra GSM BTS" But it doesn't work. It just finds the string in the files. I need to find and replace it.Can you please let me know how to correct it? Thank you (2 Replies)
Discussion started by: hadimotamedi
2 Replies

4. Shell Programming and Scripting

HPUX find string in directory and filetype and replace string

Hi, Here's my dilemma. I need to replace the string Sept_2012 to Oct_2012 in all *config.py files within the current directory and below directories Is this possible? Also I am trying to find all instances of the string Sept_2012 within files in the current directory and below I have... (13 Replies)
Discussion started by: pure_jax
13 Replies

5. Shell Programming and Scripting

find string and replace with string in other file

Dear all, I need your help, I have file like this: file1:23456 01910964830098775635 34567 01942809546554654323 67589 26546854368698023653 09778 58716868568576876878 08675 86178546154065406546 08573 54165843543054354305 . .file2: 23456 25 34567 26 67589 27 (2 Replies)
Discussion started by: attila
2 Replies

6. Shell Programming and Scripting

Find and replace string matching criteria

Dear Friends, I am looking for a way to replace a string (multiple lines) starting with something and ending with something (these two values do not change) with blank. Basically I want to delete this code injection accross many sites and folders. Search Code (across files and folders) that... (2 Replies)
Discussion started by: libras
2 Replies

7. UNIX for Dummies Questions & Answers

find single quote in a string and replace it

Hi, I have variable inside shell script - from_item. from_item = 40.1'1/16 i have to first find out whether FROM_ITEM contains single quote('). If yes, then that need to be replace with two quotes (''). How to do it inside shell script? Please note that inside shell script........ (4 Replies)
Discussion started by: yogichavan
4 Replies

8. Shell Programming and Scripting

Find the position of a string and replace with another string

Hi, I have a file named "Test_2008_01_21" The file contains a string "manual" that occurs many times in the file How can i find the positions of the string "manual" in the file Ex: if the string " manual " occurs three times in the file. i want to replace the second occurance of string... (6 Replies)
Discussion started by: bab123
6 Replies

9. UNIX for Dummies Questions & Answers

Find and replace character in a string

Hi all, My problem is the following: I've a script that must list all files in a directory and write this information in a text file. I've tried to get the list through ls command and then write it using msgecho msgecho "`ls $PATH_APS_JOB_ORA`" This works good but the created string... (7 Replies)
Discussion started by: callimaco0082
7 Replies

10. Programming

how to find and replace string

hi I wanted to find this char " ^M " in my file and replace it with blank space. I am using Unix system. If i give command " :%s/^M//gc " it wont work so can anyone tell what is command to find and replace thankx (3 Replies)
Discussion started by: mridula
3 Replies
Login or Register to Ask a Question