The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Add string after another string with special characters heliode Shell Programming and Scripting 2 03-21-2008 05:06 AM
How to change only the x first characters of a string? Juha Shell Programming and Scripting 2 09-28-2007 05:17 AM
Removing characters from a string mh53j_fe Shell Programming and Scripting 3 06-03-2005 09:35 AM
Removing characters from end of $string craig2k Shell Programming and Scripting 3 03-25-2003 07:38 AM
removing characters from end of string el_toro Shell Programming and Scripting 4 07-14-2002 05:02 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-06-2008
Registered User
 

Join Date: Dec 2004
Location: Zürich
Posts: 146
exract string between two different characters

I want to extract a string from a line in a text file between two different characters:

for example:

:string"

I want to extract string. It has to be done in a one-liner.
Reply With Quote
Forum Sponsor
  #2  
Old 05-06-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,059
One way:
Code:
$ cat file
abc:string"def
$ sed -e 's/\(^.*:\)\(.*\)\(".*$\)/\2/' file
string
$
Reply With Quote
  #3  
Old 05-06-2008
Registered User
 

Join Date: Jan 2007
Posts: 25
echo ":string\"" | awk -F":" '{print $2}' | awk -F"\"" '{print $1}'
Reply With Quote
  #4  
Old 05-06-2008
Moderator
 

Join Date: Feb 2007
Posts: 2,314
Another one:

Code:
sed 's/.*:\(.*\)".*/\1/' file
Regards
Reply With Quote
  #5  
Old 05-06-2008
drl's Avatar
drl drl is online now
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 555
Hi.

With perl:
Code:
cat <<'EOF' |
Characters before :string" and characters after.
EOF
perl -wn -e 'print "$1\n" if m{[:](.*?)["]};'
Producing:
Code:
string
cheers, drl
Reply With Quote
  #6  
Old 05-06-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,924
Do you need to handle multiple matches on one line?
Awk:

Code:
awk '$0=$2' FS=: RS=\"
Or:

Code:
perl -nle'print $1 while /:(.*?)\"/g'

Last edited by radoulov; 05-06-2008 at 05:51 AM.
Reply With Quote
  #7  
Old 05-06-2008
drl's Avatar
drl drl is online now
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 555
Hi.

The point raised by radoulov is a good one. Not all of the solutions produce the same results because of different assumptions:
Code:
#!/bin/bash -

# @(#) s3       Demonstrate comparison of solutions.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) awk perl sed

cat <<'EOF' >data1
Characters before :string" and characters after, :another instance".
EOF

echo
echo " Input file data1:"
cat data1

echo
echo " Results of perl, left-most, shortest extraction:"
perl -wn -e 'print "$1\n" if m{[:](.*?)["]};' data1

echo
echo " Results of perl, longest extraction:"
perl -wn -e 'print "$1\n" if m{[:](.*)["]};' data1

echo
echo " Results of one sed solution:"
sed -e 's/\(^.*:\)\(.*\)\(".*$\)/\2/' data1

echo
echo " Results of another sed solution:"
sed 's/.*:\(.*\)".*/\1/' data1

echo
echo " Results of one awk solution:"
awk '$0=$2' FS=: RS=\" data1

echo
echo " Results of perl, all extractions (if -> while;g):"
perl -wn -e 'print "$1\n" while m{[:](.*?)["]}g;' data1

exit 0
Producing:
Code:
% ./s3

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
GNU Awk 3.1.4
perl 5.8.4
GNU sed version 4.1.2

 Input file data1:
Characters before :string" and characters after, :another instance".

 Results of perl, left-most, shortest extraction:
string

 Results of perl, longest extraction:
string" and characters after, :another instance

 Results of one sed solution:
another instance

 Results of another sed solution:
another instance

 Results of one awk solution:
string
another instance

 Results of perl, all extractions (if -> while;g):
string
another instance
Which is another lesson for me in the usefulness of complete, non-ambiguous requirements ... cheers, drl
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:18 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0