Extracting Text Between Quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting Text Between Quotes
# 1  
Old 01-12-2010
Extracting Text Between Quotes

I won't pretend. I am no bash, grep, awk or sed specialist but I have tried for a couple of days to extract something from a line in a file but have badly failed. There are just too may parenthesis, quotemarks, periods, etc. to trip me up, I think.

I need to extract mail24b.someplace.net and nothing more or less than that. I have to consider that mail24b.someplace.net is an unknown though. It could be mail.goober.org or incoming.outthere.com, etc. Whatever ... I just need to extract the info between that last set of quotes in the line.

Code:
user_pref("mail.server.server15.realhostname", "mail24b.someplace.net");

# 2  
Old 01-12-2010
There are a lot ways you can approach this problem, one way is ( more ways to be followed by our team ):

Code:
sed 's/.*"\(.*\)"[^"]*$/\1/'
user_pref("mail.server.server15.realhostname", "mail24b.someplace.net");
mail24b.someplace.net

Explanation:
  • s/.*"\(.*\)"[^"]*$/\1/ : substitute only the final "" double quoted content.
  • second line is input,
  • third line is the output.

Last edited by thegeek; 01-12-2010 at 11:23 PM.. Reason: added explanation.
# 3  
Old 01-12-2010
Code:
awk -F\" '{print $(NF-1)}'

Example:
Code:
$ echo 'user_pref("mail.server.server15.realhostname", "mail24b.someplace.net");' | awk -F\" '{print $(NF-1)}'
mail24b.someplace.net

Regards,
alister
# 4  
Old 01-13-2010
Well, both of these work just fine. My own attempts with sed were not even close to:
Code:
sed 's/.*"\(.*\)"[^"]*$/\1/'

Obviously, I am going to have to study this a little bit, now that I have a correct example, to figure it all out.


As for the awk code, I was close with that. My last failed attempt was
Code:
awk -F/" '{print $NF}'

So, I guess I was atleast on the right trail. After my AM coffee I will check out my little guide and figure out what I was missing. I'm thinking I was maybe considering $NF as end of line. Then again, what I was wanting actually was not end of line. Whatever, I'll figure out what's what in a bit.

I appreciate the help from both of you. I get to learn something out of this. Thank you!

Last edited by Trapper; 01-14-2010 at 09:51 AM..
# 5  
Old 01-13-2010
backslash in awk.

awk -F\" '{print $NF}'
# 6  
Old 01-14-2010
Quote:
Originally Posted by rdcwayx
backslash in awk.

awk -F\" '{print $NF}'
Yeah, I caught that when comparing my attempts to the suggestions that were submitted. I retain well when learning by trial and error and this was definitely an error. Smilie
# 7  
Old 01-14-2010
another approch

Code:
sed 's/.*, \"\([^"]*\).*/\1/'

SmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

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

How to add line breaks to perl command with large text in single quotes?

Below code extracts multiple field values from XML into array and prints all in one line. perl -nle '@r=/(?: jndiName| authDataAlias| value| minConnections| maxConnections| connectionTimeout| name)="(+)/g and print join ",",$ENV{tIPnSCOPE},$ENV{pr ovider},$ENV{impClassName},@r' server.xml ... (4 Replies)
Discussion started by: kchinnam
4 Replies

3. Shell Programming and Scripting

Extracting data from between double quotes

Need assistance , Below is the data between double code . </td><td><a href="geavg.t00z.pgrb2af18">geavg.t00z.pgrb2af18</a> Below commands gives me the result but i want everything in one command using single nawk nawk -v RS="< href" -F">" '/t00z/ { print $1 }' ucar.output | nawk -F... (10 Replies)
Discussion started by: ajayram_arya
10 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. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

6. Shell Programming and Scripting

Extracting text from within a section of text using AWK

I have a command which returns the below output. How can I write a script to extract mainhost and secondhost from this output and put it into an array? I may sometimes have more hosts like thirdhost. I am redirecting this output to a variable. So I guess there should be a awk or sed command to... (7 Replies)
Discussion started by: heykiran
7 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

How to parse a file for text b/n double quotes?

Hi guys, I desperately need some help here... I need to parse a file similar to this: I need to read the values for MY_BANNER_SSHD and WARNING_MESSAGE. The value could be empty/single line or multi-line! # Comments . . . Some lines MY_BANNER_SSHD=""... (7 Replies)
Discussion started by: shreeda
7 Replies

9. Shell Programming and Scripting

Extracting Text

Hi, I have a file like that No seq Text 269091 123443124 how are slkslks, serial no is 042890000 and pincode is 090001223433454 269091 123443124 how are slkslks, Sr/no is 0428901 and 14 digits are is ... (3 Replies)
Discussion started by: krabu
3 Replies

10. UNIX for Dummies Questions & Answers

extracting text and reusing the text to rename file

Hi, I have some ps files where I want to ectract/copy a certain number from and use that number to rename the ps file. eg: 'file.ps' contains following text: 14 (09 01 932688 0)t the text can be variable, the only fixed element is the '14 ('. The problem is that the fixed element can appear... (7 Replies)
Discussion started by: JohnDS
7 Replies
Login or Register to Ask a Question