LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 08-04-2017, 08:26 AM   #1
kkrrss
Member
 
Registered: Jun 2016
Posts: 56
Blog Entries: 1

Rep: Reputation: Disabled
Grep text line from


Hi There,

Assume there is file call version.txt which contains following text line.

090df276848c2fb2fbdd0e7449284768ead8348b commit refs/tags/PNB.X.X.3.015.0

I wont to grep this version.txt file and get the result only the part of the version "PNB.X.X.3.015.0"

Note, this "PNB.X.X.3.015.0" is dynamically changing and the "PNB" part of the version is still renaming same.

Please help.
 
Old 08-04-2017, 09:04 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
grep "090df276848c2fb2fbdd0e7449284768ead8348b commit refs/tags/PNB" <filename> | awk -F/ '{print $NF}'

You encapsulate the string in double quotes since white space is the default field separator. You end at PNB since you say what follows that changes.

You pipe into awk and use the -F flag to specify field separator to be "/" rather than white space. The NF is a special variable in awk meaning "number of fields" so $NF is reference to the (last) number. Since your PNB... is preceded by "/" it is a perfect separator to use for what you asked.

Last edited by MensaWater; 08-04-2017 at 09:08 AM.
 
1 members found this post helpful.
Old 08-04-2017, 09:06 AM   #3
sschirrxq
LQ Newbie
 
Registered: Oct 2015
Posts: 8

Rep: Reputation: Disabled
You could use sed for this:

Code:
sed 's/.*tags\///' version.txt
This command will get the single line from the file and removes anything until "tags/" (including) and returns the remaining line (your version).
If you add the parameter '-i' sed will change the file in place.

Code:
sed 's/.*tags\///' version.txt -i
 
1 members found this post helpful.
Old 08-04-2017, 09:07 AM   #4
kkrrss
Member
 
Registered: Jun 2016
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Thank You so much.
Its giving me the answer what I need. could you please further clarify me about the syntax which you have use here.

awk -F/ '{print $NF}

Hope this would be a good learning point for me. appreciate it.
 
Old 08-04-2017, 09:10 AM   #5
kkrrss
Member
 
Registered: Jun 2016
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Thank you so much. both answers has given what I need.
 
Old 08-04-2017, 09:11 AM   #6
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by kkrrss View Post
could you please further clarify me about the syntax which you have use here.
awk -F/ '{print $NF}
I edited my original post to explain it. Let me know if you need more.
 
1 members found this post helpful.
Old 08-04-2017, 09:14 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Grep has "-o" to return only the requested text. See the manpage.
 
Old 08-04-2017, 09:23 AM   #8
kkrrss
Member
 
Registered: Jun 2016
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Thank You all,

how can I write the output of the command into a file as a variable.

for example, how can I wire the following value into a file named "app.version.txt"


version=PNB.X.X.3.015.0

So the "version=" part should be added before the "PNB.X.X.3.015.0"

Last edited by kkrrss; 08-04-2017 at 09:24 AM.
 
Old 08-04-2017, 09:26 AM   #9
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by sschirrxq View Post
You could use sed for this:

Code:
sed 's/.*tags\///' version.txt
This command will get the single line from the file and removes anything until "tags/" (including) and returns the remaining line (your version).
If you add the parameter '-i' sed will change the file in place.

Code:
sed 's/.*tags\///' version.txt -i
This assumes he has no other lines with *tags* on them but is a nice solution.
 
1 members found this post helpful.
Old 08-04-2017, 09:28 AM   #10
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by syg00 View Post
Grep has "-o" to return only the requested text. See the manpage.
Of course since he says what follows PNB changes he'd have to do something like:

grep -o PNB.......... <filename>

But like the sed solution that assumes no other lines have PNB in them and also assumes that the number of characters following PNB are always the same.
 
1 members found this post helpful.
Old 08-04-2017, 09:32 AM   #11
kkrrss
Member
 
Registered: Jun 2016
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Red face

Quote:
Originally Posted by MensaWater View Post
Of course since he says what follows PNB changes he'd have to do something like:

grep -o PNB.......... <filename>

But like the sed solution that assumes no other lines have PNB in them and also assumes that the number of characters following PNB are always the same.


Can you help me on this? people like you help us to save our time.
I would like to learn all this, but I just confuse about the command line "sed"


Thank You all,

how can I write the output of the command into a file as a variable.

for example, how can I wire the following value into a file named "app.version.txt"


version=PNB.X.X.3.015.0

So the "version=" part should be added before the "PNB.X.X.3.015.0"
 
Old 08-04-2017, 09:34 AM   #12
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by MensaWater
But like the sed solution that assumes no other lines have PNB in them and also assumes that the number of characters following PNB are always the same.
That's what regex was invented for.

No more fragile than your presumption there are no other lines with both a slash and PNB in a different order.

Last edited by syg00; 08-04-2017 at 09:36 AM.
 
Old 08-04-2017, 09:51 AM   #13
kkrrss
Member
 
Registered: Jun 2016
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by kkrrss View Post
Can you help me on this? people like you help us to save our time.
I would like to learn all this, but I just confuse about the command line "sed"


Thank You all,

how can I write the output of the command into a file as a variable.

for example, how can I wire the following value into a file named "app.version.txt"


version=PNB.X.X.3.015.0

So the "version=" part should be added before the "PNB.X.X.3.015.0"

Sorry for asking silly question. I have done this using below.
#!/bin/bash

git_tag=`git for-each-ref --sort='-committerdate' 'refs/tags' | head -n 1 | awk -F/ '{print $NF}'`
echo "version="$git_tag > version.txt
 
Old 08-04-2017, 10:22 AM   #14
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by kkrrss View Post
git_tag=`git for-each-ref --sort='-committerdate' 'refs/tags' | head -n 1 | awk -F/ '{print $NF}'`
echo "version="$git_tag > version.txt
When using bash I'd suggest instead:
Code:
git_tag=$(git for-each-ref --sort='-committerdate' 'refs/tags' | head -n 1 | awk -F/ '{print $NF}')
echo "version="$git_tag > version.txt
The `` certainly works but is the old style. Using $() has a couple of advantages:
1) ` is often confused with ' when reading lines so debugging is sometimes a problem.
2) The $() can be nested more easily than the ``.
 
1 members found this post helpful.
Old 08-04-2017, 10:25 AM   #15
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by syg00 View Post
No more fragile than your presumption there are no other lines with both a slash and PNB in a different order.
I didn't assume - I took the OP at his word. He said the only thing that changes is what is after PNB.

There was no slur intended against your suggestion - I was just trying to spell it out further for the OP.
 
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
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
[SOLVED] grep text from a line in between "start" and "end" word deepakdeore2004 Programming 7 08-07-2013 09:45 AM
grep a text in files and print the file name who don't contain such text whossa Linux - Newbie 5 04-13-2012 07:49 AM
grep query to list 1 line [which is fixed] and the next line after it [variable text] Glenn D. Linux - Software 3 01-20-2011 06:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 11:35 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