Extract words starting with a pattern from a file

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract words starting with a pattern from a file
# 1  
Old 09-21-2013
Extract words starting with a pattern from a file

Hi Guys..

I have a file and i want to extract all words that starts with a pattern 'ABC_' or 'ADF_'
For example,


Output :

Hope the query is clear.
Thanks in Advance.

Last edited by Don Cragun; 09-21-2013 at 01:51 PM.. Reason: Change COLOR tags to CODE tags and add missing CODE tags
# 2  
Old 09-21-2013
perl -lne 'push @w, /\bA(?:BC|DF)_\w+/g; END{ print join ", ", @w if @w}' ABC.txt

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 09-21-2013
Another approach in bash:

while read line
        for word in $line
                if [[ "$word" =~ ^ABC_ ]] || [[ "$word" =~ ^ADF_ ]]
                        [ -z "$str" ] && str="$word" || str="${str},${word}"
done < ABC.txt

echo "$str"

# 4  
Old 09-21-2013
Note that you said that you wanted the output:

but you said you wanted to match ABC_; not ABS_ and ABS_ does not appear in your sample input.

If the strings you want to match could contain characters that have special meanings in regular expressions or if you want varying strings with different invocations of your script, here is a way to do it using awk:
printf '%s\n' 'ABC_' 'ADF_' | awk '
FNR == NR {
        # Read patterns to be matched from standard input.
        p[++pc] = $0
{       # For every field in the current line...
        for(i = 1; i <= NF;i++)
                # If this field has not already been printed...
                if(!($i in m))
                        # See if this field matches any of the given patterns..
                        for(j = 1; j <= pc; j++)
                                if(substr($i, 1, length(p[j])) == p[j]) {
                                        printf("%s%s", mc ? ", " : "", $i)
                                        mc = 1
END {   printf("%s\n", mc ? "" : "No matches found.");
}' - ABC.txt

As shown, this script will only print one occurrence of each matched word. If you delete the lines shown in red, it will print every occurrence of each matched word. (Your sample input didn't contain duplicates of any words starting with the strings to be matched, but if you duplicate the input, you'll see the difference.)

As always, if you want to try this on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.
# 5  
Old 09-21-2013
Thanks Elixir and Don for the help, the code is working perfectly fine.
I apologise for my mistake Don, it should be ABC_ or ADF_. But i got your point too.

Smilie Smilie Smilie Smilie
# 6  
Old 09-21-2013
A sed version:
sed ':deb;${s/,\|\n/ /g;s/ \(ABC\|ADF\)/,\1/g;s/[^,]*\(,[^ ]*\)/\1/g;s/^,\| .*//g;b};N;bdeb' ABC.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep file starting from pattern matching line

I have a file with a list of references towards the end and want to apply a grep for some string. text .... @unnumbered References @sp 1 @paragraphindent 0 2017. @strong{Chalenski, D.A.}; Wang, K.; Tatanova, Maria; Lopez, Jorge L.; Hatchell, P.; Dutta, P.; @strong{Small airgun... (1 Reply)
Discussion started by: kristinu
1 Replies

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

3. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

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

5. Shell Programming and Scripting

To extract a string between two words in XML file

i need to extract the string between two tags, input file is <PersonInfoShipTo AddressID="446311709" AddressLine1="" AddressLine2="" AddressLine3="" AddressLine4="" AddressLine5="" AddressLine6="" AlternateEmailID="" Beeper="" City="" Company="" Country="" DayFaxNo="" DayPhone="" Department=""... (5 Replies)
Discussion started by: Padmanabhan
5 Replies

6. UNIX for Dummies Questions & Answers

Extract words to new file

Hi there, Unix Gurus Working with big listings of english sentences for my pupils, of the type: 1. If the boss's son had been , someone would have asked for money by now. 2. Look, I haven't a crime, so why can't you let me go? .... I wondered how to extract the words between brackets in... (7 Replies)
Discussion started by: eldeingles
7 Replies

7. UNIX for Dummies Questions & Answers

To Extract words from File based on Position

Hi Guys, While I was writing one shell script , I just got struck at this point. I need to extract words from a file at some specified position and do some comparison operation and need to replace the extracted word with another word. Eg : I like Orange very much. I need to replace... (19 Replies)
Discussion started by: kuttu123
19 Replies

8. Shell Programming and Scripting

Searching words in a file containing a pattern

Hi all, I would like to print words in a file seperated by whitespaces containing a specific pattern like "=" e.g. I have a file1 containing strings like %cat file1 The= some= in wish= born <eof> .I want to display only those words containing = i.e The= , some=,wish= ... (5 Replies)
Discussion started by: sree_123
5 Replies

9. Shell Programming and Scripting

Extract words before and after a pattern/regexp

Couldn't find much help on the kind of question I've here: There is this text file with text as: Line one has a bingo Line two does not have a bingo but it has a tango Bingo is on line three Line four has both tango and bingo Now I would want to search for the pattern "bingo" in this file... (3 Replies)
Discussion started by: manthasirisha
3 Replies

10. Programming

getting file words as pattern matching

Sir, I want to check for the repation of a user address in a file i used || as my delimiter and want to check repetaip0n of the address that is mailid and then i have to use IMAP and all. How can i do this... I am in linux ...and my file is linux file. ... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies
Login or Register to Ask a Question