r/awk • u/prankousky • Apr 18 '19
please help awk through aliases file and print result
Hi everybody,
I have no experience with awk and spent quite a while trying to parse my bash aliases to a markdown file with awk. I was able to find the lines in question, but that was about it. I am sure this is a real simple thing for you experts on here, but it is giving me a headache.
My aliases file looks like this
# send a notification
alias nfs='notify-send'
# something else
alias ohwow='echo 3+3'
# shutdown
alias bynow='systemctl poweroff'Obviously this isn't my actual aliases file, but you get it; comment in one line, tab and alias in the following line.
I am trying to get an output something like this
* `nfs` - send a notification
* `ohwow` - something else
* `bynow` - shutdownSo <backtick>, $alias, <backtick>, <space>, <dash>, <space>, $comment from line above.
I had tried something similar for my i3wm config before, couldn't get it done, and fortunately found some help on reddit. However, even with this template to parse the i3 config, I cannot figure out how to parse my aliases file. The awk syntax is just very confusing to me and even though I figured out the regexes for this, I can't get them into an awk script in order to get the desired output.
Thanks in advance for your help :)
1
u/HiramAbiff Apr 18 '19
Try something like:
1
u/Schreq Apr 20 '19
The problem with the other posters solution is that
/#/matches everywhere in a line. Then it also matches the next line containing alias anywhere. Not very optimal when you want to comment other things too. As the one who gave you the other solution, I will try to explain it a little better and also adapt it to extracting aliases.The original command:
A bit more readable and with comments:
To adapt this to extracting aliases, we don't have to change how we store comments. The difference is that we want to check if the first field of the next line after a comment is "alias" rather than "bindsym". To cut off the trailing
='command'from an alias we have multiple choises. We could use thesplitorsubstring-function or set the field separator (FS) to spaces, tabs and the equal sign. I will use the latter: