我需要从单词中删除字母 X:
例如:我需要剪掉 Star Wars 中的第一个字母,munich 中的第四个字母,...
1 star wars
4 munich
5 casino royale
7 the fast and the furious
52 a fish called wanda
to
tar wars
munch
casio royale
the fat and the furious
a fish called wanda
我已经尝试过使用 cut,但没有成功。
这是我的命令:
sed 's/^\([0-9]*\) \(.*\)/ echo \2 | cut -c \1/'
所以它给了我这个输出:
echo star wars | cut -c 5
echo munich | cut -c 5
echo casino royale | cut -c 5
echo the fast and the furious | cut -c 5
echo a fish called wanda | cut -c 52
比我把它发送到 bash 还要好。我只得到这个词的第 X 个字母。
我需要使用 sed 和其他命令进行练习。但我不能使用 awk 或 perl。
Thanks
您可以只使用 bash 及其参数扩展:
while read n s ; do
echo "${s:0:n-1}${s:n}"
done < input.txt
如果您需要一行,只需删除换行符并添加分号:
while read n s ; do echo "${s:0:n-1}${s:n}" ; done < input.txt
如果你确实需要使用sed
and cut
,它也是可行的,但可读性稍差:
cat -n input.txt \
| sed 's/\t\([0-9]\+\).*/s=\\(.\\{\1\\}\\).=\\1=/' \
| sed -f- <(sed 's/[0-9]*//' input.txt) \
| cut -c2-
解释:
- 给行编号
- 将每一行转换为 sed 命令,该命令搜索给定数量的字符并删除其后的字符
- 在删除数字的原始文件上运行生成的 sed 命令
- 删除多余的前导空格
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)