LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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-06-2019, 11:17 PM   #1
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

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-06-2019 at 11:29 PM.
 
Old 12-07-2019, 12:02 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,138

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
*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, 12:04 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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, 12:11 AM   #4
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

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, 12:36 AM   #5
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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, 12:47 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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, 01:04 AM   #7
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

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, 04:41 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,138

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

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



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 07: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 12:30 PM
Make multiple blank lines to a single lines kimhj3715 Programming 5 06-10-2012 10:35 AM
[SOLVED] CAT command | multiple lines to multiple lines udiubu Programming 11 10-28-2011 06:09 AM
replace several lines in a file with other lines in another file if condition yara Linux - General 12 10-27-2009 03:46 PM

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

All times are GMT -5. The time now is 10:13 PM.

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
Open Source Consulting | Domain Registration