47

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)

6 Answers 6

59

Multiple ways, a selection:

str=abc.out

Shell:

echo ${str%.*}

Grep:

echo $str | grep -o '^[^\.]*'

Sed:

echo $str | sed -E 's/(.*?)\..*/\1/'

Awk:

echo $str | awk -F. '{print $1}'

-F. means split the string by . and $1 means the first column.

Cut:

echo $str | cut -d. -f1

All output:

abc
Sign up to request clarification or add additional context in comments.

Comments

37

I found this worked best because the pattern you want to use can be in a variable:

DATA="abc.out"
pattern=".out"
DATA=${DATA/$pattern/}
echo "DATA=${DATA}"

The result is:

abc

1 Comment

Works for prefixes as well :) Thank you!
29

If these strings are stored in a file (let's call it input_file):

# input_file:

abc.out abc.out abc.out
def.out def.out
def.out

You can do:

sed -i 's/\.out//g' input_file

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 to stdout
  • 's/\.out//g': Use regular expression to delete .out. the g at the end means delete all occurrences.
  • input_file: specify the input file

If these strings are stored in variables:

var1="abc.out"

You can use parameter subsitution:

var1=${var1%.out}
echo "$var1"

abc

Explanation:

2 Comments

You need an end of line anchor on your sed versions, and the g flag is probably undesirable.
@WilliamPursell I guess that depends on exact input of his files: you are right, I should make my assumptions more clear.
6
$ foo=abc.def.out
$ echo ${foo%.out}
abc.def

In general, to delete the shortest suffix, use:

$ echo ${foo%.*}
abc.def

To delete the longest suffix, use

$ echo ${foo%%.*}
abc

3 Comments

Removes only suffixes, not substrings as was asked for.
@not2savvy The OP used the word "substring", but is clearly asking only to delete suffixes. Shall we take all questions literally? I estimate that at least 85% of all questions asked are stated incorrectly. If answered literally, the answers would all be useless.
I think he’s not „clearly asking only to delete suffixes“, bzt he just gave that as one example of what he’s asking for. Perhaps I’m wrong. However, an answer that gives a more general approach is obviously useful for more people. Just saying!
1

If these are filenames, you can use basename.

$ basename a.out .out

will give you:

a

1 Comment

basename -s .out a.out works too if you like writing your options.
0

An example in zsh:

% myVar="abc.out def.out ghi.out"

% myVar=${myVar:s/.out/} # replace first match of ".out" with ""
% echo myVar
abc def.out ghi.out

% myVar=${myVar:gs/.out/} # replace all matches of ".out" with ""
% echo myVar
abc def ghi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.