0

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.

3
  • 2
    What does "it didn't work" mean, exactly? What did you expect to happen, what happened instead? In general, we can't know your situation exactly, and if you gave details, it'd be easier for the readers to help you. And in this case, the error message you get should help reveal the matter.
    – ilkkachu
    Commented Sep 28, 2021 at 8:43
  • 2
    What is $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.
    – pLumo
    Commented Sep 28, 2021 at 8:44
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Sep 28, 2021 at 8:55

1 Answer 1

3

Suppose you have a filename like place.type-date.log in the variable file, and you want to store the part of the filename before the first dot in another variable called first. In that case, you may do that using a standard parameter substitution.

first=${file%%.*}

The above removes the longest suffix matching the shell pattern .* from the value of $file and stores the result in first.

Using % in place of %% would remove the shortest matching suffix instead, leaving place.type-date. Using # or ## in place of % and %% would remove prefix strings rather than suffix strings.

For another example of using these substitutions, see my answer to your previous question.


Your code does not work because $file is the string place.type-date.log, not a command. You may have attempted to do something like

first=$( echo "$file" | awk -F '[.-]' '{ print $1 }' )

But this is more complicated than it needs to be. It also has some issues regarding escape sequences in filenames (which echo may expand) and filenames containing newlines (awk is a line-oriented text processing tool).

3
  • I just needed to add echo before "$file" as you said. Thanks so much. it works perfectly now. Commented Sep 28, 2021 at 9:53
  • 1
    @NgouabaRosalie I would suggest you use the variable substitution method that I showed first in my answer instead of using awk for this. It is slow to run awk like this in a loop, and you will find that your code runs many times faster when using the variable substitution.
    – Kusalananda
    Commented Sep 28, 2021 at 9:56
  • 3
    Note that echo can mangle data. Better to use printf. And awk processes one line at a time, so would be a problem with file names that contain newline characters. Commented Sep 28, 2021 at 10:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .