How to remove a substring using shell script

I have lines:

abc.out def.out 

How to remove a substring

.out

In these lines?

Which command should I use? (Bourne shell)

+8
source share
5 answers

If these lines are stored in a file (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 eliminate any occurrence of the .out substring from this file.

Explanation:

  • sed : call the sed tool to edit text streams.
  • -i : use the option "in place" - this changes the input file that you provide, instead of writing to stdout
  • 's/\.out//g' : use regex to remove .out . g at the end means deleting all occurrences.
  • input_file : specify the input file

If these lines are stored in variables:

 var1="abc.out" 

You can use parameter substitution :

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

Explanation:

  • From the link above: "$ {var% Pattern} Remove from $ var the shortest part of $ Pattern that matches the back end of $ var.
  • Note that the β€œpattern” mentioned here is called globbing, which differs from regular expression in important ways .
+13
source

Several ways to choose:

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}' 

To cut:

echo $str | cut -d. -f1

All conclusion:

 abc 
+12
source
 $ foo=abc.def.out $ echo ${foo%.out} abc.def 

In general, to remove the shortest suffix, use:

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

To remove the longest suffix, use

 $ echo ${foo%%.*} abc 
+4
source

If these are file names, you can use the base name.

 $ basename a.out .out 

will provide you with:

 a 
+1
source

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

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

Result:

abc

+1
source