I am new to shell, I have a case where I am trying to evaluate a particular column unique values to check if they are valid in a shell script which will be invoked later.
From my searches I think cut along with sort & unique is good to do it
So my attempt is
file=/filepath/*vendor.csv
file_categories = `cut -d, -f1 $file |sort |unique`
$file should hold file which has vendor in its filename
but even after using command substitution (`) the $file is not getting replaced with the correct filename , it just places what is present in file
Another example for what I am attempting is
a=/stage/scripts/vendor/*.out
echo $a
/stage/Scripts/ecommerce/oneclick/nohup.out /stage/Scripts/ecommerce/Vendor/Vendor_Automate_Ingestion_Process.out
wc-l
wc: /stage/Scripts/ecommerce/vendor/*.out:
$(wc -l "$a")
wc: /stage/Scripts/ecommerce/vendor/*.out:No such file or directory
I want to understand how we can pass wild characters in command substitution and what I can do to rectify.
In order to make your
wc -l
command to work, call it as such:Do not quote the
a
variable, the shell needs to expand it to read its*
wildcard valueNo,
file
will contain the literal string with the wildcard. When you interpolate the value$file
without quotes around it, that's when the shell evaluates it as a wildcard.echo "$file"
with proper quoting shows you the actual value of the variable.There is no good way to store a list of file names in a regular shell variable. Ksh and some other shells have arrays for this purpose, but it's not portable back to generic
sh
and may be something else than what you actually need, depending on what end goal you are trying to accomplish. If you want to extract unique values from a field in the files matching the wildcard into a string, just make sure you don't have spaces around the equals sign in the assignment and you're done.Storing the wildcard in a variable is dubious here; probably simply use the wildcard directly if this is the problem you want to solve.
Everywhere you don't specifically want the shell to expand wildcards and tokenize a string, you need to put double quotes around it.
This string isn't properly machine readable, and so it's of limited use to capture it in a variable at all. I'll wager a small sum of money that you actually simply want to display the output directly instead of storing it in a variable so that you can then
echo
its value:If you want to loop over the values, pipe this further to
while read -r ...