bash 如何将 sed 命令组合到 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20151483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I combine sed commands to a shell script
提问by Ranjith Siji
I Have to Operate the following sed commands over a file to remove some lines in that file. How can I make this a shell scipt with the file name as a variable. Or is there any simple way to do this as a shell script
我必须在文件上操作以下 sed 命令以删除该文件中的某些行。我怎样才能使它成为一个以文件名作为变量的 shell scipt。或者是否有任何简单的方法可以将其作为 shell 脚本执行
sed -i '/^Total/d' delhi222517.txt
sed -i '/^CBSE/d' delhi222517.txt
sed -i '/^Keyword wise/d' delhi222517.txt
sed -i '/^wise/d' delhi222517.txt
sed -i '/^Select A/d' delhi222517.txt
sed -i '/^Enter A/d' delhi222517.txt
sed -i '/^(Keyword/d' delhi222517.txt
sed -i '/^State Name/d' delhi222517.txt
sed -i '/^SNo/d' delhi222517.txt
sed -i '/^Disclaimer/d' delhi222517.txt
sed -i '/^provided/d' delhi222517.txt
sed -i '/^at$/d' delhi222517.txt
sed -i '/^Designed/d' delhi222517.txt
sed -i '/^National/d' delhi222517.txt
sed -i '/^$/d' delhi222517.txt
sed -i '/^\t$/d' delhi222517.txt
sed -i '/^\s$/d' delhi222517.txt
sed -i '/^ /d' delhi222517.txt
sed -i '/^ /d' delhi222517.txt
sed -i 's/^\([0-9]\)/--/g' delhi222517.txt
回答by rodrigo
The variable thing is easy:
变量的事情很简单:
F=delhi222517.txt
sed -i '/^Total/d' "$F"
...
Or if you want to pass the name of the file as argument to your script:
或者,如果您想将文件名作为参数传递给脚本:
F=""
sed -i '/^Total/d' "$F"
...
But it is better to use the sed
options to call it only once. You can use:
但最好使用sed
选项只调用一次。您可以使用:
sed -i \
-e '/^Total/d' \
-e '/^CBSE/d' \
-e '/^Keyword wise/d' \
... \
delhi222517.txt
Or you can write a file with the full script:
或者您可以使用完整脚本编写一个文件:
sed -i -f script.sed delhi222517.txt
Or if you feel geek enough, you can use the standard input:
或者,如果您觉得自己够怪,可以使用标准输入:
sed -i -f - delhi222517.txt << EOF
/^Total/d
/^CBSE/d
/^Keyword wise/d
...
EOF
回答by Todd A. Jacobs
On the Command Line
在命令行上
On the command line, you can separate sed commands with semi-colons or with multiple expression arguments. As generic examples:
在命令行上,您可以使用分号或多个表达式参数来分隔 sed 命令。作为通用示例:
# Using Semi-Colons
sed -i 's/foo/bar/; s/baz/quux/' infile
# Using Multiple Expressions
sed -i -e 's/foo/bar/' -e 's/baz/quux/' infile
Write a Full-Fledged Sed Script
编写成熟的 Sed 脚本
In general, though, if your commands are numerous, stop using one-liners and build a full-fledged sed script. For example, you could create a file named /tmp/foo.sed
containing the following commands from your question:
但是,总的来说,如果您的命令很多,请停止使用单行程序并构建成熟的 sed 脚本。例如,您可以创建一个名为的文件,/tmp/foo.sed
其中包含您的问题中的以下命令:
/^Total/d
/^CBSE/d
/^Keyword wise/d
/^wise/d
/^Select A/d
/^Enter A/d
/^(Keyword/d
/^State Name/d
/^SNo/d
/^Disclaimer/d
/^provided/d
/^at$/d
/^Designed/d
/^National/d
/^$/d
/^\t$/d
/^\s$/d
/^ /d
/^ /d
s/^\([0-9]\)/--/g
Then invoke your commands all at once. For example, using GNU sed:
然后一次性调用所有命令。例如,使用 GNU sed:
infile='delhi222517.txt'
script='/tmp/foo.sed'
sed --in-place --file="$script" "$infile"
回答by anubhava
Well you can put them in a a shell script like this:
那么你可以像这样把它们放在一个 shell 脚本中:
#!/bin/bash
# some sanity checks
file=""
sed -i '/^Total/d' "$file"
sed -i '/^CBSE/d' "$file"
sed -i '/^Keyword wise/d' "$file"
sed -i '/^wise/d' "$file"
#.. more sed commands
btw your various sed commands can be combined into 1 or fewer sed command using reges like:
顺便说一句,您可以使用以下 reges 将各种 sed 命令组合成 1 个或更少的 sed 命令:
sed -r -i '/^(Total|CBSE)/d' "$file"
回答by Jotne
Using awk
you can do all in go:
使用awk
你可以做所有的事情:
file=delhi222517.txt
awk '!/^(Total|CBSE|Keyword wise|wise)/' "$file"
回答by Kevin
If your script is not going to run any program other than sed your file, then this may be the cleanest way to set it up:
如果您的脚本除了 sed 您的文件之外不会运行任何程序,那么这可能是设置它的最干净的方法:
#!/bin/sed -f # <- run the file passed to the program from the command-line
/^Total/Id # /I is the case-insensitive flag, replacing sed -i
/^CBSE/Id
/^Keyword wise/Id
/^wise/Id
/^Select A/Id
/^Enter A/Id
...
Make the above script executable, and then just pass it the filename that you wish to convert: ./mysedscript delhi222517.txt
使上述脚本可执行,然后将您要转换的文件名传递给它:./mysedscript delhi222517.txt