Get gcc version number and compare with bash [duplicate] - bash

This question already has answers here:
How to compare two strings in dot separated version format in Bash?
(38 answers)
Closed 3 years ago.
I want to check gcc version whether >= 9.1.
First, I type this shell:
$ gcc --version | grep gcc | awk '{print $3}'
I get following in my computer after I type above shell:
9.1.0
I know bash only can compare number of integer,
such as command "-gt".
I want to compare gcc which version >= 9.1 .
How do I do?

You need not to use grep with awk, awk could look for string and could do comparison too. Could you please try following.
gcc --version | awk '/gcc/ && ($3+0)>9.1{print "Version is greater than 9.1"}'

Related

Sort set of string elements in descending order using shell script

I am trying to sort set of string elements in descending order but the result surprises me. I can see 7.1.1 comes before 7.10.1, not sure why.
$ cat sort.sh
#!/bin/bash
for i in $( echo "5.2.0 5.1.2 7.1.1 7.10.1" | tr ' ' '\n' | sort -r )
do
echo $i
done
Output:
$ ./sort.sh
7.1.1
7.10.1
5.2.0
5.1.2
Expected output:
$ ./sort.sh
7.10.1
7.1.1
5.2.0
5.1.2
Am i missing anything here? Any tips would be really helpful.
Thanks
Sort uses the sorting rules specified by the current locale. See "Sort does not sort in normal order!" in GNU Coreutils FAQ. GNU sort (1) manual page contains this warning:
*** WARNING *** The locale specified by the environment affects sort order. Set LC_ALL=C to get the traditional sort order that uses native byte values.
If you specifically want to sort version numbers, see the -V command line option in GNU sort:
-V, --version-sort
natural sort of (version) numbers within text

Using grep to filter real time output of a process? If so, how to get the line after a match? [duplicate]

This question already has answers here:
How to show only next line after the matched one?
(14 answers)
grep: show lines surrounding each match
(14 answers)
Read from a endless pipe bash [duplicate]
(1 answer)
Closed 4 years ago.
Should I use grep to filter a real time output? I'm not sure if this is what I should use for a real time output.
Example: command -option | grep --color 'string1\|string2'
If so, how to get also the lines after string1 and string2?
As #shellter mentioned, from man grep:
-A num, --after-context=num
Print num lines of trailing context after each match. See also the -B and -C options.
so you would use command -option | grep -A 1 --color 'string1\|string2' to print matched lines and the line right after them.
There are plenty of other options in the manual for grep, and most other command-line programs, so I suggest getting used to running man cmd as a quick first check.

bash sorting numbers with decimals [duplicate]

This question already has answers here:
Sorting numbers with multiple decimals in bash
(4 answers)
Unix sort of version numbers
(7 answers)
Sort version strings on bash
(2 answers)
Closed 5 years ago.
I have a file like so:
1.1
3.2
1.2
1.10
I would like to sort the file so that it looks like so:
1.1
1.2
1.10
3.2
In other words, 1.10 is bigger than 1.2
I tried:
sort -nk 1,1 file
But I keep getting this, which is not what I want
1.1
1.10
1.2
3.2
Thanks
With GNU sort:
sort -t "." -n -k1,1 -k2,2 file
Output:
1.1
1.2
1.10
3.2
You may use the -V option.
sort -V numbers
However this option is only in GNU Coreutils and could be absent from other implementation.
See https://stackoverflow.com/a/35386002/1107536

Grep differences between two lists [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I've been using a simple grep command to output differences between two files and it works fine in Windows 10 (cygwin grep), and in Windows 10's Ubuntu based Bash shell, worked great with Cygwin's Grep, but is not working on Mac OS X Yosemite.
Here is the is the very simple command line I've been using:
grep -F -v -f list1.txt list2.txt > differences1.txt
With Mac OS X issuing that command from terminal causes a long pause and no output to screen. I've checked both list1.txt and list2.txt to ensure they have the appropriate line terminations for their respective OS and still doesn't work. I've consulted the man page for grep along with command line help and can't discern any difference in parameters between the different OSes that would cause this problem. But for the record here is the Windows 10 Bash Shell Grep version (GNU grep 2.16) and Mac OS X Yosemite (BSD grep 2.5.1-FreeBSD).
techraf's suggestion to install GNU coreutils to use grep as you used in Windows Bash Shell will work for you always.
Even without that FreeBSD awk has enough functionality to get the difference between two files with the logic below:-
awk 'NR == FNR{a[$0]++; next} !($0 in a)' file1 file2
will do the diff between two files as confirmed on awk version 20091126 (FreeBSD)
Assuming my files are like below:-
file1:-
1
2
3
4
file2:-
2
5
To get the lines that are unique in file1, do
awk 'NR == FNR{a[$0]++; next} !($0 in a)' file2 file1
will produce output as
1
3
4
To get the lines that are unique in file2, do
awk 'NR == FNR{a[$0]++; next} !($0 in a)' file1 file2
will produce output as
5
Make sure you are running the correct grep (not alias, function, or some script that happened to be found earlier on your PATH):
/usr/bin/grep -F -v -f list1.txt list2.txt > differences1.txt
A number of tools in FreeBSD (on which Mac OS X is based) and GNU distributions differ in functionality. That said, the parameters in your command indeed look consistent across the versions.
You can install GNU grep with Homebrew:
brew tap homebrew/dupes; brew install grep
and then run it using the command ggrep.
As a side-note: you can also install other GNU tools which also differ (like gdate for date) with:
brew install coreutils

Sorting strings with numbers in Bash [duplicate]

This question already has answers here:
How to sort strings that contain a common prefix and suffix numerically from Bash?
(5 answers)
Closed 6 years ago.
I've often wanted to sort strings with numbers in them so that, when sorting e.g. abc_2, abc_1, abc_10 the result is abc_1, abc_2, abc_10. Every sort mechanism I've seen sorts as abc_1, abc_10, abc_2, that is character by character from the left.
Is there any efficient way to sort to get the result I want? The idea of looking at every character, determining if it's a numeral, building a substring out of subsequent numerals and sorting on that as a number is too appalling to contemplate in bash.
Has no bearded *nix guru implemented an alternative version of sort with a --sensible_numerical option?
Execute this
sort -t _ -k 2 -g data.file
-t separator
-k key/column
-g general numeric sort
I think this is a GNU extension to sort, but you're looking for the --version-sort (or -V) option:
$ printf "prefix%d\n" $(seq 10 -3 1)
prefix10
prefix7
prefix4
prefix1
$ printf "prefix%d\n" $(seq 10 -3 1) | sort
prefix1
prefix10
prefix4
prefix7
$ printf "prefix%d\n" $(seq 10 -3 1) | sort --version-sort
prefix1
prefix4
prefix7
prefix10
https://www.gnu.org/software/coreutils/manual/html_node/sort-invocation.html
You can sort using version-sort
Just pass the following arg
-V or --version-sort
# without (ersion-sort)
$ cat a.txt
abc_1
abc_4
abc_2
abc_10
abc_5
# with (version-sort)
$ sort -V a.txt
abc_1
abc_2
abc_4
abc_5
abc_10

Resources