简体   繁体   English

Bash脚本编辑一堆文件

[英]Bash script to edit a bunch of files

To process a bunch of data and get it ready to be inserted into our database, we generate a bunch of shell scripts. 为了处理一堆数据并将其准备好插入到我们的数据库中,我们生成了一堆shell脚本。 Each of them has about 15 lines, one for each table that the data is going. 他们每个人都有大约15行,每个行将用于数据表。 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. 因此,我有一堆shell脚本(大约600个),需要在其中注释掉前7行,然后重新运行该文件。 There are about 6000 shell scripts in this folder, and nothing about a particular file can tell me if it needs the edit. 此文件夹中大约有6000个Shell脚本,关于某个特定文件的信息无法告诉我是否需要编辑。 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? 那么,我该如何编写bash脚本(或其他效果更好的脚本)来获取此文件名列表,并为每个文件名注释掉前7行并运行该脚本?

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. 基本上,前7行(不计算第一行)需要在其开头添加#。 Note: the files have been edited to make each line shorter and partially cut off copying out of VIM. 注意:已对文件进行了编辑,以使每一行都更短,并且部分截取了从VIM中复制的内容。 But in the main part of each file, there is a line starting with echo, then a line starting with sqlldr 但是在每个文件的主要部分中,都有一行以echo开头,然后一行以sqlldr开头

Using sed, you can specify a line number range in the file to be changed. 使用sed,可以在要更改的文件中指定行号范围。

#!/bin/bash

while read line
do
    # add a comment to beginning of lines 1 - 7 and rename the script
    sed '3,9 s/^/#/' $line > $line.new
    exec $line.new
done < "filelist.txt"

You may wish to test this before running it on all of those scripts... 您可能希望先在所有这些脚本上运行它之前对其进行测试...

EDIT: changed the lines numbers to reflect comments. 编辑:更改行号以反映注释。

Roughly speaking: 粗略地说:

#!/bin/sh
for file in "$@"
do
    out=/tmp/$file.$$
    sed '2,8s/^/#/' < $file > $out
    $SHELL $out
    rm -f $out
done

Assuming you don't care about checking for race conditions etc. 假设您不关心检查比赛条件等。

Ultimately you're going to want to use the linux command sed. 最终,您将要使用linux命令sed。 Whatever logic you need to place in the script, you know. 知道您需要在脚本中放置什么逻辑。 But your script will ultimately call sed. 但是您的脚本最终将调用sed。 http://lowfatlinux.com/linux-sed.html http://lowfatlinux.com/linux-sed.html

ex seems made for what you want to do. 前似乎为您想要做的事。

For instance, for editing one file, with a here document: 例如,要编辑一个文件,请使用here文档:

#!/bin/sh

ex test.txt << END
1,12s/^/#/
wq
END

That'll comment out the first 12 lines in "test.txt". 这将注释掉“ test.txt”中的前12行。 For your example you could try "$FILE" or similar (including quotes!). 对于您的示例,您可以尝试"$FILE"或类似名称(包括引号!)。

Then run them the usual way, ie ./"$FILE" 然后以常规方式运行它们,即./"$FILE"

edit: $SHELL "$FILE" is probably a better approach to run them (from one of the above commenters). 编辑: $SHELL "$FILE"可能是一种更好的方法(从上面的评论者之一)运行它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM