LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-07-2012, 10:07 AM   #1
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
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
 
Old 09-07-2012, 11:38 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 09-07-2012, 01:22 PM   #3
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by grail View Post
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.
 
Old 09-07-2012, 03:34 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
I haven't tried this myself, but GNU awk comes with an Awk Debugger.
 
1 members found this post helpful.
Old 09-07-2012, 04:10 PM   #5
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by ntubski View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
AWK/BASH: get nth line from a file by getline feed to actions in a same awk line cristalp Programming 3 11-23-2011 11:38 AM
[SOLVED] Vimscript: using substitute() on a variable porphyry5 Programming 2 10-09-2011 04:54 AM
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 05:50 PM
grep query to list 1 line [which is fixed] and the next line after it [variable text] Glenn D. Linux - Software 3 01-20-2011 06:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration