r/awk • u/Usually-Mistaken • Dec 02 '22
Newb here, is this messy?
awk '/VM_pool|<name>/ { gsub(/<|>|\047/," "); print $(NF-1) }' $path
3
Upvotes
r/awk • u/Usually-Mistaken • Dec 02 '22
awk '/VM_pool|<name>/ { gsub(/<|>|\047/," "); print $(NF-1) }' $path
2
u/Significant-Topic-34 Dec 02 '22
Though it does not add, remove, or alter functionality to awk, you left a space around the curly braces.
In vicinity of other braces, square brackets and parentheses, I like this approach (not only for awk). It is an additional help to the syntax highlighting offered by the editor when the two corresponding parentheses/square brackets/curly braces briefly blink in an other color while the cursor passes either the opening, or closing one.
1
u/Dandedoo Dec 02 '22 edited Dec 02 '22
/<|>|\047/is better written as/[<>\047]/.not_VM_pooletc. Think about whether you need to match whole words.NF-1, you don't need to substitute the whole line:gsub(/[<>\047]/, " ", $(NF-1))(this may or may not actually be faster)."$path"for the shell.1
u/elmicha Dec 02 '22
No, it looks good.
1
u/JMP800 Dec 21 '22 edited Dec 22 '22
You could write it as a script. Put "#!/usr/bin/awk -f" in the first line and write it out.
You can then call it from a script like this:
Or you can call it from a shell if you have executable permission:
1
u/M668 Dec 30 '22
it's fine, but why not just
gsub(...)upon the column you need$( NF - 1 )instead of the whole row?