[SOLVED] vimscript: from current line only return list of every awk variable reference
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
vimscript: from current line only return list of every awk variable reference
Working on an awk program in vim. From the current line only, I want to get a vimscript list of each awk variable or array reference in the line.
Here is an example of what I want to do done in bash.
Code:
~ $ grep -oP '[$a-z][a-z0-9\[\]]*[=, ;"/+*\-]' <<< "split(nt/3600,h,"."); m="." h[2]; split(m*60,mm,"."); s="."mm[2]; ss=s*60"|sed 's/.$//'|sort -u
h
h[2]
m
mm
mm[2]
nt
s
ss
Last edited by porphyry5; 09-07-2012 at 11:23 AM.
Reason: typos
You may need to explain further as the example seems unclear to me? The line you have show is supposed to be what? A snippet of a bigger script? Also, you do not seem to have considered
any of the built in variables and your current example is easily broken when replacing the data (currently a period) between the quotes with characters such as letters.
Code:
$ grep -oP '[$a-z][a-z0-9\[\]]*[=, ;"/+*\-]' <<< "s="l"mm[2]" | sed 's/.$//'
s
$ grep -oP '[$a-z][a-z0-9\[\]]*[=, ;"/+*\-]' <<< "s="l"mm[2]; ss=s*60" |sed 's/.$//'
s
lmm[2]
ss
s
As you can see, both these outputs would appear to be incorrect.
You may need to explain further as the example seems unclear to me?
The awk scripts I've written have been simple and straightforward, and were no problem to get running. My latest one is more complex, and raises the question "how to debug awk?" --lint is not helping, so, after each line of awk code I want to add a line that prints the current value of all the variables referenced in the previous line.
By hand, that is going to be very tedious, so I want to write a vim script that will generate the desired print line from the content of the current line. Unfortunately, every time I try to write vimscript I enter an endless loop of help lookups to explain an unfamiliar term in the previous help lookup. After 4 or 5 of these I enter a comatose state in which I stare at the monitor for a while before erupting with "fu*k it, I'll write the damn thing in bash instead".
Which I have done in this case also. But it would be much cleaner if I did it within vim.
So here is the immediate problem. From a line of awk code, in vim extract each varaiable or array reference, and eliminate the duplicates.
I haven't tried this myself, but GNU awk comes with an Awk Debugger.
Thanks very much for this info. But in the current context, I want to get this vim scripting business off the ground, instead of just retreating to bash each time I try it.
I guess I'll mark this one as solved because I'm getting it done. I was unrealistic in expecting vim's pattern matching and extraction to be as sophisticated as grep's. Apparently you have to index your way through a string getting one match at a time, instead of all at once in a nice little array as with grep.
Last edited by porphyry5; 09-07-2012 at 06:38 PM.
Reason: Found what I need to do
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.