grep multipe wildcards in string - linux

Say I have a file that contains:
Release 2.1 OS: RHEL File: package_el6_2.0.1.1_x86_64.rpm
Release 2.1 OS: RHEL File: package_el6_2.0.1.1_i686.rpm
Release 2.1 OS: RHEL File: package_el7_2.0.1.1_x86_64.rpm
Release 2.1 OS: RHEL File: package_el7_2.0.1.1_i686.rpm
I want to grep and match lines that only contain 'package', 'el6', and 'x86_64'
How would I go about doing that on a one liner using grep? The line must match all three and grep shouldn't care about how many characters are in between. If there is a better tool for the job, I'm happy to use that instead.
I've tried the following and got no results:
grep package*el6*x86_64*
Seeing posts and documentation, I understand that * does not mean the same thing as it would in shell. I'm looking for the equivalent of it to use in regex. Hope this makes sense.

Your attempt is very close. * in shell glob terms is roughly equivalent to .* in regex terms. . means "any character" and * is means "repeated any number of times (including zero).
Your regex just needs . before each *. The trailing * is not necessary:
package.*el6.*x86_64
Here is a sample run with your input:
grep 'package.*el6.*x86_64' <<< "Release 2.1 OS: RHEL File: package_el6_2.0.1.1_x86_64.rpm
Release 2.1 OS: RHEL File: package_el6_2.0.1.1_i686.rpm
Release 2.1 OS: RHEL File: package_el7_2.0.1.1_x86_64.rpm
Release 2.1 OS: RHEL File: package_el7_2.0.1.1_i686.rpm"
Prints:
Release 2.1 OS: RHEL File: package_el6_2.0.1.1_x86_64.rpm

Not the best solution (in fact ineficient), but really easy to remember: join 3 greps
grep "package" | grep "el6" | grep "x86_64"

If they're guarenteed to be in order, then a simple grep:
grep "package.*el6.*x86_64" file.txt
would do it. If the items can be in any order, you can try a pipe:
cat file.txt | grep package | grep el6 | grep x86_64
will only show lines containing all three, but in any order

You can use egrep
egrep 'package|el6|x86_64' name_of_file.txt

Related

How can I extract the linux version and gcc version of the proc file?

As the question says, im wondering how to get the linux and gcc versions from the proc file. I know that I have to use cat /proc/version but the result is too much of (to me) unnecessary information. If I run the command I get this: MINGW64_NT-10.0-19042 version 3.1.6-340.x86_64 (#fv-az314) (gcc version 9.3.0 (GCC) ) 2020-07-09 14:33 UTC(the command was run in cmder bash.exe). What I want instead is to only get 3.1.6-340.x86_64(linux version) and 9.3.0(gcc version). I understand there are certain commands that allow for this sort of info extraction but as i am new to bash scripting I don't know how to go about doing that. My wanted output is: Linux: <linux version> \n gcc: <gcc version> (where \n is the new line character).
Any and all help is apreciated.
I kept your "formatting", though I'm not sure that's what you actually want.
awk '{printf "Linux: %s \n gcc: %s", $3, gensub(/^.*gcc version ([^ ]+).*$/, "\\1","1",$0)}' /proc/version
Linux: 5.4.0-72-generic
gcc: 9.3.0tink#linux:~
The code would be :
var1=$(uname -r)
var2=$(gcc -dumpversion)
echo -e "Linux version is $var1 \n gcc version is $var2"
This should give you your required output

sed behavior: Pattern space and Address range [duplicate]

I noticed something a bit odd while fooling around with sed. If you try to remove multiple line intervals (by number) from a file, but any interval specified later in the list is fully contained within an interval earlier in the list, then an additional single line is removed after the specified (larger) interval.
seq 10 > foo.txt
sed '2,7d;3,6d' foo.txt
1
9
10
This behaviour was behind an annoying bug for me, since in my script I generated the interval endpoints on the fly, and in some cases the intervals produced were redundant. I can clean this up, but I can't think of a good reason why sed would behave this way on purpose.
Since this question was highlighted as needing an answer in the Stack Overflow Weekly Newsletter email for 2015-02-24, I'm converting the comments above (which provide the answer) into a formal answer. Unattributed comments here were made by me in essentially equivalent form.
Thank you for a concise, complete question. The result is interesting. I can reproduce it with your script. Intriguingly, sed '3,6d;2,7d' foo.txt (with the delete operations in the reverse order) produces the expected answer with 8 included in the output. That makes it look like it might be a reportable bug in (GNU) sed, especially as BSD sed (on Mac OS X 10.10.2 Yosemite) works correctly with the operations in either order. I tested using 'sed (GNU sed) 4.2.2' from an Ubuntu 14.04 derivative.
More data points for you/them. Both of these include 8 in the output:
sed -e '/2/,/7/d' -e '/3/,/6/d' foo.txt
sed -e '2,7d' -e '/3/,/6/d' foo.txt
By contrast, this does not:
sed -e '/2/,/7/d' -e '3,6d' foo.txt
The latter surprised me (even accepting the basic bug).
Beats me. I thought given some of sed's arcane constructs that you might be missing the batman symbol or something from the middle of your command but sed -e '2,7d' -e '3,6d' foo.txt behaves the same way and swapping the order produces the expected results (GNU sed 4.2.2 on Cygwin). /bin/sed on Solaris always produces the expected result and interestingly so does GNU sed 3.02. Ed Morton
More data: it only seems to happen with sed 4.2.2 if the 2nd range is a subset of the first: sed '2,5d;2,5d' shows the bug, sed '2,5d;1,5d' and sed '2,5d;2,6d' do not. glenn jackman
The GNU sed home page says "Please send bug reports to bug-sed at gnu.org" (except it has an # in place of ' at '). You've got a good reproduction; be explicit about the output you expect vs the output you get (they'll get the point, but it's best to make sure they can't misunderstand). Point out that the reverse ordering of the commands works as expected, and give the various other commands as examples of working or not working. (You could even give this Q&A URL as a cross-reference, but make sure that the bug report is self-contained so that it can be understood even if no-one follows the URL.)
You can also point to BSD sed (and the Solaris version, and the older GNU 3.02 sed) as behaving as expected. With the old version GNU sed working, it means this is arguably a regression. […After a little experimentation…] The breakage occurred in the 4.1 release; the 4.0.9 release is OK. (I also checked 4.1.5 and 4.2.1; both are broken.) That will help the maintainers if they want to find the trouble by looking at what changed.
The OP noted:
Thanks everyone for comments and additional tests. I'll submit a bug report to GNU sed and post their response. santayana

Trying to grep instance id from within a text file

I have a text file bak.txt with the following content:
^[[34mINFO^[[0m[0000] Your engine version 1.11.1-cs1 is compatible
^[[34mINFO^[[0m[0000] We detected local components of UCP instance H7LQ:WKR5:G2PX:4F3V:JQ47:WCIG:JV4W:V6SE:4WMR:TLZN:XYWH:MIEQ
^[[31mFATA^[[0m[0000] Re-run the command with "--id H7LQ:WKR5:G2PX:4F3V:JQ47:WCIG:JV4W:V6SE:4WMR:TLZN:XYWH:MIEQ" or --interactive to confirm you want to upgrade this UCP instance.
I am now trying to grep the UCP instance value from bak.txt file using the following command:
grep -Po '(?<=instance=)[^"]*' bak.txt
It is not working. Please suggest the correct way.
Try this grep:
grep -oP '(?<=instance )[^"]+' bak.txt

Extracting checksum from singe line

I'm trying to get the MD5 of the specified Java package by going through: http://www.oracle.com/technetwork/java/javase/downloads/java-se-binaries-checksum-1956892.html
However, that entire table is just a one-liner in the HTML code, so that makes it a little trickier.
Since you have tagded your question with sed, grep, etc. I am assuming you'll do it from Linux. So you can use Perl's one liner for this.
perl -MLWP::Simple -e "$\ = $/; print for get('http://www.oracle.com/technetwork/java/javase/downloads/java-se-binaries-checksum-1956892.html') =~ m|<td>([a-f0-9]{32})</td>|g;"
This is first downloading the html into $_ variable. Then its parsing the hash from the <td> tags using regex. Pretty simple, yet powerful!
This is from your request, using curl + gnu grep
curl -s http://www.oracle.com/technetwork/java/javase/downloads/java-se-binaries-checksum-1956892.html|grep -Po '(?<=<td>)[a-f0-9]{32}'
Explanation
curl command will get the hmtl to stout and pipe to grep command
grep -Po '(?<=<td>)[a-f0-9]{32}' is a positive look-behind assertion, get only md5 sum. It should be supported in JAVA as well.
For your new request, I recommend to use lynx (text-based web browser). So if you have it ready, run this command:
lynx -dump http://www.oracle.com/technetwork/java/javase/downloads/java-se-binaries-checksum-1956892.html |grep jdk-7u51-solaris-sparc.tar.Z
jdk-7u51-solaris-sparc.tar.Z eb2ebfe3217d306f0ee549edc1875a93
explanation
1) lynx is text-base web browser, here are its homepage and related introduces.
http://lynx.isc.org/lynx2.8.7/index.html
http://en.wikipedia.org/wiki/Lynx_(web_browser)
http://en.wikipedia.org/wiki/Text-based_web_browser
2) lynx with -dump option will take a snapshot on that webpage with reserved format. I used it as html2txt tool. Here is the sample webpage for your reference.
Java SE Binaries Checksum
Checksum for Java SE 7u51 binaries
Filename MD5 Checksum
jdk-7u51-linux-arm-vfp-hflt.tar.gz 80e14facc0aa784f44d8f142025dd020
jdk-7u51-linux-arm-vfp-sflt.tar.gz a2965bc7591a257da8c09772f15f6195
jdk-7u51-linux-i586.rpm 457fb449a4486860ec5bde6c28ce8ec4
jdk-7u51-linux-i586.tar.gz 909d353c1caf6b3b54cc20767a7778ef
jdk-7u51-linux-x64.rpm c523e7339d925c1e6c5994813f7c9e86
jdk-7u51-linux-x64.tar.gz 764f96c4b078b80adaa5983e75470ff2
jdk-7u51-macosx-x64.dmg 73e9cc08d590021706e117c81bc9a4a9
jdk-7u51-solaris-i586.tar.Z 9127418718bec67a4146c5dc1da15155
jdk-7u51-solaris-i586.tar.gz cd914ce06ff537a3acb249d23baf6244
jdk-7u51-solaris-x64.tar.Z 5ee1d6b0d607f80ac0e376485d70e9e4
jdk-7u51-solaris-x64.tar.gz 6e00698dc72b707580f11c4e0288ab2b
jdk-7u51-solaris-sparc.tar.Z eb2ebfe3217d306f0ee549edc1875a93
jdk-7u51-solaris-sparc.tar.gz 60bdb8a9b19db80848d8b6c27466276b
jdk-7u51-solaris-sparcv9.tar.Z 9da60e11238b288a5339688acd64abe0
jdk-7u51-solaris-sparcv9.tar.gz 1cb3c5e8cdcad6c9bfaffc3874187786
jdk-7u51-windows-i586.exe 121b2a740e18bc00b0e13f4537e5f1bc
jdk-7u51-windows-x64.exe d1367410be659f1b47e554e7bd011ea0
jre-7u51-linux-i586.rpm 28d0ee36020023904e64afeebc9555cc
jre-7u51-linux-i586.tar.gz f133f125ca93acef3f70d1912cc2f4b0
jre-7u51-linux-x64.rpm d914baffa3cb378a6054969d7d9bbbd0
jre-7u51-linux-x64.tar.gz 1f6a93cc5ef5f66bb01bc39fd731cd9f
jre-7u51-macosx-x64.dmg b66f5af9e3607dc5727f752a9d28b7fd
jre-7u51-macosx-x64.tar.gz cbd57817ea302be8b2c44968e130bb9b
jre-7u51-solaris-i586.tar.gz 61c5daacea83dc1b267e84bf21e22645
jre-7u51-solaris-x64.tar.gz f03c4d69124f0595db32e20f2aa517f2
jre-7u51-solaris-sparc.tar.gz f9b459dabd97428e95275e259422d6a7
jre-7u51-solaris-sparcv9.tar.gz 32cb98b794bc01ca79f1b6e51fe09c9c
jre-7u51-windows-i586-iftw.exe 5e8cb14f5264af82f66008306e56eaa8
jre-7u51-windows-i586.exe 1af9e2aa8264b023404a76d3fb6751fe
jre-7u51-windows-i586.tar.gz 3921c19528d180902939b9f4c9ac92f1
jre-7u51-windows-x64.exe b0f3a9c0f4c2c66127223ba3644b54f6
jre-7u51-windows-x64.tar.gz 1931de2341f22408be9d6639205675c9
server-jre-7u51-linux-x64.tar.gz c5a034f4222bac326101799bcb20509c
server-jre-7u51-solaris-i586.tar.gz 955d2884960124e93699008236d736fe
server-jre-7u51-solaris-x64.tar.gz b858f9326986cfc7f7cceb4b166c0bfa
server-jre-7u51-solaris-sparc.tar.gz 04c708b162e6210b546b0eef188d4adb
server-jre-7u51-solaris-sparcv9.tar.gz 7ae0e51f5836289d71ad614326c5e9c8
server-jre-7u51-windows-x64.tar.gz 4d9855b5b54cbae9d04318eae9b8e11e
Use the md5sum command line utility on Linux to verify the integrity of
the downloaded file.
Use the md5 command line utility on Mac OS X to verify the integrity of
the downloaded file
See the following articles for guidance on how to verify these
checksums on other platforms:
* Microsoft Windows: [29]Availability and description of the File
Checksum Integrity Verifier utility
Left Curve
Java SDKs and Tools
Right Curve
[30]Java SE
[31]Java EE and Glassfish
[32]Java ME
[33]JavaFX
[34]Java Card
[35]NetBeans IDE
[36]Java Mission Control
Left Curve
Java Resources
Finally went with sed:
$ curl -s http://www.oracle.com/technetwork/java/javase/downloads/javase8-binaries-checksum-2133161.html | sed -nr 's|.*>jdk-8-linux-x64.tar.gz</td><td>(<*[^<]*).*|\1|p'
7e9e5e5229c6603a4d8476050bbd98b1

extracting version string in shell script

I've a progam installed in my linux box called my-scheduler-1.1.0-1112
When I do rpm -qa | grep my, it lists as shown below:
my-scheduler-1.1.0-1112
I want a command which will extract 1.1.0-1112 which is version part in my shell script.
what would be the command to extract it in shell script?
For this question you can try the --queryformat parameter for rpm.
like:
rpm -qa --queryformat '%{NAME}' | grep my
should print
my-scheduler
without the version string... What is much better as mugling it with sed or like. Because you can get something like package-1.0.3-rc2 or soo..
For version-release: use:
--queryformat "%{VERSION}-%{RELEASE}"
and maybe will be useful read http://www.rpm.org/max-rpm/ch-queryformat-tags.html - here is many useful query format tags, so you can directly can get what you want and in what format you want, without scripting...
Not sure what other version strings you may encounter, but you can try:
sed -e 's/^[^0-9]*-//g'
This is a sed replace. It's matching the regular expression ^[^0-9]*-, which is:
starting from the beginning of the String
match as many non-numbers as there are
then the very next character is a -
And it replaces everything that matched with a blank, essentially removing it. This should leave everything after that which is the version string.

Resources