如何读取文件的第N行并将其打印到新文件中?
我有一个名为 foo 的文件夹。 Foo 还有一些其他文件夹,其中可能包含子文件夹和文本文件。我想找到以名称年份开头的每个文件,并读取其第 N 行并将其打印到新文件中。例如 foo 有一个名为year1的文件,子文件夹有名为year2、year3等的文件。程序会将year1的第一行打印到名为writeout的文件中,然后将year2的第二行打印到文件writeout等。
我也不太明白如何对文件进行 for 循环。
到目前为止,我已经:
#!/bin/bash
for year* in ~/foo
do
Here I tried writing some code using the sed command but I can't think of something else.
done
我还在终端中收到一条消息,上面写着“年份*”不是有效的标识符。 有什么想法吗?
I have a folder called foo. Foo has some other folders which might have sub folders and text files. I want to find every file which begins with the name year and and read its Nth line and print it to a new file. For example foo has a file called year1 and the sub folders have files called year2, year3 etc. The program will print the 1st line of year1 to a file called writeout, then it will print the 2nd line of year2 to the file writeout etc.
I also didn't really understand how to do a for loop for a file.
So far I have:
#!/bin/bash
for year* in ~/foo
do
Here I tried writing some code using the sed command but I can't think of something else.
done
I also get a message in the terminal which says `year*' not a valid identifier.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
sed 可以帮助你。
回想一下,sed 通常会处理文件中的所有行并打印文件中的每一行。
您可以关闭该功能,并让 sed 通过匹配模式或行号来仅打印感兴趣的行。
因此,要打印文件 2 的第 2 行,您可以说
要打印第 2 行然后停止处理,请添加 q(表示退出)命令(您还需要大括号将 2 个命令组合在一起),即
(如果您正在处理大文件,这可以节省相当多的时间)。
为了使其更通用,您可以将数字更改为保存数字的变量,即
如果您希望所有切片行进入1个文件,则使用shell'append-redirection',即
其他帖子,使用
find ...
的结果来驱动文件列表是一种很好的方法。我希望这有帮助。
Sed can help you.
Recall that sed will normally process all lines in a file AND print each line in the file.
You can turn off that feature, and have sed only print lines of interest by matching a pattern or line number.
So, to print the 2nd line of file 2, you can say
To print the 2nd line and then stop processing add the q (for quit) command (you also need braces to group the 2 commands together), i.e.
(if you are processing large files, this can be quite a time saving).
To make that more general, you can change the number to a variable that will hold a number, i.e.
If you want all of your sliced lines to go into 1 file, then use the shells 'append-redirection', i.e.
The other postings, with using the results of
find ...
to drive your filelist, are an excellent approach.I hope this helps.
这是一种方法:
Here is one way to do it:
使用
find
找到您想要的文件,然后使用sed
提取您想要的文件:这种方法:
find
生成文件列表名称以字符串“year”开头。while
循环以避免长命令行sed
从文件名中提取所需的行号sed
仅打印所需的行,然后立即退出。 (您可以省略q
并只编写${line}p
,这可以工作,但可能会降低$file
的效率。另外,sed
的所有版本可能并不完全支持q
。)但对于名称中带有空格的文件,它无法正常工作。
Use
find
to locate the files you want, and thensed
to extract what you want:This approach:
find
to produce a list of files with a name starting with the string "year".while
loop to avoid long command linessed
to extract the desired line number from the name of the filesed
to print just the desired line and then immediately quit. (You can leave out theq
and just write${line}p
which would work but be potentially less efficient of$file
is big. Also,q
may not be fully supported on all versions ofsed
.)It will not work properly for files with spaces in their names though.
始终有效的最佳方法是提供 2 个参数:
键入:
使用
^X
、y
,然后输入进行保存。然后运行 mycommand:假设您的
year
文件是:另外,现在您已经完成设置,从现在开始您只需使用
$ ./mycommand LINENUMBER FILENAME
即可。The best way that always works, provided you provide 2 arguments:
Type this:
Use
^X
,y
, and enter to save. Then run mycommand:Presuming your
year
files are:Additionally, now you have setup, you just have to use
$ ./mycommand LINENUMBER FILENAME
from now on.干得好
Here you go
您的任务有两个子任务:查找所有年份文件的名称,然后提取第 N 行。考虑以下脚本:
find 调用在目录 foo 中查找与您匹配的文件。第二行仅从文件名中提取文件名末尾的数字。然后,第三行从文件中提取前 N 行,仅保留前 N 行中的最后一行(读作:仅第 N 行)。
Your task has two sub-tasks: Find the name of all the year files, and then extract the Nth line. Consider the following script:
The find call finds the matching files for you in the directory foo. The second line extracts only the digits at the end of the filename from the filename. The third line then extracts the first N lines from the file, keeping only the last of the first N lines (read: only the Nth line).