I have strings called:
abc.out
def.out
How do I delete the substring
.out
In these strings?
What command should I use? (Bourne Shell)
I have strings called:
abc.out
def.out
How do I delete the substring
.out
In these strings?
What command should I use? (Bourne Shell)
If these are filenames, you can use basename.
will give you:
Multiple ways, a selection:
str=abc.out
Shell:
Grep:
Sed:
Awk:
Cut:
echo $str | cut -d. -f1
All output:
If these strings are stored in a file (let's call it
input_file
):You can do:
And this will remove any occurrence of the substring
.out
from that file.Explanation:
sed
: invoke the sed tool to edit streams of text-i
: use the "in-place" option - this modifies the input file you provide it instead of writing output tostdout
's/\.out//g'
: Use regular expression to delete.out
. theg
at the end means delete all occurrences.input_file
: specify the input fileIf these strings are stored in variables:
You can use parameter subsitution:
Explanation:
In general, to delete the shortest suffix, use:
To delete the longest suffix, use
I found this worked best because the pattern you want to use can be in a variable:
The result is:
abc