本人最近在学习shell脚本,资料中有很多实例,不过格式都是行前有数字,还会有空格,编辑起来很麻烦,就想到用shell脚本处理:
原文件:
1 #!/bin/bash
2 # 从 /etc/fstab 中读行
3
4 File=/etc/fstab
5
6{
7 read line1
8 read line2
9 } < $File
10
11 echo "First line in $File is:"
12 echo "$line1"
13 echo
14 echo "Second line in $File is:"
15 echo "$line2"
16
17 exit 0
脚本文件:
#!/bin/bash
# 从 /etc/fstab 中读行
File=/home/library/test_shell/source.sh #原文件,也可以从参数中获取
cat $File | sed 's/^[[:space:]]*//' | sed 's/^[0-9]*//' >result.text
#运用管道先 去除空格,再去除数字,结果输出到result.txt中
exit 0
结果:
#!/bin/bash
# 从 /etc/fstab 中读行
File=/etc/fstab
{
read line1
read line2
} < $File
echo "First line in $File is:"
echo "$line1"
echo
echo "Second line in $File is:"
echo "$line2"
exit 0
shell脚本,去除空格与数字
最新推荐文章于 2024-04-29 15:20:15 发布