用于编辑一堆文件的 Bash 脚本
为了处理一堆数据并准备好将其插入到我们的数据库中,我们生成了一堆 shell 脚本。每行大约有 15 行,每行对应数据所在的表。在最近的一批导入中,某些导入文件无法进入某个特定表。因此,我有一堆 shell 脚本(大约 600 个),我需要注释掉前 7 行,然后重新运行该文件。该文件夹中有大约 6000 个 shell 脚本,没有任何关于特定文件的信息可以告诉我它是否需要编辑。我有一个从数据库输出中提取的文件的列表。
那么,我如何编写一个 bash 脚本(或其他更好的方法)来获取此文件名列表,并为每个文件名注释掉前 7 行,然后运行该脚本?
编辑:
#!/usr/bin/env sh
cmd1
cmd2
cmd3
cmd4
cmd5
cmd6
cmd7
cmd8
不确定它的可读性如何。基本上,前 7 行(不包括第一行)需要在其开头添加 #。注意:这些文件已被编辑,以使每行更短,并部分切断从 VIM 中的复制。但在每个文件的主要部分中,有一行以 echo 开头,然后是一行以 sqlldr 开头
To process a bunch of data and get it ready to be inserted into our database, we generate a bunch of shell scripts. Each of them has about 15 lines, one for each table that the data is going. One a recent import batch, some of the import files failed going into one particular table. So, I have a bunch of shell scripts (about 600) where I need to comment out the first 7 lines, then rerun the file. There are about 6000 shell scripts in this folder, and nothing about a particular file can tell me if it needs the edit. I've got a list of which files that I pulled from the database output.
So how do I write a bash script (or anything else that would work better) to take this list of file names and for each of them, comment out the first 7 lines, and run the script?
EDIT:
#!/usr/bin/env sh
cmd1
cmd2
cmd3
cmd4
cmd5
cmd6
cmd7
cmd8
Not sure how readable that is. Basically, the first 7 lines (not counting the first line) need to have a # added to the beginning of them. Note: the files have been edited to make each line shorter and partially cut off copying out of VIM. But in the main part of each file, there is a line starting with echo, then a line starting with sqlldr
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 sed,您可以指定要更改的文件中的行号范围。
您可能希望在所有这些脚本上运行之前对其进行测试...
编辑:更改行号以反映注释。
Using sed, you can specify a line number range in the file to be changed.
You may wish to test this before running it on all of those scripts...
EDIT: changed the lines numbers to reflect comments.
粗略地说:
假设您不关心检查竞争条件等。
Roughly speaking:
Assuming you don't care about checking for race conditions etc.
ex 似乎是为你想做的事而生的。
例如,要编辑一个文件,请使用此处的文档:
这将注释掉“test.txt”中的前 12 行。对于您的示例,您可以尝试
"$FILE"
或类似的(包括引号!)。然后以通常的方式运行它们,即
./"$FILE"
编辑:
$SHELL "$FILE"
可能是运行它们的更好方法(来自上述之一评论者)。ex seems made for what you want to do.
For instance, for editing one file, with a here document:
That'll comment out the first 12 lines in "test.txt". For your example you could try
"$FILE"
or similar (including quotes!).Then run them the usual way, i.e.
./"$FILE"
edit:
$SHELL "$FILE"
is probably a better approach to run them (from one of the above commenters).最终您将需要使用 linux 命令 sed。无论您需要在脚本中放置什么逻辑,您都知道。但你的脚本最终会调用 sed。 http://lowfatlinux.com/linux-sed.html
Ultimately you're going to want to use the linux command sed. Whatever logic you need to place in the script, you know. But your script will ultimately call sed. http://lowfatlinux.com/linux-sed.html