Sponsored Content
Full Discussion: Using sed to get a substring
Top Forums Shell Programming and Scripting Using sed to get a substring Post 302283726 by cfajohnson on Tuesday 3rd of February 2009 11:18:08 PM
Old 02-04-2009
Quote:
Originally Posted by CentaurAtlas
Hi,
I have looked all over for this. I am attempting to get a the substring of a string using sed since it seemed the best solution for this.

For example my string is: "zzz foo to you and bar123 or foo"

I would like to extract the text between "and" and "or" (it could be anything, but that was my test string). "and" and "or" are unique in the string and the example is a single line, but it would be good to have it work on an arbitrary chunk of text.

To do so I thought I could do a match from the beginning of the string and just replace from the start to and with nothing. Then do the same from the reverse:

echo "zzz foo to you and bar123 or foo" | sed -e 's/[\*]and//' | more

There's no need for sed (and even less need for more):

Code:
string="zzz foo to you and bar123 or foo"
temp=${string##*and}
printf "%s\n" "${temp%%or*}"

Quote:
(I was piping the text into sed vs a file just for easy of copying and pasting, but it will be the output of another process.)

If the ouput of the process is a single line, save the output of the other process in a variable then use the above lines:

Code:
string=$( another_process ... )

If there will be multi-line output, use sed or awk, e.g.:

Code:
another_process | sed 's/.*and\(.*\)or.*/\1/'

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed extract substring on (OS X)

On OS 10.4.11 I have filenames like: 670711 SA T2 v1-1_DS_EF.doc CT_670520 AM T1 v1-2_DS_EF.doc CT_670716 - 2 SA T4 v1-2_DS_EF.doc CT_670713 SA T3 v1-1_DS_EF.doc 670421 PA DYP1 v1-1_DS_EF.doc CT_670425 PA DYP2 v1-1_DS_EF.doc CT_670107 RA T3 v1-2_DS_EF.doc CT_670521 AM T2 v1-2_DS_EF.doc... (3 Replies)
Discussion started by: mlommel
3 Replies

2. Shell Programming and Scripting

Substring using sed or awk

I am trying to get a substring from a string stored in a variable. I tried sed with a bit help from this forum, but not successful. Here is my problem. My string is: "REPLYFILE=myfile.txt" And I need: myfile.txt (everything after the = symbol). My string is: "myfile.txt.gz.20091120.enc... (5 Replies)
Discussion started by: jamjam10k
5 Replies

3. Shell Programming and Scripting

Sed and SubString

Hi Folks, I am here with a simple doubt. I am having a flat file in which I want to replace the characters from say 5 to 15 as some text. Flat file contains a single line. For example 01MRRAJESH21000RAJESH INDUSTRIES In the above line pos 16-21 is Rajesh, I want to search for the... (4 Replies)
Discussion started by: dinesh1985
4 Replies

4. Shell Programming and Scripting

Making substring with sed

Input: You can easily do this by highlighting your code. How can i get the substring from index 9 to 12 using sed? (6 Replies)
Discussion started by: cola
6 Replies

5. Shell Programming and Scripting

Substring using cut/awk/sed

Hi Gurus,I have a seemingly simple problem but struggling with it.It is as follows : I/p string - ABCDEFGHIJ20100909.txt desired o/p - AB,DEF,20100909,ABCDEFGHIJ20100909.txt How to achieve it ?Thanks in advance. Please use code tags, thank you (20 Replies)
Discussion started by: sumoka
20 Replies

6. Shell Programming and Scripting

Using sed to find a substring from a file

HI All, I have a file which looks like below ./Prod_id/rel/prod/lib.a ./Some_text/rel/fld/lib.a ./Some_text/deb/detail/lib.a ./Some_text/deb/err/lib.a ./Some_text/rel/prod/lib.a ./Some_text/rel/fld/lib.a ./Some_text/deb/detail/lib.a ./Some_text/deb/err/lib.a I want... (5 Replies)
Discussion started by: anand.shah
5 Replies

7. Shell Programming and Scripting

sed optimized for replace substring

Hi All, I have this text file IR7013903069171012INT10171211864 Axxxxxxxxxx12345dddddddddddd Byyyyyyyyy99999999ddddddddd IR7013903069171012TOS10171211865 Cffffffffff5745747ehdefffffffffgggggggg Dyyyyyyyyy99999999ddddddddd I need to 1) find lines where the substring IR70139 is... (5 Replies)
Discussion started by: gio123bgg
5 Replies

8. Shell Programming and Scripting

Substring Problem with sed . . .

Greetings. I'm looking to isolate the first occurrence of an arbitrary substring which may be present at any particular line in a given file. The enclosing end markers for the target in our thought problem are string" and ". The complete string and surrounding text could look something like... (3 Replies)
Discussion started by: LinQ
3 Replies

9. Shell Programming and Scripting

sed - replacing a substring containing a hyphen

I'm attempting to replace a substring that contains a hyphen and not having much success, can anyone point out where i'm going wrong or suggest an alternative. # echo /var/lib/libvirt/images/vm888b-clone.qcow | sed -e 's|vm888-clone|qaz|g' /var/lib/libvirt/images/vm888b-clone.qcow (1 Reply)
Discussion started by: squrcles
1 Replies

10. Shell Programming and Scripting

sed question for substring search

i have this data where i am looking for a two digit number 01,03,05 or 07. if not found i should detect that . this sed command gives me the matching rows . I want the opposite , i want the rows if the match is NOT found . also the sed command is only looking for 01, can i add 03, 05, 07 to... (7 Replies)
Discussion started by: boncuk
7 Replies
gobject-query(1)                                                   User Commands                                                  gobject-query(1)

NAME
gobject-query - displays information about gobject types SYNOPSIS
gobject-query qualifier [-r type] [ -i | -b ""] [-s #] [-n] [-h] DESCRIPTION
gobject-query can be used to display the different types of gobjects provided by the glib library OPTIONS
The following options are supported: -b Specifies the indent string. This option only applies to the output layout. -h Displays a help message about the gobject-query command, and the options that you can use with the gobject-query command. -i Specifies an incremental indent string. This option only applies to the output layout. -n Specifies that gobject-query should not descend the type tree. This means that derived types are not displayed. -r Specifies the root type, for example, GObject, GParam. -s Specifies the line spacing. This option only applies to the output layout. OPERANDS
The following operands are supported: qualifier The following qualifiers are supported: froots Iterates over the fundamental root tree Prints the type tree EXAMPLES
Example 1: Displaying All Types Provided by glib and Derived from GParam example% gobject-query tree GParam Example 2: Changing the Output Layout example% gobject-query froots | `void | `void | `void | `void | `GInterface | `GTypePlugin example% gobject-query froots -b foo foo| foo`void foo| foo`void foo| foo`void foo| foo`void foo| foo`GInterface example% gobject-query froots -s 5 | | | | | `void | | | | | `void | | | | | `void ... ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of gobject-query: NLSPATH. EXIT STATUS
The following exit values are returned: 0 Application exited successfully >0 Application exited with failure FILES
The following files are used by this application: /usr/bin/gobject-query The command-line executable for the application. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWgnome-base-libs-devel | +-----------------------------+-----------------------------+ |Interface stability |Evolving | +-----------------------------+-----------------------------+ SEE ALSO
The pango documentation can be found at the following URL: file:///usr/share/gnome/devel-doc/pango NOTES
Written by Erwann Chenede, Sun Microsystems Inc., 2003. SunOS 5.10 8 Apr 2003 gobject-query(1)
All times are GMT -4. The time now is 04:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy