LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Search multiple lines in a file (https://www.linuxquestions.org/questions/linux-newbie-8/search-multiple-lines-in-a-file-4175665549/)

cbtshare 12-06-2019 11:17 PM

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":
                "ParameterValue":
                "Description":
                "Source": "
                "ApplyType":
                "DataType":
                "AllowedValues":
                "IsModifiable":

The file also has patterns with no ParameterValue i.e

Code:

"ParameterName":
                "Description":
                "Source": "
                "ApplyType":
                "DataType":
                "AllowedValues":
                "IsModifiable":

Is there a way I can get only content that corresponding ParameterName AND ParameterValue

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
gets both, but it get ParameterName ONLY values as well, they dont match
Code:

grep -e ParameterName -e ParameterValue dbparam2
Shows nothing
Code:

awk '/ParameterName/ && /ParameterValue/' dbparam2
Please assist me, thank you

syg00 12-07-2019 12:02 AM

*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.

grail 12-07-2019 12:04 AM

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?

cbtshare 12-07-2019 12:11 AM

Quote:

Originally Posted by grail (Post 6065482)
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?

Yes, ParameterName always precedes ParameterValue , but there are also line without ParameterValue and those lines are what I do not want.
An example is


Code:

              "ParameterName": "tmpdir",
                "ParameterValue": "/rdsdbdata/tmp/",
                "Description": "The directory used for temporary files and temporary tables",
                "Source": "system",
                "ApplyType": "static",
                "DataType": "string",
                "IsModifiable": false

a desired output would be just to show

Code:

"ParameterName": "tmpdir",
 "ParameterValue": "/rdsdbdata/tmp/",

or even the whole lines

Code:

              "ParameterName": "tmpdir",
                "ParameterValue": "/rdsdbdata/tmp/",
                "Description": "The directory used for temporary files and temporary tables",
                "Source": "system",
                "ApplyType": "static",
                "DataType": "string",
                "IsModifiable": false


berndbausch 12-07-2019 12:36 AM

Quote:

Originally Posted by cbtshare (Post 6065473)
Shows nothing
Code:

awk '/ParameterName/ && /ParameterValue/' dbparam2

Because this awk program finds lines that contain both.

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 }
/ParameterValue/ { print PN; print }


grail 12-07-2019 12:47 AM

SO maybe something like:
Code:

awk '/ParameterName/{out = $0; getline; if(/ParameterValue/)print out RS $0}' file_name

cbtshare 12-07-2019 01:04 AM

Quote:

Originally Posted by grail (Post 6065494)
SO maybe something like:
Code:

awk '/ParameterName/{out = $0; getline; if(/ParameterValue/)print out RS $0}' file_name

amazing, thank you !

syg00 12-07-2019 04:41 AM

What's wrong with KISS ?.
Code:

grep -B1 ParameterValue input.file

MadeInGermany 12-07-2019 04:49 AM

In sed the N command appends the next line.
Code:

sed '/"ParameterName":/!d;N;/"ParameterValue":/!d' file_name
Delete if not "ParameterName": otherwise N, delete if not "ParameterValue": otherwise default-print.
Or
Code:

sed -n '/"ParameterName":/{N;/"ParameterValue":/p;}' file_name
No default-print. If "ParameterName": then N, if "ParameterValue": then print.


All times are GMT -5. The time now is 06:41 AM.