How to get only the version from set of lines?
These are the lines inn flat file:
./org.jamon/jamon-runtime/jars/jamon-runtime-2.3.1.jar
./org.apache.ftpserver/ftplet-api/bundles/ftplet-api-1.0.0.jar
./org.clojars.jasonjckn/kafka_2.9.2/jars/kafka_2.9.2-0.7.2-test1.jar
./org.apache.ftpserver/ftpserver-deprecated/jars/ftpserver-deprecated-1.0.0-M2.jar
./org.codehaus.jettison/jettison/bundles/jettison-1.1.jar
./stax/stax-api/jars/stax-api-1.0.1.jar
Output:
2.3.1
1.0.0
0.7.2-test1
1.0.0-M2
1.1
For a sed solution:
sed 's/.*-\([0-9][0-9.]*[^\/]*\)\.jar$/\1/'
Explanation:
Replace: anything + hyphen + revision + ".jar" with just the revision, where:
The the revision is identified as: starts with number, followed by numbers or dots, followed by any text except slash /, and is preceded by a hyphen -.
".jar" must be at the end-of-line.
Using GNU grep with -P option (PCRE regex for look ahead and behind):
grep -oP '(?<=-)[0-9.]+[-[:alnum:]]*(?=\.jar)' file
2.3.1
1.0.0
0.7.2-test1
1.0.0-M2
1.1
1.0.1
As suggested cbuckley in the comments more generic way of saying it would be (will work if you files have extensions other than .jar:
grep -oP '(?<=-)[\d.]+[-\w]*(?=\.\w+)' file
Here you go:
awk -F/ '{n=split($NF,a,"-");if (a[n-1]~/[0-9.]+/) a[n]=a[n-1]"-"a[n];sub(/\.jar$/,"",a[n]);print a[n]}' file
2.3.1
1.0.0
0.7.2-test1
1.0.0-M2
1.1
1.0.1
If you have gnu grep use the Jaypal's solution :), this awk should work on most system.
Simple solution is
grep -Po "(?<=-)\d[^/]*(?=\.jar$)"
Related
Requirement is to mask some sensitive data from the log file, below code works as expected when awk version is 4.0.2.
I will be greping the log files and then have to mask some data using pattern as mentioned in the below awk snippet and then return the result.
echo "123-123-432-123-999-889 and 123456 and 1234-1234-4321-1234 and xyz#abc.com" | awk ' gsub (/[0-9]{6,}|([0-9]{3,}.){3,}|\w{2,}#\w{2,}.\w{2,}/, "****") 1'
The same is not working in awk version 3.1.7 which is production server version.
I can use only grep, cat, awk and there is no permission to use perl or sed as it is restricted by Admin Team.
Expected Output:
****and **** and ****and ****
Solution should also work if the content is in file, for example
sample.log
123-123-432-123-999-889
and
123456
and
1234-1234-4321-1234
and xyz#abc.com
Command:
cat sample.log | awk ' gsub (/[0-9]{6,}|([0-9]{3,}.){3,}|\w{2,}#\w{2,}.\w{2,}/, "****") 1'
Please help me with awk which can work in 3.1.7 version of awk
Activate RE intervals with:
awk --re-interval '...'
You MAY also need to replace \ws with [[:alnum:]_].
The problem you;re having is that you're using a very old version of gawk from before RE Intervals (e.g. {1,3}) were enabled by default so in that old gawk every { and } is just a literal character for backward compatibility with the 1980s awks (old, broken awk and nawk), so you need to explicitly tell gawk to interpret {1,3} as a RE Interval instead of a literal string of 5 chars.
Idk if back then \w was supported or not so you MAY also need to use the bracket expression I suggested above instead.
I am using Jira to create a bitbucket branch for releases. As part of the build process I need to extract the release number from the branch name.
An earlier part of the build dumps the whole branch name to a text file. The problem I'm having is removing all the text before the build number.
An example branch name would be:
release/some-jira-ticket-343-X.X.X
Where X.X.X is the release number e.g. 1.11.1 (each of X could be any length integer).
My first thought was to literally just select the last 3 characters with sed, however as X could be any length this won't work.
Another post (Removing non-alphanumeric characters with sed) suggesting using the sed alpha class. However this won't work as the jira ticket ID will have numbers in.
Any ideas?
You can remove all characters up to last -:
$ sed 's/.*-//' <<< "release/some-jira-ticket-343-1.11.2"
1.11.2
or with grep, to output only digits and dots at the end of the line:
grep -o '[0-9.]*$'
awk solution,
$ awk -F- '{print $NF}' <<< "release/some-jira-ticket-343-1.11.1"
grep solution,
grep -oP '[0-9]-\K.*' <<< "release/some-jira-ticket-343-1.11.1"
use string operators:
var="release/some-jira-ticket-343-2.155.7"
echo ${var##*-}
print:
2.155.7
Awk solution:
awk -F [-.] '{ print $5"."$6"."$7 }' <<< "release/some-jira-ticket-343-12.4.7"
12.4.7
Set the field delimiter to - and . and then extract the pieces of data we need.
I am trying to figure out how to add a comma between two line outputs of application name and application version.
Here is my present code and output:
ordepmod-mbp:~ ordepmod$ mdls -name kMDItemFSName /Applications/*VMware*.app -name kMDItemVersion | sed 's/[^"]*"\([^"]*\)".*/\1/' | xargs -L 2
VMware Fusion.app 8.1.1
VMware Horizon Client.app 4.0.1
I cannot figure out the syntax to use that would keep the present command as simple as possible, while replacing ".app " with ".app, ". So the intended output would look like (manually edited):
VMware Fusion.app, 8.1.1
VMware Horizon Client.app, 4.0.1
I don't have VMware to test with, so I'll use iTunes instead.
Simplest Method
Basically, I am specifying raw output from mdls to get rid of all the extraneous stuff, then using tr to transliterate the NUL between the two fields into a comma. This is simple, but doesn't give you the space afterwards, but may be good enough.
mdls -name kMDItemFSName -name kMDItemVersion -raw /Applications/iTunes.app | tr '\0' ','
iTunes.app,12.4.1
Slightly Harder, and more accurate
Replacing NULs with tr is ok, but you cannot get both the comma and the space because tr only does "one-for-one" replacements. sed can replace the single NUL with a comma and a space, but it is not very easy with the non-GNU version of sed on OSX, so I would go with perl instead.
mdls -name kMDItemFSName -name kMDItemVersion -raw /Applications/Textedit.app | perl -pe 's/\x0/, /'
TextEdit.app, 1.11
Wildcard Version
Or, if you want to use wildcards, I would go with awk
mdls -name kMDItemFSName -name kMDItemVersion /Applications/*.app | awk -F'"' '/kMDItemFSName/{n=$2} /kMDItemVersion/{print n, $2}' OFS=', '
Output
App Store.app, 2.1
Automator.app, 2.6
Calculator.app, 10.8
Calendar.app, 8.0
Carbon Copy Cloner.app, 4.1.9
Chess.app, 3.13
Contacts.app, 9.0
...
Sonos.app, 6.2.2
Stickies.app, 10.0
System Preferences.app, 14.0
TextEdit.app, 1.11
TextWrangler.app, 5.0.2
Time Machine.app, 1.3
VirtualBox.app, 5.0.24
Xcode.app, 7.3
iBooks.app, 1.5
iMovie.app, 10.1.2
iTunes.app, 12.4.1
The awk command is basically setting the input field separator to ", and then every time it sees kMDItemFSName it saves the second field (which is the app name) as n. Then, every time it sees kMDItemVersion, it outputs the saved name and the second field (which is the version). The OFS (output field separator) is set to a comma and a space.
I`m trying to find lines which match the pattern x.y or x.y.z, where x,y and z are numbers.
For example, given the lines:
1.0/
2.2.5rc1/
2.3.0/
2.3.1/
abc-1.0.0/
the result should be:
1.0
2.3.0
2.3.1
How can I do this?
Things to know:
Call grep in extended mode using -E.
start the pattern with a ^ to signify you want the search to start at the first character.
To search for a digit, use \d
To search for a dot, use \.
To search for thing1 OR thing1, use thing1|thing2.
Note: As Jonathan Leffler pointed out below, \d is a notation that might not work across all version of grep. Try [0-9] or [[:digit:]] to be compliant in POSIX-standard implementations of grep.
Knowing that, we put it together like so:
grep -E "^(\d.\d.|\d.\d.\d)/" yourfile
You can do
grep -C 2 yourSearch yourFile
To send it in a file, do
grep -C 2 yourSearch yourFile > result.txt
Hope it helps!
Basically i am creating an update checker for emesene on osx. I need to find out the version number from inside this file: http://emesene.svn.sourceforge.net/viewvc/emesene/trunk/emesene/Controller.py
The version number is found at self.VERSION = 'version' in the file e.g. self.VERSION = '1.6.3'
The version number then needs to be saved in a file
Is this possible using grep?
grep "self.VERSION = '.*'" Controller.py | cut -d "'" -f 2 > file
If you can use sed(1), then you can extract it with a single command:
sed -n "s/.*self\.VERSION = '\([^']*\)'.*/\1/p" Controller.py > file
you can use awk,
$ awk -F"['-]" '/VERSION[ \t]=/{print $2}' Controller.py
1.6.3