0

I am currently writing a bash script to grep out certain lines as shown in bold. However when I do a grep 'number', all the irrelevant lines will appear as well.

Is there a way to just grep out the bolded lines?
Or is grep not the command to use?

callingNumber = SEQUENCE
typeOfNumber = national
numberingPlan = isdnTelephony
numberPresentationStatus = allowed
number = '12345678'
calledNumber = SEQUENCE
typeOfNumber = national
number = '897654321'
addressNumber = SEQUENCE
typeOfNumber = national
number = '897654321'
contactedNumber = SEQUENCE
typeofNumber = 'national'
number = '8888888'
anotherNumber = SEQUENCE
typeOfNumber = international
numberingPlan = isdnTelephony
number = '$$$$$$$$'

1 Answer 1

1

You can use grep with Regex expressions, depending on how complex you want to get and your exact match requirements. The last line is a bit of a curveball, and it depends somewhat on what the requirements are. A fairly naive approach that worked for this specific data set was

 cat del.me | grep -E "callingNumber|contactedNumber|number = '[0-9]{6}|calledNumber = " | uniq

This pulled everything matching callingNumber,number and calledNumber (because of case sensitivity). For "number" it matched anything which started with a single quote followed by at least 6 numbers - which filtered out the last string.

Result:

callingNumber = SEQUENCE
number = '12345678'
calledNumber = SEQUENCE
number = '897654321'
contactedNumber = SEQUENCE
number = '8888888'
5
  • however, this does not filter out the extra "number" line after "addressNumber" which lies the crux of my problem. I was thinking of using -A 2 to get the "number" under "calledNumber" but I'm unable to append options to individual patterns.
    – CurrySauce
    Commented Apr 8, 2020 at 8:29
  • I missed that. (Using del.me as a file above), I modified the output by piping it through uniq. - Uniq filters out adjacent matching lines.
    – davidgo
    Commented Apr 8, 2020 at 8:39
  • I have another question, what if the number :$$4$$$$$ was another string instead, for example "44444444", how would you get rid of this line then?
    – CurrySauce
    Commented Apr 8, 2020 at 11:33
  • I wouldn't use grep. There may be ways to do it but my regex too is weak. I'd write a little program to read and parse the file.
    – davidgo
    Commented Apr 8, 2020 at 11:50
  • If I were wanting to do it with just Linux tools I'd pass the output of my previous command through sed to create lines with both sequence and number in one line, then grep -v anothernumber to strip unwanted lined then split it back up.
    – davidgo
    Commented Apr 8, 2020 at 11:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .