[英]How to loop through a file in bash starting at a custom line?
I have a file for which I want to loop through just a specific part of using a bash script.我有一个文件,我只想循环使用 bash 脚本的特定部分。 Here is an example of it's format:以下是其格式的示例:
FILE 1
(exclude)
.
(Start loop from here)
.
.
.
(stop)
How do I loop through the file starting at the line I need and stop where required ?如何从我需要的行开始遍历文件并在需要的地方停止? I assume I will need a for loop for this situation ?我假设我需要一个 for 循环来处理这种情况? I do know the pattern for the first line if grep is required.如果需要 grep,我确实知道第一行的模式。 Or the line number which is probably constant for all of my files (say starts at 5, stops at 9)或者我所有文件的行号可能是恒定的(比如从 5 开始,在 9 停止)
Set up a state machine using AWK that toggles processing of the lines ON when the start pattern is found and either使用 AWK 设置状态机,在找到起始模式时切换行的处理,或者
or或者
awk '/stop/ {process = 0}
process {print $1, $5}
/start/ {process = 1}' inputfile
This could be put on one line, but I've broken it apart here for easier reading.这可以放在一行上,但为了更容易阅读,我在这里把它分开了。
process
is being used as a boolean variable. process
被用作布尔变量。 If it's true, print a couple of fields from the current line.如果为真,则从当前行打印几个字段。
/start/
and /stop/
represent regular expressions to match strings that you will use to delimit the section(s) of the file you wish to process. /start/
和/stop/
表示匹配字符串的正则表达式,您将使用这些字符串来分隔要处理的文件部分。 You could instead use line numbers, for example you could change the /start/
test to NR == 120
to begin processing on the 120th line.您可以改为使用行号,例如您可以将/start/
test 更改为NR == 120
以在第 120 行开始处理。
If you want the beginning and ending lines to be included in the processing, just move the process
line after the /start/
line and the /stop/
line to be the last (essentially, reverse their order).如果您希望将开始和结束行包含在处理中,只需将process
行移动到/start/
行之后,并将/stop/
行作为最后一行(基本上,颠倒它们的顺序)。
As I've shown it, this script will process multiple start/stop blocks within the file.正如我所展示的,这个脚本将处理文件中的多个启动/停止块。 If you want to only process the first (or only) you can change the /stop/
line to:如果您只想处理第一个(或唯一),您可以将/stop/
行更改为:
/stop/ {exit)
Since you said that you have the line numbers, you can use tail
.既然你说你有行号,你可以使用tail
。 For instance, to loop through the file starting at line 42, you could do a例如,要遍历从第 42 行开始的文件,您可以执行以下操作
tail -n +42 YOUR_FILE | while line
do
process_line "$line"
done
Example: extract user name and home directory from /etc/passwd
starting at mail
and ending at nobody
.示例:从/etc/passwd
提取用户名和主目录,从mail
开始,到nobody
结束。
cat /etc/passwd |
sed -n '/^mail/,/^nobody/p' |
while IFS=: read user pw uid gid geco home sh line; do
echo "$user: $home"
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.