LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-07-2020, 11:01 PM   #1
eng_mohammedmostafa
Member
 
Registered: Nov 2008
Location: Egypt
Posts: 110
Blog Entries: 1

Rep: Reputation: 16
Grep last occurrence word till end of line


Hi All,

Please, I need to grep last occurrence word till end of line.

Here is the file content:
[[{"configKey":"Parm1",{"configKey":"Parm2",{"configKey":"Parm3"]]

How to return only this please?
{"configKey":"Parm3"]]

Regards,
Mohamed
 
Old 04-07-2020, 11:17 PM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Code:
grep -Po '.*Parm2",\K.*'
 
Old 04-08-2020, 12:48 AM   #3
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
The text looks ill-formed. Where are the closing braces?

shruggy's expression only works if you know the string on the left of the last configkey. If this code is JSON, try a JSON tool like jq or pipe it through a pretty-printer like Python's json.tool. But again, it looks ill-formed.

sed instead of grep:
Code:
echo '[[{"configKey":"Parm1",{"configKey":"Parm2",{"configKey":"Parm3"]]' | sed 's/.*\({"configKey.*\)/\1/'

Last edited by berndbausch; 04-08-2020 at 12:53 AM. Reason: added sed
 
Old 04-08-2020, 02:09 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
yes, sed is better here.
If you want to use grep you need to be more explicit:
Code:
grep -o '{"configKey":"[^"]*"[]]*$'
This will work on the example you posted, but most probably will fail on a real json file.
you need to use jq/perl/python to handle it properly.
 
Old 04-08-2020, 10:30 AM   #5
eng_mohammedmostafa
Member
 
Registered: Nov 2008
Location: Egypt
Posts: 110

Original Poster
Blog Entries: 1

Rep: Reputation: 16
Thanks for your feedback


But let me clarify please that (Parm1,Parm2 and Parm3) are variables .. I just provided them as examples.
The only common word between all of them is: {"configKey":"

So, I need to grep last {"configKey":" till the end of the line.
 
Old 04-08-2020, 11:36 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by eng_mohammedmostafa View Post
Thanks for your feedback
But let me clarify please that (Parm1,Parm2 and Parm3) are variables .. I just provided them as examples. The only common word between all of them is: {"configKey":"

So, I need to grep last {"configKey":" till the end of the line.
Ok, so as you've been asked in the past, can you show us what YOU have done/tried to solve your problem?? Read the "Question Guidelines" link in my posting signature...there are lots of examples using sed and awk that you can find with a brief Google search, and a simple sed/grep can do this, if you tried. Given the input of:
Code:
[[{"configKey":"Parm1",{"configKey":"Parm2",{"configKey":"Parm3"]]
[[{"configKey":"Parm1",{"configKey":"Parm2",{"configKey":"Parm4"]]
[[{"configKey":"Parm1",{"configKey":"Parm2",{"configKey":"Parm5"]]
[[{"configKey":"Parm1",{"configKey":"Parm2",{"configKey":"Parm9"]]
[[{"configKey":"Parm1",{"configKey":"Parm2",{"configKey":"Parm8"]]
[[{"configKey":"Parm1",{"configKey":"Parm2",{"configKey":"Parm2"]]
...and using this:
Code:
sed 's/,/^M/g' t | grep configKey
...you get:
Code:
{"configKey":"Parm3"]]
{"configKey":"Parm4"]]
{"configKey":"Parm5"]]
{"configKey":"Parm9"]]
{"configKey":"Parm8"]]
{"configKey":"Parm2"]]
...which seems to be what you want, isn't it?? You never followed up the last time you asked people to write your scripts for you:
https://www.linuxquestions.org/quest...ge-4175670905/
 
Old 04-08-2020, 12:29 PM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you can easily adjust the grep I posted
 
1 members found this post helpful.
Old 04-08-2020, 12:49 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Quote:
Originally Posted by berndbausch View Post
The text looks ill-formed. Where are the closing braces?
...
sed instead of grep:
Code:
echo '[[{"configKey":"Parm1",{"configKey":"Parm2",{"configKey":"Parm3"]]' | sed 's/.*\({"configKey.*\)/\1/'
You do not need to cover (cut and give back) the remainder of the line:
Code:
echo '[[{"configKey":"Parm1",{"configKey":"Parm2",{"configKey":"Parm3"]]' | sed 's/.*\({"configKey\)/\1/'
will save a few CPU cycles.
 
1 members found this post helpful.
Old 04-08-2020, 02:33 PM   #9
eng_mohammedmostafa
Member
 
Registered: Nov 2008
Location: Egypt
Posts: 110

Original Poster
Blog Entries: 1

Rep: Reputation: 16
This one fulfilled the request
sed 's/.*\({"configKey.*\)/\1/'

Many thanks
 
1 members found this post helpful.
Old 04-08-2020, 05:22 PM   #10
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Maybe I am oversimplifying, but
Code:
sed 's/.*,//' testfile
As has already been pointed out, the text looks like some malformed JSON file. If the format is corrected the solutions may need to be adjusted.
 
1 members found this post helpful.
Old 04-08-2020, 06:00 PM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by crts View Post
Maybe I am oversimplifying, but
Code:
sed 's/.*,//' testfile
As has already been pointed out, the text looks like some malformed JSON file. If the format is corrected the solutions may need to be adjusted.
Who knows? The OP didn't answer any questions about the input data, and showed no effort of their own to solve their problem.

Last edited by TB0ne; 04-08-2020 at 06:20 PM.
 
  


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
[SOLVED] grep text from a line in between "start" and "end" word deepakdeore2004 Programming 7 08-07-2013 09:45 AM
sed append word at end of line if word is missing franjo124 Linux - Newbie 3 03-08-2012 08:41 PM
grep till the 1st occurrence of a pattern raghu123 Programming 2 04-15-2009 05:47 AM
grep till the 1st occurrence of a pattern raghu123 Programming 1 04-15-2009 05:17 AM
read the input file from the specified line no till end suchi_s Programming 5 09-09-2004 04:36 AM

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

All times are GMT -5. The time now is 01:19 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
Open Source Consulting | Domain Registration