![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
One way:
Code:
$ cat file
abc:string"def
$ sed -e 's/\(^.*:\)\(.*\)\(".*$\)/\2/' file
string
$
|
|
#3
|
|||
|
|||
|
echo ":string\"" | awk -F":" '{print $2}' | awk -F"\"" '{print $1}'
|
|
#4
|
|||
|
|||
|
Another one:
Code:
sed 's/.*:\(.*\)".*/\1/' file |
|
#5
|
||||
|
||||
|
Hi.
With perl: Code:
cat <<'EOF' |
Characters before :string" and characters after.
EOF
perl -wn -e 'print "$1\n" if m{[:](.*?)["]};'
Code:
string |
|
#6
|
||||
|
||||
|
Do you need to handle multiple matches on one line?
Awk: Code:
awk '$0=$2' FS=: RS=\" Code:
perl -nle'print $1 while /:(.*?)\"/g' Last edited by radoulov; 05-06-2008 at 05:51 AM. |
|
#7
|
||||
|
||||
|
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
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 |
||||
| Google The UNIX and Linux Forums |
| Tags |
| linux, solaris |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|