'Using AWK, print $1 (First column) if the string in the column starts with a specific string?
Firstly I would like to say that I am aware of the many questions here on StackOverflow regarding AWK and regular expressions. I already tried searching different questions and answer, tested multiple answers and none worked.
I have a list that is generated by the command:
iostat -dx | awk ' { print $1 }'
It outputs the following:
extended
device
ada0
ada1
ada2
pass0
pass1
pass2
I want to output only the lines starting with ada... ada0, ada1, ada2.
Here are some commands I tried, and they all output nothing:
iostat -dx | awk '($1 == "^ada") { print $1 }'
iostat -dx | awk '($1 == "/^ada.*$/") { print $1 }'
This one outputs device (??):
iostat -dx | awk '($1 ~ /^d[ada]*/ ) { print $1 }'
Important: I can not use grep for this, since this is being runned on a Docker Image that DOES NOT have GREP, only AWK. I am very much aware of the command "iostat -x | grep "ada" | awk '{print $1}'", but unfortunatelly I can not use that.
Solution 1:[1]
The regular expression should just be ^ada, as in your first attempt. But the regexp should be inside //, not quotes, and you have to use ~ to compare it.
iostat -dx | awk '$1 ~ /^ada/ { print $1 }'
Solution 2:[2]
I am very much aware of the command "iostat -x | grep "ada" | awk '{print $1}'", but unfortunatelly I can not use that.
Just replacing grep "ada" using awk in above gives
iostat -x | awk '/ada/' | awk '{print $1}'
which might be written more concisely as
iostat -x | awk '/ada/{print $1}'
Note that this will print all lines containing ada anywhere, like that with grep you have shown.
As side note if grep is prohibited you might replace simple grep commands using awk as shown above or using sed as follows
iostat -x | sed -n '/ada/p' | awk '{print $1}'
-n does turn off default printing, /ada/p means if line contains ada do print it
Solution 3:[3]
UPDATE 2 : testing through all awk variants I have to illustrate portability of solution :
% cat iostat.txt | gawk -te 'NF=/^ada/'
ada0
ada1
ada2
% cat iostat.txt | gawk -ce 'NF=/^ada/'
ada0
ada1
ada2
% cat iostat.txt | gawk -Sbe 'NF=/^ada/'
ada0
ada1
ada2
% cat iostat.txt | gawk -ne 'NF=/^ada/'
ada0
ada1
ada2
ada2
% cat iostat.txt | gawk -Pe 'NF=/^ada/'
ada0
ada1
ada2
% cat iostat.txt | gawk -e 'NF=/^ada/'
ada0
ada1
ada2
% cat iostat.txt | mawk 'NF=/^ada/'
ada0
ada1
ada2
% cat iostat.txt | mawk2 'NF=/^ada/'
ada0
ada1
ada2
% cat iostat.txt | nawk 'NF=/^ada/'
ada0
ada1
ada2
UPDATE : just realized one could make it extremely succinct :
iostat -x | mawk 'NF=/^ada/'
ada0
ada1
ada2
or make it even simpler :
iostat -x \
\
| mawk '!_<NF' FS='^ada'
ada0
ada1
ada2
And if you wanna even skip the shell quoting part :
mawk2 -F^ada NF==2
mawk2 NF==2 FS=^ada
And even more exotic approaches :
mawk '$_!=$NF' FS='^ada[0-9]+'
ada0
ada1
ada2
mawk '_~$NF' FS='^ada[0-9]+'
ada0
ada1
ada2
And if you prefer gnu-gawk instead, then this is an excellent example of why RT exists :
gawk -e '$_=RT' RS='ada[0-9]+'
ada0
ada1
ada2
And if you REALLY like to use unconventional approaches that borderlines on being counter-intuitive and illogical :
mawk '$NF<"<"' FS='^ada[0-9]+'
mawk '$NF<=">"' FS='^ada[0-9]+'
mawk '!_!~NF' FS='^ada[0-9]+'
mawk '_~_!~NF' FS='^ada[0-9]+'
ada0
ada1
ada2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Barmar |
| Solution 2 | Daweo |
| Solution 3 |
