Sponsored Content
Top Forums Shell Programming and Scripting Extract string between two different characters Post 302192217 by drl on Tuesday 6th of May 2008 09:47:50 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
PMFUNC(1p)						User Contributed Perl Documentation						PMFUNC(1p)

NAME
pmfunc - cat out a function from a module DESCRIPTION
Given a fully-qualified function, this program opens up the file and attempts to cat out the source for that function. EXAMPLES
$ pmfunc Cwd::getcwd sub getcwd { abs_path('.'); } RESTRICTIONS
Only subroutines that are defined in the normal fashion are seen, since a simple pattern-match is what does the extraction. Those loaded other ways, such as via AUTOLOAD, typeglob aliasing, or in an "eval", will all necessarily be missed. This is mostly here for people who are too lazy to type sed '/^sub getcwd/,/}/p' `pmpath Cwd` or perl -ne 'print if /^subs+getcwd/ .. /}/' `pmpath Cwd` RESTRICTIONS
SEE ALSO
AUTHORS and COPYRIGHTS Copyright (C) 1999 Tom Christiansen. Copyright (C) 2006-2008 Mark Leighton Fisher. This is free software; you can redistribute it and/or modify it under the terms of either: (a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or (b) the Perl "Artistic License". (This is the Perl 5 licensing scheme.) Please note this is a change from the original pmtools-1.00 (still available on CPAN), as pmtools-1.00 were licensed only under the Perl "Artistic License". perl v5.10.1 2010-02-22 PMFUNC(1p)
All times are GMT -4. The time now is 08:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy