Search multiple lines in a file
Hello All,
I have a file with a particular pattern that I want.(with the title ParameterValue) Code:
"ParameterName": Code:
"ParameterName": The values are in a file calle dbparam2 I have tried the following: Only gets ParameterValue, but I need the name Code:
grep -- 'ParameterValue*' dbparam2 Code:
grep -e ParameterName -e ParameterValue dbparam2 Code:
awk '/ParameterName/ && /ParameterValue/' dbparam2 |
*nix is stream orientated - a record ends at the newline. So by definition you can't test for multiple values across newlines - but we've all tried. Well, you can even with grep, but the regex can get monstrous. There is (or was) a multiline grep available - same comment re the regex.
If the ParameterValue is always immediately after ParameterName, search for the former and specify you want 1 line of context before the match. See the manpage. Easy. If not, you'll need to flag when you hit ParameterName and keep looking - awk is the better tool then. |
Please show a full example, ie including the 'name'. Also, include the desired output
As for the information, will ParameterValue always follow ParameterName or can it be anywhere in the list of values? |
Quote:
An example is Code:
"ParameterName": "tmpdir", Code:
"ParameterName": "tmpdir", Code:
"ParameterName": "tmpdir", |
Quote:
You could create an awk program that remembers ParameterName and only outputs it when it encounters ParameterValue, something like (not tested): Code:
/ParameterName/ { PN = $0 } |
SO maybe something like:
Code:
awk '/ParameterName/{out = $0; getline; if(/ParameterValue/)print out RS $0}' file_name |
Quote:
|
What's wrong with KISS ?.
Code:
grep -B1 ParameterValue input.file |
In sed the N command appends the next line.
Code:
sed '/"ParameterName":/!d;N;/"ParameterValue":/!d' file_name Or Code:
sed -n '/"ParameterName":/{N;/"ParameterValue":/p;}' file_name |
All times are GMT -5. The time now is 06:41 AM. |