2

I have a Linux driver running in the background that is able to return the current system data/stats. I view the data by running a console utility (let's call it dump-data) in a console. All data is dumped every time I run dump-data. The output of the utility is like below

Output:
- A=reading1
- B=reading2
- C=reading3
- D=reading4
- E=reading5
...
- variableX=readingX
...

The list of readings returned by the utility can be really long. Depending on the scenario, certain readings would be useful while everything else would be useless.

I need a way to grep only the useful readings whose names might have have nothing in common (via a bash script). I.e. Sometimes I'll need to collect A,D,E; and other times I'll need C,D,E.

I'm attempting to graph the readings over time to look for trends, so I can't run something like this:

# forgive my pseudocode
Loop
    dump-data | grep A
    dump-data | grep D
    dump-data | grep E
End Loop

to collect A,D,E as that would actually give me readings from 3 separate calls of dump-data as that would not be accurate.

4 Answers 4

5

If you want to save all result of grep in the same file, you can just join all expressions in one:

grep -E 'expr1|expr2|expr3'

But if you want to have results (for expr1, expr2 and expr3) in separate files, things are getting more interesting.

You can do this using tee >(command).

For example, here I process the same pipe with thre different commands:

$ echo abc | tee >(sed s/a/_a_/ > file1) | tee >(sed s/b/_b_/ > file2) | sed s/c/_c_/ > file3
$ grep "" file[123]
file1:_a_bc
file2:a_b_c
file3:ab_c_

But the command seems to be too complex.

I would better save dump-data results to a file and then grep it.

TEMP=$(mktemp /tmp/dump-data-XXXXXXXX)
dump-data > ${TEMP}
grep A ${TEMP}
grep B ${TEMP}
grep C ${TEMP}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer! grep -E worked fine. I selected your answer as it offered a little more than the rest. I did think of your last solution, but as I might take readings pretty often it might be a little inefficient.
yes of course, but when you need separate output files for each expression, it is inevitable :) when not, of course, not :)
3

You can use dump-data | grep -E "A|D|E". Note the -E option of grep. Alternatively you could use egrep without the -E option.

Comments

2

you can simply use:

dump-data | grep -E 'A|D|E'

Comments

0
awk '/MY PATTERN/{print > "matches-"FILENAME;}' myfile{1,3}

thx Guru at Stack Exchange

Comments

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.