assigning complicated input to a variable

I cannot seem to correctly assign this line of code to a variable:

HITS=`pwd`/csv/LightningProbability30minAlignmentErrorTable_.csv | sed 's/,/ /g' | awk '{print int(100$5)}' | wc

This does not work.

I tried surrounding it in backticks, but I think the backticks in `pwd` is messing it up.

Any ideas?

You do not need the pwd so try just pointing at the directory under the current directory using a relative path instead of an absolute path like this:

HITS=`csv/LightningProbability30minAlignmentErrorTable_*.csv | sed 's/,/ /g' | awk '{print int(100*$5)}' | wc`

What does "does not work." mean? What does happen? What error messages do you get? What did you expect to happen?

What is "it"?

Did you have nested backticks? If so, did you escape the inner ones?

Hi.

Here you're trying to execute all the CSV files. I'd guess this won't work.

It's hard to say, because what you're trying to do isn't totally clear.

You want to assign a path to a variable, remove commas from the path, pass that to awk (without actually echo'ing it) and multiply the fifth field by 100, then get a word count.

Sounds like you wanted to cat the files (otherwise what use is sed in this?). And why go to the trouble of multiplying the fifth field and then pipiing the output through wc?

Perhaps something like:

HITS=$(awk -F, '{print int(100*$5)}' csv/LightningProbability30minAlignmentErrorTable_*.csv )

If you want to use wc, then the multiplication inside awk serves no purpose because wc will only show the lines, words and characters:

HITS=$(cat csv/LightningProbability30minAlignmentErrorTable_*.csv | sed "s/,/ /g" | wc)

(if your fields in the CSV files have spaces in them, then the wc output is meaningless)

If you want a record count from each file then use

HITS=$(wc csv/LightningProbability30minAlignmentErrorTable_*.csv)

yes, I should be more specific...

cat `pwd`/csv/LightningProbability30minAlignmentErrorTable_.csv | sed 's/,/ /g' | awk '{print int(100$5)}' | grep "^0" | wc -l

this prints out the number of zeroes in the 5th 'column' of all of the csv files in the directory. So I am simply trying to assign it to a variable.

HITS=cat `pwd`/csv/LightningProbability30minAlignmentErrorTable_.csv | sed 's/,/ /g' | awk '{print int(100$5)}' | grep "^0" | wc -l

this does not work, and the error message was along the lines of 'file or directory does not exist'. When I surrounded the RHS of the expression with backticks, I had the same result.

However, this worked as desired:
HITS=`cat /csv/LightningProbability30minAlignmentErrorTable_.csv | sed 's/,/ /g' | awk '{print int(100$5)}' | grep "^0" | wc -l`

For some reason, I didn't think I still needed the cat in there.

Thanks for the help, all!

You don't need cat:

HIST=$( sed 's/,/ /g' /csv/LightningProbability30minAlignmentErrorTable_*.csv | awk '{print int(100*$5)}' | grep "^0" | wc -l )

-- or sed or grep or wc:

HITS=$( awk -F, '$5 == 0 { ++x } END { print x }' /csv/LightningProbability30minAlignmentErrorTable_*.csv )

That's the reason why I hate backticks. Hard to read, prone to errors due to keyboard mapping, display fonts etc.. A real pain (in the bash ;)). For command substitution I prefer the $(...) construct as shown in scottand and cfajohnsonn's posts.

On top of that I thought they were obsolete...

I always use tickbacks (in Solaris and Linux), I agree that the $( ) construct is more visible.

... and easily permits nested commands. I am not sure it's possible with backticks.

It is possible, but the inner ticks must be quoted:

x=`some_command \`other_command\``

Cool cfajohnson. Didn't know you could next backticks.