Skip to main content
reworded bits to clarify
Source Link
Sotto Voce
  • 7.2k
  • 1
  • 14
  • 28

Filtering out the contents at the end of a line when those contents are known in advance can be fairly easy in Bash and shells that support similar variable expansion functions. For example:

#!/usr/bin/env bash
line='DEF, characters I do not want'
echo "${line%, characters I do not want}"

will print:

DEF

The syntax $line%string${var%string} returns the contents of $line$var with the string after the % removed from the end of the contents. In this caseexample the string to be removed is ", characters I do not want". If that string isn't at the end, the full contents of $line are returned. There are variations for removing a string from the endstart of the variable, as well as a substitution that can replace a string in the middle of the contents or delete it.

I admit in the above example to changing don't -> do not in order to avoid complications from using single quotes when assigning the string to the $line variable.

The advantage of this approach is that your script doesn't need to invoke an external command to perform the simple filtering. But is it a replacement for the power of python?. Probably not, but there may be other factors that push you toward using a shell script rather than python for this task.

Filtering out the contents at the end of a line when those contents are known in advance can be fairly easy in Bash and shells that support similar variable expansion functions. For example:

#!/usr/bin/env bash
line='DEF, characters I do not want'
echo "${line%, characters I do not want}"

will print:

DEF

The syntax $line%string} returns the contents of $line with the string after the % removed from the end of the contents. In this case the string is ", characters I do not want". If that string isn't at the end, the full contents of $line are returned. There are variations for removing a string from the end of the variable, as well as a substitution that can replace a string or delete it.

I admit in the above example to changing don't -> do not in order to avoid complications from using single quotes when assigning the string to the $line variable.

The advantage of this approach is that your script doesn't need to invoke an external command to perform the simple filtering. But is it a replacement for the power of python?. Probably not, but there may be other factors that push you toward using a shell script rather than python for this task.

Filtering out the contents at the end of a line when those contents are known in advance can be fairly easy in Bash and shells that support similar variable expansion functions. For example:

#!/usr/bin/env bash
line='DEF, characters I do not want'
echo "${line%, characters I do not want}"

will print:

DEF

The syntax ${var%string} returns the contents of $var with the string after the % removed from the end of the contents. In this example the string to be removed is ", characters I do not want". If that string isn't at the end, the full contents of $line are returned. There are variations for removing a string from the start of the variable, as well as a substitution that can replace a string in the middle of the contents or delete it.

I admit in the above example to changing don't -> do not in order to avoid complications from using single quotes when assigning the string to the $line variable.

The advantage of this approach is that your script doesn't need to invoke an external command to perform the simple filtering. But is it a replacement for the power of python?. Probably not, but there may be other factors that push you toward using a shell script rather than python for this task.

Source Link
Sotto Voce
  • 7.2k
  • 1
  • 14
  • 28

Filtering out the contents at the end of a line when those contents are known in advance can be fairly easy in Bash and shells that support similar variable expansion functions. For example:

#!/usr/bin/env bash
line='DEF, characters I do not want'
echo "${line%, characters I do not want}"

will print:

DEF

The syntax $line%string} returns the contents of $line with the string after the % removed from the end of the contents. In this case the string is ", characters I do not want". If that string isn't at the end, the full contents of $line are returned. There are variations for removing a string from the end of the variable, as well as a substitution that can replace a string or delete it.

I admit in the above example to changing don't -> do not in order to avoid complications from using single quotes when assigning the string to the $line variable.

The advantage of this approach is that your script doesn't need to invoke an external command to perform the simple filtering. But is it a replacement for the power of python?. Probably not, but there may be other factors that push you toward using a shell script rather than python for this task.