Pattern matching & storing result in variable

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern matching & storing result in variable
# 1  
Old 08-04-2011
Data Pattern matching & storing result in variable

i'm trying to parse textfiles against a pattern and storing the result in a variable.
The strings i want to get are embraced by [" and "] and can occur several times in one line, so e.g.
some text ["i want this string"] anything else ["and this one too"] end

what i have so far:
for f in $*
    exec 3<&0
    exec 0<$f
    while read line
        S=`echo $line | sed -n '/\["\([^"]*\)"\]/p'`
        # do something with S

but this code gets the whole lines!
i've also tried epxr, but then i only get the first match.

please, help me!
# 2  
Old 08-04-2011
I think this example can help:
some text ["i want this string"] anything else ["and this one too"] end
some text ["i want this string"] 

cat INPUTFILE | sed 's/\[/\n[/g; s/\]/]\n/g' | sed -n '/^\[\(.*\)\]$/ s//\1/p'
"i want this string"
"and this one too"
"i want this string"

PS It's "useful use of cat".
This User Gave Thanks to yazu For This Post:
# 3  
Old 08-04-2011
thanks a lot, it works fine!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

2. Shell Programming and Scripting

Pattern Matching & replacing of content in file1 with file2

I have file 1 & file 2 with content mentioned below. I want to get the output as shown in file3. Requirement: check the content of column 1 & column 2, if value of column 1 in file1 matches with first column of file2 then remaining columns(2&3) of file2 should get replaced, also if value of... (4 Replies)
Discussion started by: siramitsharma
4 Replies

3. Shell Programming and Scripting

PERL : pattern matching a string stored in a variable

I have two variables, my $filename = "abc_yyyy_mm_dd.txt"; my $filename1 = " abc_2011_11_07.txt"; I need to perform some operations after checking if $filename has $filename1 in it i have used the below code, if($filename =~ /^$filename1/) { ---- -- } (2 Replies)
Discussion started by: irudayaraj
2 Replies

4. Shell Programming and Scripting

AWK - Pattern Matching & Replacing - Performance

Experts, I am a beginner to Unix Shell Scripting We have source as a flat file which contains CTRL+F character as the delimiter. We need to count the number of records in the file (CTRL+F) to perform file validation Following command being used: awk '{cnt+=gsub(//,"&")}END {print cnt}'... (4 Replies)
Discussion started by: srivijay81
4 Replies

5. Shell Programming and Scripting

storing output from echo & cut into variable

Hi All, Hope someone can advise here as I have been struggling to find a syntax that works here. I have tried a stack of combination I have seed in the forums but I think because I have needed to use "" and `` in the statments another method is found. I am reading in lines with the following... (1 Reply)
Discussion started by: nkwilliams
1 Replies

6. Shell Programming and Scripting

matching a pattern in a file & prefixing a word

Hi, In my shell script i have to match a patten in a file , if found i have to prefix the entair line by a "word" eg. pattern = "aaa" prefix= #123 file: bbbb xxx zzzz aaaa qqqq kkkk outPut file: bbbb xxx ... (5 Replies)
Discussion started by: shivarajM
5 Replies

7. UNIX for Dummies Questions & Answers

Pattern matching New to Sed & Awk

Hello, Despite reading the Pattern Matching chapter in the O'Reilly Sed & Awk book several times and looking at numerous examples, I cannot seem to get any kind of conditional script to work in my awk scripts! I am able to do the basic awk and grep script to capture the data but when I do with... (0 Replies)
Discussion started by: pg55
0 Replies

8. Shell Programming and Scripting

storing result of a command in variable

For whatever reason I cant seem to fix my syntax to do the following. I want to run a grep and count how many instances come up and store that number in a variable but I keep erroring out. Here's my code in bash: number=grep blah file.txt | wc -l (1 Reply)
Discussion started by: eltinator
1 Replies

9. Shell Programming and Scripting

Problem storing SSH result in a variable

i have this SSH command which runs perfectly on command prompt in sunOS ssh -o Port=${portno} ${uname}@${server} find ${dir_path} -name '***' output : /usr/local/home/*** My problem is when i run same command in my script #!/usr/bin/ksh res=`ssh -o Port=${portno} ${uname}@${server}... (1 Reply)
Discussion started by: prash184u
1 Replies

10. Shell Programming and Scripting

Perl: Pattern Matching a HASH variable

Hi All, I'm trying to test a Hash variable but it's not working. Here is my code - can anyone tell me if the test is valid? for (keys %enabled_yn) { if ($enabled_yn{$1} =~ m/\s+Y/) { $html =~ s/%(\w+)%/\<b\>\<font color\=orange\>$enabled_yn{$1}\<\/font\>\<\/b\>/g; }... (1 Reply)
Discussion started by: pondlife
1 Replies
Login or Register to Ask a Question

Featured Tech Videos