grep/sed/awk multi line array
This is not homework :)
Actually it is work at home. Need a ninja! I want to match and covert ruby variables which are multiple line arrays to single line variable. So considering the example: /tmp/file Code:
# comment blah Code:
$from_here="foo,bar,${to_somewhere_near_here}" cat /tmp/file | grep -v "^#" | sed -n '/\$.* = \[/,/.*\]$/p' And various iterations of all sorts, \1, etc, etc and I am no closer. I have the remove quotes parts and double quote and comma separate, just failing to iterate the multi line arrays properly. Any ninjas? |
One would ask that if you are comfortable using Ruby, why not use it for your task?
Also, is the goal to only print the global variables or are you intending to keep all the other data as well but convert the arrays to strings? |
Hi grail
Hey if it works, I don't care ruby, shell, (perl, python) they can all be execd. But it is in shell at the moment so. The intended goal is to get every variable which has the data in an array converted into a string shell variable. On that note my example was a little incorrect (copy paste job). There should be no leading $ in the result ;) Code:
from_here="foo,bar,${to_somewhere_near_here}" But if you have a pattern for that in ruby :) I would sure appreciated it :) |
A brute-force solution. Not pretty but it works.
Code:
paste -s $InFile \ |
Yo danielbmartin thanks will try in the morning! Thanks!
|
Got it
sed 's/[ \t]*//g; s/\[//g; $d' test3.txt | awk '/\$/,/\]/ {ORS=""; gsub("]", "\n"); print; echo -e "\n"}' | cut -c2-; |
......
|
@amboxer21 - just ran your code which returned me:
Code:
$from_here='foo','bar',"${to_somewhere_near_here}", @OP - Are we to assume that the ruby code you run this over will actually be incorrect data? I ask based on 2 examples: Code:
$from_here = ['foo, # no quote after the word foo will cause ruby to error |
Quote:
|
Thanks all - Daniel. Perfect, you are a ninja! [SOLVED]
amboxer21 thank you as well, however your second version erroneously includes the final variable. Not that I am complaining, thank you to all of you for the time. Code:
gary@mc9:~$ paste -s /tmp/file \ |
Any chance you might answer my questions? Just curious mainly. If I can assume that the original would be actual ruby code, ie without the issues mentioned:
Code:
$ cat earthgecko.infile |
Hi grail
1. The 'foo variable was incorrect as you rightly point out it should be 'foo' - apologies that was put together too hastily. 2. comma after last value in array is present but would not necessarily need to be - e.g. "${to_somewhere_near_here}", # here you have a comma to say there is another element but the next piece of data is the end of the array, this will also cause an error. This is just a legacy formatting related to puppet resources. Thanks for the input. Ironically when I applied Daniel's pattern to the proper large data set, it did not work :( So I turned the problem inside out, from a point of view of "do not" change to a one of "rather extend". Changes often have unintended knock-on effects. So all this input really did help me to simplify the issue and just fix the incorrect outputs after the original operation, instead of trying to fix the big original operation. Not pretty, but effective. |
I'm saying, anyone can group a bunch of excess calls together. I was going for efficiency. I feel it forces you to learn the language/tool your using. If I use the same approach as Daniel has, I could have provided a working solution as well.
|
Hi amboxer21 and grail
I titled the post grep/sed/awk not ruby. Things in the real world are sometimes complex. I know it is possible (probably) to use ruby for everything, but the world is heterogeneous. Thank you for the ruby examples I will in no doubt reuse some of them in some place, in the future :) Please do not take offence and if you do, just remember fences are expensive to replace. |
All times are GMT -5. The time now is 06:40 PM. |