Extracting a string matching a pattern from a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting a string matching a pattern from a line
# 1  
Old 05-11-2010
Extracting a string matching a pattern from a line

Hi All,

I am pretty new to pattern matching and extraction using shell scripting. Could anyone please help me in extracting the word matching a pattern from a line in bash.

Input Sample (can vary between any of the 3 samples below):
1) Adaptec SCSI RAID 5445
2) Adaptec SCSI 5445S RAID
3) ICP ICP5085BL SATA RAID

Expected Output:
1) 5445
2) 5445S
3) ICP5085BL

I tried various options and zero'ed in with the below sed
Code:
echo "$1" | sed 's/.*\([0-9]\{4\}.*\)/\1/'


Though the above script works for the first 2 inputs it does not work for the third option. It gives only 5085BL instead of ICP5085BL. Any suggestions would be deeply appreciated.

Last edited by jharish; 05-11-2010 at 12:32 PM.. Reason: have put the script inside [CODE] syntax
# 2  
Old 05-11-2010
try this..

Code:
echo "Adaptec SCSI RAID 5445" | awk '{print $4}'

echo "Adaptec SCSI 5445S RAID" | awk '{print $3}'

echo "ICP ICP5085BL SATA RAID" | awk '{print $2}'

# 3  
Old 05-11-2010
What do you want to achieve? Looks like you want to match strings containing 4 digits Smilie
# 4  
Old 05-11-2010
try:

Code:
#!/bin/bash
input=$1
echo ${input}                      # echos the full input
echo ${input//[0-9]/}              # echos the digits found within the input

This assumes all digits are together within the input.

edit...
Sorry, I didn't pay attention... this only extracts the numbers.
# 5  
Old 05-11-2010
Hi Tuxidow,

Thanks for your reply. But I need one single script to answer the three inputs as "$1" can either of them depending on the available RAID controller

Quote:
Originally Posted by Tuxidow
try this..

Code:
echo "Adaptec SCSI RAID 5445" | awk '{print $4}'

echo "Adaptec SCSI 5445S RAID" | awk '{print $3}'

echo "ICP ICP5085BL SATA RAID" | awk '{print $2}'

Hi pseudocoder,

I am trying to extract the complete word that contains 4 digits.

The RAID controller model number can be 5445 or 5445S or ICP5085BL or someother valid Adaptec model.

Quote:
Originally Posted by pseudocoder
What do you want to achieve? Looks like you want to match strings containing 4 digits Smilie
# 6  
Old 05-11-2010
Try this...

Code:
#!/bin/bash
input=( $* )
for w in ${input[@]}; do
    if [ ! ${w//[0-9]*/} ]; then
      echo $w
    fi
done

# 7  
Old 05-11-2010
Quote:
Originally Posted by jharish
I am trying to extract the complete word that contains 4 digits.
Code:
[^ ]*[0-9][0-9][0-9][0-9][^ ]*

should be the proper regex.

Code:
$ cat sedinput
1) Adaptec SCSI RAID 5445
2) Adaptec SCSI 5445S RAID
3) ICP ICP5085BL SATA RAID
$ grep -o "[^ ]*[0-9][0-9][0-9][0-9][^ ]*" sedinput
5445
5445S
ICP5085BL
$

or
Code:
$ echo "1) Adaptec SCSI RAID 5445" | grep -o "[^ ]*[0-9][0-9][0-9][0-9][^ ]*"
5445
$ echo "2) Adaptec SCSI 5445S RAID" | grep -o "[^ ]*[0-9][0-9][0-9][0-9][^ ]*"
5445S
$ echo "3) ICP ICP5085BL SATA RAID" | grep -o "[^ ]*[0-9][0-9][0-9][0-9][^ ]*"
ICP5085BL
$

Let me know if you by all means want to do it with sed and if you fail building appropriate sed command with the mentioned regex.
This User Gave Thanks to pseudocoder For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

2. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

3. UNIX for Dummies Questions & Answers

Extracting sub-string matching the pattern.

Hi, I have a string looks like the following: USERS 32767.9844 UNDOTBS1 32767.9844 SYSAUX 32767.9844 SYSTEM 32767.9844 EMS 8192 EMS 8192 EMS_INDEXES 4096 EMS_INDEXES 4096 8 rows selected. How do I extract a sub-string to get the expected output as following: EMS 8192 EMS_INDEXES 4096 ... (3 Replies)
Discussion started by: NetBear
3 Replies

4. Shell Programming and Scripting

Pattern Matching and extracting the required fields in Perl

Hi All, I am writing the following Perl Scrip and need your help in Pattern matching : I have the following Shell Script that would read line by line from the file (file_svn) and would inturn calls the Perl Script: #!/bin/bash perl_path="/home/dev/filter"... (2 Replies)
Discussion started by: filter
2 Replies

5. Shell Programming and Scripting

Korn Shell for pattern matching and extracting

Guys, i'm new to shell scripting. Here's what i need. I need a shell script which would read a file containing only 1 line which never changes. File containts - SQL_Mgd_Svc_ELONMCL54496 |EMEA\brookkev, EMEA\fieldgra, EMEA\tidmamar, EMEA\attfiste, EMEA\baldogar, EMEA\clarkia2, EMEA\conwasha,... (9 Replies)
Discussion started by: butterfly20
9 Replies

6. Shell Programming and Scripting

Extracting the strings matching a pattern from a word

Hi All , I need to extract the strings that are matching with the pattern : CUST.<AnyStringOfAnyLength>.<AnyStringOfAnyLength> from a file and then write all these string into another file. e.g. If a file SOURCE contains following lines : IF(CUST.ABCD.EFGH==1) THEN CUST.ABCD.EFGH =... (7 Replies)
Discussion started by: swapnil.nawale
7 Replies

7. Shell Programming and Scripting

help extracting a matching pattern and next lines of match

Hi there, i'm having some problems just making an awk script (i've tried this way, but other way can be posible for sure), for the next file file.txt <register> <createProfile> <result>0</result> <description><!]></description> <msisdn>34661461174</msisdn> <inputOmvID>1</inputOmvID>... (6 Replies)
Discussion started by: vicious
6 Replies

8. Shell Programming and Scripting

Problem extracting just a part of a matching pattern

Hello everyone, this is my first post so please give me a hand. I apologize for my English, I'll try to be clear with my request. I need to write a script (Bash) which finds all the variables defined in the file .h of the folder and then writes the name of the files .c where these variables are... (1 Reply)
Discussion started by: paxilpaz
1 Replies

9. Shell Programming and Scripting

Pattern matching extracting urls from rss, shell scripts

Hi all, how could i do ? I have a Rss file, i want to extract only the Urls (many) matching http://www.xxx.com/trailers/ from that file and copy into another file. like " <pubDate>Wed, 29 Apr 2009 00:00:00 PST</pubDate> <content:encoded><!Apple - Movie Trailers - The Hangover"><img... (3 Replies)
Discussion started by: BremboloIV
3 Replies

10. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies
Login or Register to Ask a Question