LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-19-2007, 08:53 AM   #1
anandjk1
LQ Newbie
 
Registered: Oct 2007
Posts: 0

Rep: Reputation: 0
Replace string in Shell scripting


Hi,

I need to check if the starting or ending of the given string has a double quote and replace it with null.

e.g. Input
var="test string "is" string"
Output
var=test string "is" string

Please help in this regard

Thanks
Anand
 
Old 10-19-2007, 11:43 AM   #2
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Not sure if this is the best way of doing it, but here you go.
Code:
$ cat file
var="test string "is" string"
var2=test2 "is" also a string

$ sed 's/\([^=]*=\)"\(.*\)"$/\1\2/' file
var=test string "is" string
var2=test2 "is" also a string
edit:

awk and perl solutions:
Code:
$ awk 'BEGIN{FS=OFS="="} {gsub("\"$", "", $2);gsub("^\"", "", $2)};1' 'file'

$ perl -F'=' -lane '@F[1] =~ s/^"//;@F[1] =~ s/"$//;print join "=",@F' 'file'

Last edited by angrybanana; 10-19-2007 at 12:00 PM.
 
Old 10-19-2007, 08:16 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
assuming you only want to check starting and ending

Code:
#var='"test string "is" string"'
#echo $var|awk 'BEGIN{FS=""}$1=="\""&&$NF=="\""{print substr($0,2,NF-2)}'
test string "is" string
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 08:49 PM
sed replace string octeto Programming 4 06-06-2007 02:09 AM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 03:07 AM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration