I have to extract certain values from some filenames in a directory and I wish to have a command to keep the extracted value in a variable. Say my files have the format place.type-date.log
and I wish to extract and keep place in a variable called first
, how can I do it?
I tried to use
first=$($file | awk -F'[.-]' '{print $1}')
but it didn't work.
Normally if I do
echo " place.type-date.log" | awk -F'[.-]' '{print $1}'
and the result will be place
. So I just replaced place.type-date.log
by $file
in my command because I have to do it for multiple files in a particular directory where I used a for loop.
I want to store the extracted value place
in the variable first
that is why I equated the extraction command to the variable first
. So when I tried to echo first
, and I executed my script, it didn't show anything meaning the extracted value had not been stored in the variable.
$file
?? What happens when yo run that command without saving to variable? I can't see how$file | some_command
will possibly work if$file
is not a command/script itself, so the issue is not how to save in variable, but the initial command.