Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a substring using shell script

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)

like image 608
J0natthaaann Avatar asked Nov 26 '12 17:11

J0natthaaann


People also ask

How do I remove a specific string in bash?

Remove Character from String Using tr The tr command (short for translate) is used to translate, squeeze, and delete characters from a string. You can also use tr to remove characters from a string. For demonstration purposes, we will use a sample string and then pipe it to the tr command.

How do you delete a specific word in Unix?

How do I match and remove (delete) the words “ssh_args=-p 1222” from config file using sed command under Linux or Unix like operating systems? You can use the the substitute sed command changes all occurrences of the “ssh_args=-p 1222”. The same command can be used to delete the required words.


1 Answers

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 
like image 71
Chris Seymour Avatar answered Oct 14 '22 08:10

Chris Seymour