extract text between two words on a single line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers extract text between two words on a single line
# 1  
Old 04-11-2011
MySQL extract text between two words on a single line

Hi Guys,

Can someone help me with a way to extract text between two words on a single line.

For example if the file has below content I want to extract all text between b and f inclusive of b and f. Aparently sed does this but does it line by line and I guess it cannot read word by word.

a b c d e f -> this is single line

Thanks!
# 2  
Old 04-11-2011
This code assumes that
1) there is only one occurrence of 'b' per line
2) that f is at the end of the line

Code:
sed 's/^[^b]*//' infile

---------- Post updated at 02:03 PM ---------- Previous update was at 02:00 PM ----------

Code:
$ echo "a b c d e f"
a b c d e f
$ echo "a b c d e f" | sed 's/^[^b]*//'
b c d e f
$ echo "a c b d e f" | sed 's/^[^b]*//'
b d e f
$ echo "a c d b e f" | sed 's/^[^b]*//'
b e f
$

---------- Post updated at 02:06 PM ---------- Previous update was at 02:03 PM ----------

more generic :
Code:
sed 's/^[^b]*\(b.*f\).*/\1/' infile

---------- Post updated at 02:09 PM ---------- Previous update was at 02:06 PM ----------

Or ... depending on your need :

Code:
$ echo "a b d b c f e f" | sed 's/^[^b]*\(b.*f\).*/\1/'
b d b c f e f
$ echo "a b d b c f e f" | sed 's/^[^b]*\(b[^f]*f\).*/\1/'
b d b c f
$ echo "a b d b c f e f" | sed 's/^.*\(b[^f]*f\).*/\1/'
b c f
$ echo "a b d b c f e f" | sed 's/^.*\(b.*f\).*/\1/'
b c f e f
$

# 3  
Old 04-11-2011
Code:
$ echo "a b c d e f g h i" | ruby -e 'puts gets.scan(/b.*?f/)'
b c d e f

# 4  
Old 04-12-2011
MySQL

Thanks Guys for the response. The sed command works for me Smilie

---------- Post updated 04-12-11 at 03:41 AM ---------- Previous update was 04-11-11 at 10:30 AM ----------

The sed workded for me on a sample file, but I was looking to implement this on an xml which has loads of data and looks like the same sed command that worked for me on a simple flat file doesn't work on this xml maybe due the amount of data. it simply doesn't return anything.

is there any other way to do it without sed ?
# 5  
Old 04-12-2011
Please provide a more representative sample of input you have and output you want .
(Ideally, upload your input file, and give us an example of output you need)
# 6  
Old 04-12-2011
I have attached the file that I am working on. I want the data between first <FEEDMessage> and the last </FEEDMessage> extracted (including the tags) and writen to another file on unix.
# 7  
Old 04-12-2011
See if this works for you:
Code:
sed -n '/<FEEDMessage>/,/<\/FEEDMessage>/p' inp_file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell - Read a text file with two words and extract data

hi I made this simple script to extract data and pretty much is a list and would like to extract data of two words separated by commas and I would like to make a new text file that would list these extracted data into a list and each in a new line. Example that worked for me with text file... (5 Replies)
Discussion started by: dandaryll
5 Replies

2. Shell Programming and Scripting

I need to extract uique words from text file

Hello programmers, I need to create a list of unique words from a text file using PERL...may i have the code for that please? Thank you (1 Reply)
Discussion started by: alsohari
1 Replies

3. Shell Programming and Scripting

Command line: add text wrapper around words

I am trying to build a sinkhole for BIND. I created a master zone file for malicious domains and created a separate conf file, but I am stuck. I have a list of known bd domains that is updated nightly. The file simply contains the list of domains, one on each line: Bad.com Bad2.com... (4 Replies)
Discussion started by: uuallan
4 Replies

4. UNIX for Dummies Questions & Answers

Append a line to single column text file

I would like to add a line to the end of a single column text file. How do I go about doing that? Input: BEGIN 1 2 3 Output: BEGIN 1 2 3 END Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

5. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

6. Shell Programming and Scripting

Extract X words from end of line, minus last keynumber X

The file contains one line of text followed by a number. I want to take the number X at the end, take it out and display the last X words. X is the key telling me how many words from the end that I want and X will always be less than the number of words, so no problem there. Example input and... (4 Replies)
Discussion started by: fubaya
4 Replies

7. Shell Programming and Scripting

Help substituting text in a file having a single line but no newline char

Hello, Need help substituting a particular word in a file having a single line but no newline character at the end. I was trying to use sed but it doesn't work probably because there is no newline char at the end of the line. $ cat hlq_detail /outputs/alvan23/PDFs/bills $ cat... (5 Replies)
Discussion started by: Shan_u2005
5 Replies

8. Shell Programming and Scripting

deleting blank line and row containing certain words in single sed command

Hi Is it possible to do the following in a single command /usr/xpg4/bin/sed -e '/rows selected/d' /aemu/CALLAUTO/callauto.txt > /aemu/CALLAUTO/callautonew.txt /usr/xpg4/bin/sed -e '/^$/d' /aemu/CALLAUTO/callautonew.txt > /aemu/CALLAUTO/callauto_new.txt exit (1 Reply)
Discussion started by: aemunathan
1 Replies

9. Shell Programming and Scripting

grep multiple words in a single line

Hi.. How to search for multiple words in a single line using grep?. Eg: Jack and Jill went up the hill Jack and Jill were best friends Humpty and Dumpty were good friends too ---------- I want to extract the 2nd statement(assuming there are several statements with... (11 Replies)
Discussion started by: anduzzi
11 Replies

10. Shell Programming and Scripting

Script to add a single line to middle of text file.

I've got a configuration file that is filled with xml text statements for example: <...../> <...../> <...../> <data id="java-options" value="-server -Djava.security.policy..../> <...../> <...../> <...../> I want to write a korn shell script that will go to this specific line and add a... (2 Replies)
Discussion started by: progkcp
2 Replies
Login or Register to Ask a Question