Visit Our UNIX and Linux User Community


Search replace strings between single quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search replace strings between single quotes
# 1  
Old 07-25-2012
Search replace strings between single quotes

Hi All,

I need to serach and replace a strings between single quote in a file.
My file has :
location='/data1/test.log' and the output needed is
location='/data2/test_dir/test.log'

pls if anybody know this can be done in script sed or awk.

I have a script, but it's error :
Code:
sed -i "s/'[^']*'/`echo /data2/test_dir/test.log`/g" file_name

any help would be appreciated!!

Thanks
mnmonu
# 2  
Old 07-25-2012
Code:
sed -i 's:'\''[^'\'']*'\'':'\''/data2/test_dir/test.log'\'':' file

If that doesn't work, you may use "the orbiting laser gun" for sparking a camp fire (someone on this forum quoted this humourous line :-P)
Code:
perl -pe 's:\047.*?\047:\047/data2/test_dir/test.log\047:' file


Last edited by balajesuri; 07-25-2012 at 03:24 AM..
# 3  
Old 07-25-2012
Quote:
Originally Posted by balajesuri
Code:
sed -i 's:'\''[^'\'']*'\'':'\''/data2/test_dir/test.log'\'':' file

Hi balajesuri,

It's ok but when we put the environment variable that time error.

Code:
sed -i 's:'\''[^'\'']*'\'':'\''$ORACLE_HOME/test.log'\'':' file

suppose $ORACLE_HOME is /u01/app/oracle/product/10.2.0/db_1 , then output should be

location='/u01/app/oracle/product/10.2.0/db_1/test.log'
# 4  
Old 07-25-2012
Code:
perl -pe 's:\047.*?\047:\047/'$ORACLE_HOME'/test.log\047:' file

# 5  
Old 07-25-2012
Quote:
Originally Posted by balajesuri
Code:
perl -pe 's:\047.*?\047:\047/'$ORACLE_HOME'/test.log\047:' file

Thanks balajesur,
But I can't use perl in our environment. Is there any shell script?

Last edited by mnmonu; 07-25-2012 at 03:54 AM..
# 6  
Old 07-25-2012
Code:
sed -i "s:'/data1/test.log':'$ORACLE_HOME/test.log':" file

This User Gave Thanks to balajesuri For This Post:
# 7  
Old 07-25-2012
Quote:
Originally Posted by balajesuri
Code:
sed -i "s:'/data1/test.log':'$ORACLE_HOME/test.log':" file

Thanks balajesuri, it's working

Previous Thread | Next Thread
Test Your Knowledge in Computers #421
Difficulty: Medium
JavaScript is strongly typed.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert text to lower case except the strings within single quotes

Shell : bash that comes with RHEL 6.7 I have SQL scripts like below. I want to convert all the text in these files to lower case except the strings enclosed within single quotes . Any idea how I can achieve this ? Sample text: $ cat sample.txt SELECT ... (6 Replies)
Discussion started by: John K
6 Replies

2. Shell Programming and Scripting

Replace double quotes with a single quote within a double quoted string

Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is. I have the following data: Before: ... (32 Replies)
Discussion started by: pchang
32 Replies

3. Shell Programming and Scripting

Find multiple strings and replace single string

Hi, following Perl code i used for finding multiple strings and replace with single string. code: #!/usr/bin/perl my @files = <*.txt>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr = ',rdata\)' | ',,rdata\)' | ', ,rdata\)'; my $replaceStr =... (2 Replies)
Discussion started by: chettyravi
2 Replies

4. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

5. Shell Programming and Scripting

Multiple search strings replaced with single string

Hi, I need someone's help in writing correct perl code. I implemented following code for "multiple search strings replaced with single string". ========================================================= #!/usr/bin/perl my $searchStr = 'register_inst\.write_t\(' |... (2 Replies)
Discussion started by: chettyravi
2 Replies

6. Shell Programming and Scripting

Search IP address and replace with strings

Hi All, How can I find (pattern search with grep/awk/sed) all files containing any IP address pattern in one directory hierarchy and in its sub-directories. The files can contains more than 4 octets in addition to the IP address as shown below. IP address can contain any octet combinations like-... (3 Replies)
Discussion started by: sanzee007
3 Replies

7. Shell Programming and Scripting

Search replace strings between single quotes in a text file

Hi There... I need to serach and replace a strings in a text file. My file has; books.amazon='Let me read' and the output needed is books.amazon=NONFOUND pls if anybody know this can be done in script sed or awk.. i have a list of different strings to be repced by NONFOUND.... (7 Replies)
Discussion started by: Hiano
7 Replies

8. Shell Programming and Scripting

Replace single quote with two single quotes in perl

Hi I want to replace single quote with two single quotes in a perl string. If the string is <It's Simpson's book> It should become <It''s Simpson''s book> (3 Replies)
Discussion started by: DushyantG
3 Replies

9. Shell Programming and Scripting

Help with sed search&replace between two strings

Hi, i read couple of threads here on forum, and googled about what bugs me, yet i still can't find solution. Problem is below. I need to change this string (with sed if it is possible): This is message text that is being quoted to look like this: This is message text that is being quotedI... (2 Replies)
Discussion started by: angrybb
2 Replies

10. Linux

Replace cloud symbol with single quotes

Dear Experts My source file contains the symbol cloud (☁). How do i replace this ☁ symbol whose Unicode value is 2601 in linux file with single quotes ? Any help will be much appreciated. Many thanks (4 Replies)
Discussion started by: pklcnu
4 Replies

Featured Tech Videos