http://www.linuxquestions.org/questi...5/#post4183237
The above post is the inspiration for this experiment. Read it if you like, but I'm going to try to make that not necessary, by explaining it like it's a brand new problem.
Also,
I KNOW this code is terrible, and is not the way to do this. I simply want to understand why it doesn't work - other methods of doing the job are not needed. Learning experience only! So..
The input file (filename = "tags") contains the below:
Code:
root@reactor: cat tags
This is about soccer #soccer_generic #soccer_intro . More information is here in more text.
This is line 2 #another_hash_tag #hastag_2 . And here is even more text.
Here's another line to increase count of tag: #hastag_2
root@reactor:
I expect my output to resemble the following:
Code:
#soccer_generic (1)
#soccer_intro (1)
#another_hash_tag (1)
#hastag_2 (2)
#hastag_2 (2)
Yes, I am aware that there's a duplicate line in the output - for this exercise it doesn't matter.
What I
actually get for output, is this:
Code:
#soccer_generic (0)
#soccer_intro (0)
#another_hash_tag (0)
#hastag_2 (0)
#hastag_2 (0)
So notice the counts are not happening. Here's the code:
Code:
grep -o '#\w*' tags |\
xargs -I{} echo $(export var='{}'; printf "%s" "$var "; echo "($(grep -c "'$var'" tags))");
Now, a demonstration about why I am confused about the output. I'm going to add an
echo statement to the code, so we can see the command that's supposedly being run inside the subshell; observe the output:
Code:
grep -o '#\w*' tags |\
xargs -I{} echo $(export var='{}'; printf "%s" "$var "; echo "($(echo grep -c "'$var'" tags))");
#soccer_generic (grep -c '#soccer_generic' tags)
#soccer_intro (grep -c '#soccer_intro' tags)
#another_hash_tag (grep -c '#another_hash_tag' tags)
#hastag_2 (grep -c '#hastag_2' tags)
#hastag_2 (grep -c '#hastag_2' tags)
So it looks to me like the command should be working. I can run one of those greps:
Code:
root# grep -c '#another_hash_tag' tags
1
So why doesn't the "1" appear when the whole command is run? Things I have tried include assorted combinations of :
grep | xargs -n 1 ...
grep | xargs -L 1 ...
grep | xargs -n 1 -L 1 ...
grep | xargs -d "\n" ...
grep -Z | xargs -0 ...
echo $(grep) | xargs ...
echo "$(grep)" | xargs ...
xargs ... <<<$(grep)
xargs ... <<<"$(grep)"
Every combination of those above, I've mixed and tried, and probably more. Been to the xargs manpage repeatedly, and grep's too. If I'm missing something obvious, that's cool

- just tell me where it is!
Also tried grep -E, -e but didn't fiddle much with grep since the output appears such that grep is working fine as is.
Also tried without that subshell in there at the end, like so (and took out the brackets desired in the output, for code clarity):
Code:
root@reactor: grep -o '#\w*' tags |\
xargs -I{} echo $(export var='{}'; printf "%s" "$var "; grep -c "'$var'" tags;)
#soccer_generic 0
#soccer_intro 0
#another_hash_tag 0
#hastag_2 0
#hastag_2 0
root@reactor: grep -o '#\w*' tags |\
xargs -I{} echo $(export var='{}'; printf "%s" "$var "; echo grep -c "'$var'" tags;)
#soccer_generic grep -c '#soccer_generic' tags
#soccer_intro grep -c '#soccer_intro' tags
#another_hash_tag grep -c '#another_hash_tag' tags
#hastag_2 grep -c '#hastag_2' tags
#hastag_2 grep -c '#hastag_2' tags
root@reactor:
So still no go - seems the subshell itself is not the problem...
I've tried setting various shellopts, like dotglob/extglob/globstar, but I admit to not really having much of a grip on these anyways yet, as I rarely use them. I don't think they have anything to do with this, but you may correct me.
Tried removing the single quotes inside
grep -c "'$var'" to no avail. Tried no quotes, double quotes, and both together on
var='{}' to no avail.
Tried sticking `eval` into various places, with and without the subshell near the end. Made no difference:
Code:
root@reactor: grep -o '#\w*' tags |\
xargs -I{} echo $(export var='{}'; printf "%s" "$var "; eval grep -c "'$var'" tags;)
#soccer_generic 0
#soccer_intro 0
#another_hash_tag 0
#hastag_2 0
#hastag_2 0
root@reactor: grep -o '#\w*' tags |\
xargs -I{} echo $(export var='{}'; printf "%s" "$var "; eval echo grep -c "'$var'" tags;)
#soccer_generic grep -c #soccer_generic tags
#soccer_intro grep -c #soccer_intro tags
#another_hash_tag grep -c #another_hash_tag tags
#hastag_2 grep -c #hastag_2 tags
#hastag_2 grep -c #hastag_2 tags
root@reactor:
So, what do you folks think? Again - I don't in reality want a good, working way to do this job; I simply want to understand what exactly is causing
this way to not work. Further: the "0" being returned from the problem area - do you suppose it's a zero as in "No errors, exit 0", or is it the result of `grep -c blah` finding zero occurrences of "blah"? Or something else?
FWIW it's Slackware64 -current.