String between quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String between quotes
# 1  
Old 05-08-2012
String between quotes

Hi,

Need to capture a string between 1st & last quote.

String can be anything like
a b "c" d e
Output: c
a "b c" d e
Output: b c
a "b c" d "e" f
Output: b c d e

sed 's/.*"\(.*\)".*/\1/g'
Above helps me to find the string between last quote.
Need to find the string between 1st & last quote.
# 2  
Old 05-08-2012
Code:
echo 'a "b c" d "e" f' | sed 's/[^"]*\"\(.*\)\".*/\1/; s/\"//g'

# 3  
Old 05-08-2012
Try like...

Code:
echo 'a b "c" d e ' | grep -Po '".*?"'

# 4  
Old 05-08-2012
Goodie
Lemme try to get this.

[^"]*
Takes care of all character apart from "

sed (.*) starts matching from backward
\"\(.*\)\".*

So the 1st segment tells there shouldn't be any " before that.

Am i right?
# 5  
Old 05-08-2012
If you are in ksh then you can slice the strings with the following:-
Code:
#!/bin/ksh

MYSTRING="a b c d e"            # Quoting to set the variable only. Quotes do not form part of string
TEMPVAR="${MYSTRING#*\"}" # Cut off everything before first quote - no effect
OUTPUT="${TEMPVAR%\"*}"   # Cut off everything after last quote - no effect
echo $OUTPUT

MYSTRING="a b \"c\" d e"            # Escaped quotes do form part of string
TEMPVAR="${MYSTRING#*\"}" # Cut off everything before first quote
OUTPUT="${TEMPVAR%\"*}"   # Cut off everything after last quote
echo $OUTPUT

The last criteria is a bit awkward though. The output you suggest ignores the fact that your string may have embedded quotes in it. Do you not want to know these exist?

If not, then append | tr -d "\"" to the echo $OUTPUT statement.



I hope that this helps.

Robin
Liverpool/Blackburn
UK

Last edited by rbatte1; 05-08-2012 at 08:00 AM.. Reason: I forgot the signing off bit
# 6  
Old 05-08-2012
Bug

Code:
awk -f '"' '{$1=$NF=""}1' file


Last edited by Franklin52; 05-08-2012 at 08:17 AM.. Reason: Please use code tags
# 7  
Old 05-08-2012
Try

Code:
sed -n 's/^[^"]*"\(.*\)"[^"]*$/\1/p' <filename>|tr -d '"'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding double quotes at the end of string

My input is this: Inputfile = file.txt needs to change to, Inputfile = file.txt" I have tried using: Inputfile = `echo ${Inputfile}"' doesn't work for me. Similarly how do I change it to all double quotes: Inputfile = "file.txt" (4 Replies)
Discussion started by: bvnprasad123
4 Replies

2. UNIX for Dummies Questions & Answers

How to grep exact string with quotes and variable?

As the title says I'm running a korn script in attempts to find an exact match in named.conf finddomain.ksh #!/bin/ksh # echo "********** named.conf ************" file=/var/named/named.conf for domain in `cat $1` do grep -n '"\$domain "' $file done echo "********** thezah.inc... (1 Reply)
Discussion started by: djzah
1 Replies

3. Shell Programming and Scripting

awk : match the string and string with the quotes :

Hi all, Here is the data file: - want to match only lan3 in the output . - not lan3:1 file : OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE=""... (2 Replies)
Discussion started by: rveri
2 Replies

4. Shell Programming and Scripting

How to find a string with double quotes?

I have thousands of files in a directory. I need to find/list all files that have the below matching string - RETURNCODE: "1017" Thank you! (5 Replies)
Discussion started by: esmgr
5 Replies

5. Shell Programming and Scripting

Get string between quotes separate by commas

I'm a beginner with shell and tried to do this per hours and everytinhg gives different want i do. So I have a lot of file in *.csv ( a.csv, b.csv ...) in each file csv , it has some fields separeted by commas. ----- "joseph";"21","m";"groups";"j.j@gmail.com,j.j2@hotmail.com"... (6 Replies)
Discussion started by: flaviof
6 Replies

6. Shell Programming and Scripting

need to enclose a string in quotes

I have a script which I call and pass a text string to it. This string is then is assigned to a variable in the script. I then call another script and pass that variable to the second script, but when I do, the quotes are lost and the second script gets a total of three variables 'my', 'lovely' and... (3 Replies)
Discussion started by: iskatel
3 Replies

7. Shell Programming and Scripting

Add double quotes around the string

I have a line in multiple scripts:select into table /dir1/dir2/file.dat dir1 and dir2 are the same but file.dat is different from script to script. I need to include /dir1/dir2/file.dat into double quotes in each file of my directory:select into table "/dir1/dir2/file.dat" (13 Replies)
Discussion started by: surfer515
13 Replies

8. Shell Programming and Scripting

Removing back quotes from string in CSH

Hello, I am using csh to read a text file and save its words into variable $word in a foreach loop. These words have small back quotes ` as integral parts of them, for example, one word would be `abc`, another would be `xyz1` etc... These quotes are always the first and last characters of the... (5 Replies)
Discussion started by: aplaydoc
5 Replies

9. UNIX for Dummies Questions & Answers

String concat that keeps quotes

Hi All, I hope you can help. i am concatenating String variables using the following method. command="$command$x" i have created a script which takes a set of args passed to the script using the $* #example script args=$* count=0 for x in $args do count=`expr $count + 1` ... (8 Replies)
Discussion started by: duke
8 Replies

10. UNIX for Dummies Questions & Answers

Add single quotes in string

Hi All, I love this site, it helps newbie people like me and I appreciate everyone's help! Here is my questions. I am trying to concatenate a single quote into a character/string from a text file for each line (lets say ABC should look like 'ABC'). I tried to use awk print command to do... (1 Reply)
Discussion started by: mrjunsy
1 Replies
Login or Register to Ask a Question