Skip to main content
added 174 characters in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

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).

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.


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.

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).

added 209 characters in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

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.


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.

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.


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.

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.


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.

Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

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.


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.