LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-07-2019, 12:17 AM   #1
cbtshare
Member
 
Registered: Jul 2009
Posts: 641

Rep: Reputation: 42
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

Last edited by cbtshare; 12-07-2019 at 12:29 AM.
 
Old 12-07-2019, 01:02 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,486

Rep: Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099
*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.
 
Old 12-07-2019, 01:04 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,769

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
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?
 
Old 12-07-2019, 01:11 AM   #4
cbtshare
Member
 
Registered: Jul 2009
Posts: 641

Original Poster
Rep: Reputation: 42
Quote:
Originally Posted by grail View Post
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
 
Old 12-07-2019, 01:36 AM   #5
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: A few
Posts: 4,129

Rep: Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159
Quote:
Originally Posted by cbtshare View Post
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 }
 
Old 12-07-2019, 01:47 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,769

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
SO maybe something like:
Code:
awk '/ParameterName/{out = $0; getline; if(/ParameterValue/)print out RS $0}' file_name
 
1 members found this post helpful.
Old 12-07-2019, 02:04 AM   #7
cbtshare
Member
 
Registered: Jul 2009
Posts: 641

Original Poster
Rep: Reputation: 42
Quote:
Originally Posted by grail View Post
SO maybe something like:
Code:
awk '/ParameterName/{out = $0; getline; if(/ParameterValue/)print out RS $0}' file_name
amazing, thank you !
 
Old 12-07-2019, 05:41 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,486

Rep: Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099
What's wrong with KISS ?.
Code:
grep -B1 ParameterValue input.file
 
1 members found this post helpful.
Old 12-07-2019, 05:49 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,289

Rep: Reputation: 592Reputation: 592Reputation: 592Reputation: 592Reputation: 592Reputation: 592
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
loop replace specific lines from a file with lines from another file . dr.x Programming 7 09-02-2019 08:18 AM
LXer: How To Empty a File, Delete N Lines From a File, Remove Matching String From a File, And Remove Empty/Blank Lines From a File In Linux LXer Syndicated Linux News 0 11-22-2017 01:30 PM
Make multiple blank lines to a single lines kimhj3715 Programming 5 06-10-2012 11:35 AM
[SOLVED] CAT command | multiple lines to multiple lines udiubu Programming 11 10-28-2011 07:09 AM
replace several lines in a file with other lines in another file if condition yara Linux - General 12 10-27-2009 04:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration