0

I have a file that has different lines of texts, I would like to check if the are duplicates of the same pattern.

In the file:

Blah
Blah
Depends: ssloader, firmware (>= 3.0), firmware (<= 6.0), apta
blah

my aim is to get ">= 3.0" & "<= 6.0" into a file. but bear in mind that sometimes there's only 1 "firmware" dependency.

What i have so far, only grabs the 1st firmware info:

if grep -Fq "firmware (" inputfile #checks if pattern exists
then
 compat=$(look 'Depends:' inputfile) #grab line where pattern is
 compat=${##*firmware (} #remove pattern and other stuff infront
 compat=${compat%%)*} #remove other stuff behind ")"
 echo $compat >> outputfile
fi

I would like to know how to check if there's more than 1 pattern in the same line. Or if there's more than 1 line with the same pattern, how to identify that line can get the firmware value. Thanks

EDIT:

My initial intention is to detect if there are more than one of the same pattern. I am open to ideas. :)

something like this:

if (more than one of same pattern)
 get both values #I am open to ideas to get this done <---
else
 get value of this pattern
fi

EDIT2:

I've got this working by doing it like this;

if grep -Fq "firmware (" ./control
then
    compat=$(look 'Depends:' control)
    compat=${compat#*firmware (}
    compat=${compat%%)*}
    echo -n $compat > ./compatibility.txt
    if [ $(grep -o "firmware (" ./control | wc -l) -eq 2 ]; then

    compat=$(look 'Depends:' control)
    compat=${compat##*firmware (}
    compat=${compat%%)*}
    echo " $compat" >> ./compatibility.txt
    fi
fi

I know it's definitely very layman, and it works only if the pattern is in the "Depends:" label.

Any ideas/inputs?

5
  • Is it always going to be firmware, or are you looking for any duplication in a Depends:, or any duplication in any labeled line? Commented Apr 27, 2012 at 3:43
  • Also, how many repeats might there be? Commented Apr 27, 2012 at 3:51
  • its gonna be firmware as at times, other labels like Pre-depends: might have the firmware info. Commented Apr 27, 2012 at 3:52
  • 1
    Ok, per discussion in comments below: do you want any occurrence, or multiple (apparently meaning 2?) occurrences? Commented Apr 27, 2012 at 4:23
  • @geekosaur +1 for "multiple (apparently meaning 2?)" Commented Apr 27, 2012 at 4:25

2 Answers 2

1

If it's alright to use sed:

sed -n '/firmware (/ { s/[^(]*(\(\([<>]=\|=\|[<>]\)\s\+[0-9]\+\(\.[0-9]\+\)*\))[^(]*/\1 /g; p }' file

Sample input:

Blah
Blah
Depends: ssloader, firmware (>= 3.0), firmware (<= 6.0), firmware (= 5.0), apta
Depends: ssloader, firmware (>= 3.0), firmware (<= 6.0), apta
Depends: ssloader, firmware (<= 6.0), apta
blah

Sample Output:

>= 3.0 <= 6.0 = 5.0 
>= 3.0 <= 6.0 
<= 6.0
Sign up to request clarification or add additional context in comments.

2 Comments

i really like this, but can it be enhanced so that if only takes the value of 'firmware' only? Depends: ssloader, firmware (>= 3.0), subr (0.222-1), firmware (< 6.0), apta. does it work if 'firmware (> 6.0)'? (notice there's no '=')
@AlwynIsPat See my edits. I used a couple of gsed extentions to make it shorter so let me know if you don't have gsed and I'll use the longer version.
1

Another sed version, which might work better depending on what you're doing:

sed -n 's/.* firmware (\([^)]*\)),.* firmware (\([^)]*\)),.*$/\1 \2/p'

(It is relatively easy to generalize this to multiple packages, by the way.)

5 Comments

This will fail if there is only one instance of firmware ( on the line. Maybe you could make the second one optional if you take out the .*.
My reading of "more than one pattern" doesn't include 1. Should it? (The question seems to be specifically looking for the repeated information.)
Hmm, maybe so. I read "the most repeats, 2." to mean \{1,2\} in sed speak. It also appears that their program already accommodates the case where there is one, but they would like it to accommodate up to two. Perhaps the OP should clarify.
i've edited above of how my initial idea of how this code should run. @TimPote is right, I am already able to do if its only one. my case here is, if it happens to be 2 of the same pattern, how can i get both values?
@AlwynIsPat My answer should work for you then. See my changes to see how it reacts to 1, 2, or more version patterns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.