Extract string between two different characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract string between two different characters
# 1  
Old 05-06-2008
Extract 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.
# 2  
Old 05-06-2008
One way:
Code:
$ cat file
abc:string"def
$ sed -e 's/\(^.*:\)\(.*\)\(".*$\)/\2/' file
string
$

# 3  
Old 05-06-2008
echo ":string\"" | awk -F":" '{print $2}' | awk -F"\"" '{print $1}'
# 4  
Old 05-06-2008
Another one:

Code:
sed 's/.*:\(.*\)".*/\1/' file

Regards
# 5  
Old 05-06-2008
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
# 6  
Old 05-06-2008
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 09:51 AM..
# 7  
Old 05-06-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Outputting characters after a given string and reporting the characters in the row below --sed

I have this fastq file: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGGGGGGGGGGGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCA +test-1 GGGGGGGGGGGGGGGGGCCGGGGGFF,EDFFGEDFG,@DGGCGGEGGG7DCGGGF68CGFFFGGGG@CGDGFFDFEFEFF:30CGAFFDFEFF8CAF;;8... (10 Replies)
Discussion started by: Xterra
10 Replies

2. UNIX for Beginners Questions & Answers

Extract characters from a string name

Hi All, I am trying to extract only characters from a string value eg: abcdedg1234.cnf How can I extract only characters abcdedg and assign to a variable. Please help. Thanks (2 Replies)
Discussion started by: abhi_123
2 Replies

3. UNIX for Dummies Questions & Answers

Extract string between two special characters

Hi, I have a file that looks something like >1-18*anc... (12 Replies)
Discussion started by: jyu429
12 Replies

4. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

5. Shell Programming and Scripting

Extract string between 2 special characters

Hi, I have a unix file with contents as below Line1: ABC MNN X$$QWERTY$$ JKL Line2: HELLO $$HOW$$ ARE $$YOU$$ DOING i want to extract the string between $$ and $$ ie i want the output as QWERTY HOW YOU i want those strings seperated by some character say | desired output is... (7 Replies)
Discussion started by: vinredmac
7 Replies

6. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

7. Shell Programming and Scripting

remove characters from string based on occurrence of a string

Hello Folks.. I need your help .. here the example of my problem..i know its easy..i don't all the commands in unix to do this especiallly sed...here my string.. dwc2_dfg_ajja_dfhhj_vw_dec2_dfgh_dwq desired output is.. dwc2_dfg_ajja_dfhhj it's a simple task with tail... (5 Replies)
Discussion started by: victor369
5 Replies

8. Shell Programming and Scripting

help with Scripting - trying to search for string and extract next few characters

Hi I am new to world on unix scripting so any assistance would be gratefully appreciated, I am trying to write a script which reads through a file, reads in line by line, searches for a pattern, copies string after it and then to do a search and replace elsehwere in the line, so the... (7 Replies)
Discussion started by: LonJ_80
7 Replies

9. Shell Programming and Scripting

Extract upper case characters from a string

Hi Friends, I have a script which returns me the following values line by line ora_smon_RCAT102 oracleKHP102B (DESC oraclePROOIDDB92 (DES oraclePMS (DESCRIP oracleBRIO (LOCAL=N oracleBRIO (LOCAL=N oraclePRDB92 (DES oracleBRIO (LOCAL=N oracleBRIO (LOCAL=N oraclePMS (DESCRIP... (4 Replies)
Discussion started by: orakhan
4 Replies

10. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies
Login or Register to Ask a Question