【发布时间】:2011-11-21 08:12:58
【问题描述】:
我需要一个 mac osx 以这种方式工作的 bash 脚本:
./script.sh * folder/to/files/
#
# or #
#
./script.sh xx folder/to/files/
这个脚本
- 读取文件列表
- 打开每个文件并读取每一行
- 如果行以相同的字母('*' 模式)或自定义字母('xx')结尾,则
删除行和重新保存文件 - 备份原文件
我的第一个方法是:
#!/bin/bash
# ck init params
if [ $# -le 0 ]
then
echo "Usage: $0 <letters>"
exit 0
fi
# list files in current dir
list=`ls BRUTE*`
for i in $list
do
# prepare regex
case $1 in
"*") REGEXP="^.*(.)\1+$";;
*) REGEXP="^.*[$1]$";;
esac
FILE=$i
# backup file
cp $FILE $FILE.bak
# removing line with same letters
sed -Ee "s/$REGEXP//g" -i '' $FILE
cat $FILE | grep -v "^$"
done
exit 0
但它并没有像我想要的那样工作......
怎么了?
我该如何修复这个脚本?
示例:
$cat BRUTE02.dat BRUTE03.dat
aa
ab
ac
ad
ee
ef
ff
hhh
$
如果我使用“*”,我希望所有以相同字母结尾的文件都是干净的。
如果我使用“ff”,我希望所有以“ff”结尾的文件都是干净的。
啊,它在 Mac OSx 上。请记住,sed 与经典的 linux sed 有点不同。
人 sed
sed [-Ealn] command [file ...] sed [-Ealn] [-e command] [-f command_file] [-i extension] [file...]
描述 sed 实用程序读取指定的文件或标准输入 如果没有指定文件,则修改列表指定的输入 的命令。这 然后将输入写入标准输出。
A single command may be specified as the first argument to sed.可以使用 -e 或 -f 选项指定多个命令。全部 命令被应用 按照指定的顺序输入到输入中,而不管它们的 产地。
The following options are available: -E Interpret regular expressions as extended (modern)正则表达式而不是基本正则表达式 (BRE)。 re_format(7) 手册页 全面描述了这两种格式。
-a The files listed as parameters for the ``w'' functions默认情况下在任何处理开始之前创建(或截断)。 -a 选项导致 sed 延迟打开每个文件,直到包含的命令 相关的“w”函数应用于一行输入。
-e command Append the editing commands specified by the command命令列表的参数。
-f command_file Append the editing commands found in the filecommand_file 到命令列表。编辑命令应该 每个都列在单独的行上。
-i extension Edit files in-place, saving backups with the specified扩展名。如果给出零长度扩展名,则不会备份 保存。不推荐 修改为在就地时提供零长度扩展 编辑文件,因为在某些情况下,您可能会面临损坏或部分内容的风险 磁盘空间在哪里 筋疲力尽等
-l Make output line buffered. -n By default, each line of input is echoed to the standard在所有命令都应用到它之后输出。然后 选项抑制了这一点 行为。
The form of a sed command is as follows: [address[,address]]function[arguments] Whitespace may be inserted before the first address and the命令的功能部分。
Normally, sed cyclically copies a line of input, not including它的终止换行符,进入模式空间,(除非有 还剩下什么 在 ``D'' 函数之后),应用所有命令 选择该模式空间的地址,将模式空间复制到 标准输出,追加- ing 一个换行符,并删除模式空间。
Some of the functions use a hold space to save all or part of the用于后续检索的模式空间。
还有什么?
很清楚我的问题?
谢谢。
【问题讨论】:
-
sed cmd 中这对单引号的用途是什么?
sed -Ee "s/$REGEXP//g" -i '' $FILE。您正在尝试做的事情以及如何做是直接的 sed 用法,我希望它可以在任何地方工作。 OsX 手动引用没有帮助,或者您从手册页中明确引用您认为与众不同的原因。最后,你写道,“不像我想要的那样工作”。请展示 1 你想要什么, 2 你得到什么。祝你好运。 -
MAC OSX 上的 Sed 不使用直接输出。使用
-i ''绕过检查。 -
'不使用直接输出'。请注意您提供的手册页中的这句话:
The input is then written to the standard output.这就像任何其他 sed ;-),老实说!祝你好运!
标签: regex string macos bash sed