shell - 从shell中的文件的每一行中删除字符
问题描述
我有一个 shell 脚本,它逐行读取变量的值。我需要从每一行中删除某些字符。
我所拥有的 - $sample_variable -
Data 0 start; 1 ABCD0;2 EFGH0;3 IJKL0;4 MNOP0;5 QRST0;6 end;
我想要的是 -
start
ABCD0
EFGH0
IJKL0
MNOP0
QRST0
end
我写的代码 -
IFS=$';'
for j in $sample_variable
do
j=$j | cut -d ' ' -f3-
echo $j
j=${j// /''}
echo $j
echo $j >> output.txt
done
我正在将输出写入 txt 文件。但是,该文件被写入 output.txt -
start
1ABCD0
2EFGH0
3IJKL0
4MNOP0
5QRST0
6end
如何删除开头出现的数字?
解决方案
如果您尝试删除所有数字,我会说您可以尝试使用该tr
工具,如下所示:
IFS=$';'
for j in $sample_variable
do
j=$j | cut -d ' ' -f3-
echo $j
j=${j// /''}
echo $j | tr -d [:digit:]
echo $j | tr -d [:digit:] >> output.txt
done
但是,如果您只想删除初始数字,则需要一个更通用的工具,例如sed
,它看起来像:
IFS=$';'
for j in $sample_variable
do
j=$j | cut -d ' ' -f3-
echo $j
j=${j// /''}
echo $j | sed -e 's/^[0-9]\?//'
echo $j | sed -e 's/^[0-9]\?//' >> output.txt
done
推荐阅读
- php - 无法在 PHP Soap Clinet 中发送 CDATA
- plsql - 如何解决plsql的这个错误?
- java - 使用 jpexport 时出错(导出 jprofiler .jps 文件)
- html - 打印页面时边框颜色发生变化
- arrays - 创建具有 xy 坐标的动态二维数组,如图形
- javascript - 警告:失败的道具类型:提供给`Route`的类型为`object`的无效道具`component`,预期的`function`错误使用react-router-dom
- angular - Angular 文件夹结构
- c - 如何使用不同类型的同名指针?
- java - 如何在 Spring Boot Rest API 中的 BeanUtils.copyProperties 期间从字符串转换为枚举
- javascript - jQuery PJAX 插件在 HTML 网站中不起作用?