Parse String Using Sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse String Using Sed
# 1  
Old 04-23-2008
Parse String Using Sed

Hi,

I am wondering if there's a simpler way to extract the second occurrence of a word enclosed in [ ] that matches my search criteria.

Sample Input is as follows:

HTML Code:
[main] Error installing feature [fmx] - com.er.nms.cif.ist.NoMatchingUpgra
[main] Error installing feature [fmav] - com.er.nms.cif.ist.NoMatchingUpgra
Based on the Input, I need to get the following output:
HTML Code:
fmx 
fmav

Here's my code:

Code:
while read data; do
echo $data | sed -e 's/\([.[a-z]*] \)//' | sed 's/[A-Za-z ]*//' | sed 's/-.*//' | sed 's/]//' | sed 's/\[//'
done < data.txt

Any help is greatly appreciated.


//racbern
# 2  
Old 04-23-2008
Code:
sed -e 's/.*\].*\[//' -e 's/\].*//' data.txt

The first substitution matches any string, closing square bracket (to skip past the first pair of square brackets), anything, followed by opening square bracket, and removes that. This should remove everything before your string. Then the second substitution replaces from closing square bracket to end of line with nothing.

As a side remark, you should always double quote any variables.
# 3  
Old 04-23-2008
echo $str | sed -e 's/^\[.*\[//' -e 's/].*$//'
# 4  
Old 04-23-2008
Code:
echo '[main] Error installing feature [fmx] - com.er.nms.cif.ist.NoMatchingUpgra' | nawk -F'[][]' '{print $4}'

# 5  
Old 04-23-2008
Hi,

Thanks guys for your help.

//racbern
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl parse string

Hi Perl Guys I have another perl question I have the following code that i have written Getopt::Long::config(qw( permute bundling )); my $OPT = {}; GetOptions($OPT, qw( ver=s help|h )) or die "options parsing failed"; This will allow the user to do something like... (4 Replies)
Discussion started by: ab52
4 Replies

2. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

3. Shell Programming and Scripting

parse a mixed alphanumeric string from within a string

Hi, I would like to be able to parse out a substring matching a basic pattern, which is a character followed by 3 or 4 digits (for example S1234 out of a larger string). The main string would just be a filename, like Thisis__the FileName_S1234_ToParse.txt. The filename isn't fixed, but the... (2 Replies)
Discussion started by: keaneMB
2 Replies

4. Shell Programming and Scripting

Use awk or sed to parse delimited string

Hi I am trying to figure out the best way to search a long log file and print out certain information. For example if I had a line in a log file delimited by ampersand first_name=mike&last_name=smith&zip_code=55555&phone=555-5555&state=ma&city=boston and I only wanted to search for and... (3 Replies)
Discussion started by: mstefaniak
3 Replies

5. Shell Programming and Scripting

Parse a string as a command

I've a problem parsing a string as a command: Consider script stefano.sh as following: #!/usr/bin/sh txtshell="./parser.sh /ews/MyEventHandler/data/handler/StopAndMail.php eventid=StopAndMail.MVIN.6300 lot_number=1122FXB facility=EWSF3 'mailto=prova.prova@nohost.com, prova.test@nohost.com'... (2 Replies)
Discussion started by: buonstefano
2 Replies

6. Shell Programming and Scripting

Parse string

Hi, I need to parse a string, check if there are periods and strip the string. For example i have the following domains and subdomains: mydomain.com, dev.mydomain.com I need to strip all periods so i have a string without periods or domain extensions: mydomain, devmydomain. I use this for... (12 Replies)
Discussion started by: ktm
12 Replies

7. Shell Programming and Scripting

How to parse a string into variables

I'm working in korn shell and have a variable which contains a string like: aa_yyyymmdd_bbb_ccc_ddd.abc. I want to treat the _ and . as delimiters and parse the string so I end up with 6 values in variables that I can manipulate. My original plan was to use var1=`echo $sting1 | cut -c1-c2` but... (9 Replies)
Discussion started by: aquimby
9 Replies

8. Shell Programming and Scripting

Parse String from a Variable

Hello, Is there a quick way to parse the values from a variable? The variable has the following sample input: TA= The values of the TA variable is not fixed/hardcoded Basically I need to get the IV_Test and PF_SAPP_FWK values. I created a script that first use sed to remove ,... (3 Replies)
Discussion started by: racbern
3 Replies

9. Shell Programming and Scripting

how to parse this string

I want to get filenames from the following input. How can I parse this in bash. input data ------------------------------------------------------------------- path=/aaa/bbb/filename1;/aaa/filename2;/aaa/bbb/ccc/ddd/filename3 -------------------------------------------------------------------... (13 Replies)
Discussion started by: hcliff
13 Replies

10. Shell Programming and Scripting

parse a string variable

Hello all, need a little help. I have an input variable such as ARGV which equals something like /use/home/name/script/test.dat I need to be able to get just the "test.dat" (i.e. the file name) at the end of the directory and the directory can be anything and any length. To put it another... (3 Replies)
Discussion started by: methos
3 Replies
Login or Register to Ask a Question