如何使用shell脚本删除子字符串 [英] How to delete a substring using shell script

查看:84
本文介绍了如何使用shell脚本删除子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字符串调用:

abc.out
def.out

如何删除子串

.out

在这些字符串中?

我应该使用什么命令?(伯恩壳)

What command should I use? (Bourne Shell)

推荐答案

多种方式,一种选择:

str=abc.out

外壳:

echo ${str%.*}

格雷普:

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

Sed:

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

awk:

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

-F. 表示将字符串拆分为 .$1 表示第一列.

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

剪切:

echo $str |切-d.-f1

所有输出:

abc

这篇关于如何使用shell脚本删除子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆