0

I want a shell script to get current linux distribution name. There is a command I found : awk '/^ID=/' /etc/*-release | awk -F'=' '{ print tolower($2) }', but it's output is "centos", need remove ".

By learning similar partern from:

https://stackoverflow.com/a/25507646/1637673

https://stackoverflow.com/a/20601998/1637673

I finnally work it out by awk '/^ID=/' /etc/*-release | awk -F'=' '{ gsub(/\"/, "") }1 { print tolower($2) }'.

But I don't understand what does number 1 do in that pattern. Remove 1 would break that command. Read man awk , awk_regular_expressions and others tutorials, none of them mention such a pattern.

Update

I copy Floris's explanation here:

gsub(/^[ \t]+/,"",$2);    - starting at the beginning (^) replace all (+ = zero or more, greedy)
                             consecutive tabs and spaces with an empty string
gsub(/[ \t]+$/,"",$2)}    - do the same, but now for all space up to the end of string ($)
1                         - ="true". Shorthand for "use default action", which is print $0
                          - that is, print the entire (modified) line

1 ="true". Shorthand for "use default action", which is print $0

But there is already an action { print tolower($2) } in my script, why need another default action before it?

Community
  • 1
  • 1
Mithril
  • 12,947
  • 18
  • 102
  • 153
  • consider an example: `awk 'NF > 2 {print $1" "$NF}'` .. here `NF > 2 ` is condition check and `{print $1" "$NF}` is action performed when that condition is satisfied... the `1` in your example is a condition which of course is always true.. when action (statements within {}) is ignored, awk by default prints content of `$0` – Sundeep Oct 28 '16 at 01:56
  • and to get distribution name, you can use `lsb_release -si` – Sundeep Oct 28 '16 at 01:59
  • @Sundeep awk basic structure is `awk 'BEGIN{ commands } pattern{ commands } END{ commands }' `. There is a worked command `awk 'BEGIN{ i=0 } { i++ } END{ print i }' filename`, I don't see any condition in it. Does a pattern become a condition If I use regex ? PS: `lsb_release` not exist on some centos, that's why I need a common script. – Mithril Oct 28 '16 at 02:08
  • `pattern` can be regex, comparison check, mix of two, etc... for ex: `/foo/{print $2}` will print second column only if line contains `foo`... `/foo/ && $3 > 3{print $2}` will print second column if line contains `foo` and third column value is greater than 3.. would recommend to read https://www.gnu.org/software/gawk/manual/ - there is pdf too which you can download... there are examples here on SO documentation too - https://stackoverflow.com/documentation/awk/topics – Sundeep Oct 28 '16 at 02:15
  • in `BEGIN{ i=0 } { i++ } END{ print i }` the statement `i++` is executed once every line.. condition is optional – Sundeep Oct 28 '16 at 02:17
  • your awk script can be shortened to `awk -F'=' '/ID/{print tolower($2)}' /etc/*-release` ... think over it – Sundeep Oct 28 '16 at 02:20
  • @Sundeep & @Mithril Isn't there an empty condition in `{ i++ }` that matches everything? Awk basic structure is `condition { action }` where `condition` is typically a pattern but can be an _empty condition_ to match everything or `BEGIN`, `END` etc. – James Brown Oct 28 '16 at 06:43

0 Answers0