如何读取文件的第N行并将其打印到新文件中?

发布于 2024-12-13 21:27:27 字数 454 浏览 1 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

虐人心 2024-12-20 21:27:27

sed 可以帮助你。

回想一下,sed 通常会处理文件中的所有行并打印文件中的每一行。

您可以关闭该功能,并让 sed 通过匹配模式或行号来仅打印感兴趣的行。

因此,要打印文件 2 的第 2 行,您可以说

sed -n '2p' file2 > newFile2

要打印第 2 行然后停止处理,请添加 q(表示退出)命令(您还需要大括号将 2 个命令组合在一起),即

sed -n '2{p;q;}' file2 > newFile2

(如果您正在处理大文件,这可以节省相当多的时间)。

为了使其更通用,您可以将数字更改为保存数字的变量,即

  lineNo=3
  sed -n "${lineNo}{p;q;}" file3 > newFile3

如果您希望所有切片行进入1个文件,则使用shell'append-redirection',即

 for lineNo in 1 2 3 4 5 ; do
     sed -n  "${lineNo}{p;q;}" file${lineNo} >> aggregateFile
 done

其他帖子,使用 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

sed -n '2p' file2 > newFile2

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.

sed -n '2{p;q;}' file2 > newFile2

(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.

  lineNo=3
  sed -n "${lineNo}{p;q;}" file3 > newFile3

If you want all of your sliced lines to go into 1 file, then use the shells 'append-redirection', i.e.

 for lineNo in 1 2 3 4 5 ; do
     sed -n  "${lineNo}{p;q;}" file${lineNo} >> aggregateFile
 done

The other postings, with using the results of find ... to drive your filelist, are an excellent approach.

I hope this helps.

雨的味道风的声音 2024-12-20 21:27:27

这是一种方法:

awk "NR==$YEAR" $file

Here is one way to do it:

awk "NR==$YEAR" $file
故人的歌 2024-12-20 21:27:27

使用 find 找到您想要的文件,然后使用 sed 提取您想要的文件:

find foo -type f -name year* |
while read file; do
    line=$(echo $file | sed 's/.*year\([0-9]*\)$/\1/')
    sed -n -e "$line {p; q}" $file
done

这种方法:

  • 使用 find 生成文件列表名称以字符串“year”开头。
  • 将文件列表通过管道传送到 while 循环以避免长命令行
  • 使用 sed 从文件名中提取所需的行号
  • 使用 sed仅打印所需的行,然后立即退出。 (您可以省略 q 并只编写 ${line}p ,这可以工作,但可能会降低 $file 的效率。另外,sed 的所有版本可能并不完全支持 q。)

但对于名称中带有空格的文件,它无法正常工作。

Use find to locate the files you want, and then sed to extract what you want:

find foo -type f -name year* |
while read file; do
    line=$(echo $file | sed 's/.*year\([0-9]*\)$/\1/')
    sed -n -e "$line {p; q}" $file
done

This approach:

  • Use find to produce a list of files with a name starting with the string "year".
  • Pipes the file list to a while loop to avoid long command lines
  • Uses sed to extract the desired line number from the name of the file
  • Uses sed to print just the desired line and then immediately quit. (You can leave out the q 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 of sed.)

It will not work properly for files with spaces in their names though.

眼眸 2024-12-20 21:27:27
1.time head -5 emp.lst tail -1
It has taken time for execution is
real 0m0.004s
user 0m0.001s
sys 0m0.001s

or

2.awk 'NR==5' emp.lst
It has taken time for execution is
real 0m0.003s
user 0m0.000s
sys 0m0.002s

or 

3.sed -n '5p' emp.lst
It has taken time for execution is
real 0m0.001s
user 0m0.000s
sys 0m0.001s

or 

4.using some cute trick we can get this with cut command
cut -d “
“ -f 5 emp.lst
# after -d press enter ,it means delimiter is newline
It has taken time for execution is
real 0m0.001s
1.time head -5 emp.lst tail -1
It has taken time for execution is
real 0m0.004s
user 0m0.001s
sys 0m0.001s

or

2.awk 'NR==5' emp.lst
It has taken time for execution is
real 0m0.003s
user 0m0.000s
sys 0m0.002s

or 

3.sed -n '5p' emp.lst
It has taken time for execution is
real 0m0.001s
user 0m0.000s
sys 0m0.001s

or 

4.using some cute trick we can get this with cut command
cut -d “
“ -f 5 emp.lst
# after -d press enter ,it means delimiter is newline
It has taken time for execution is
real 0m0.001s
如若梦似彩虹 2024-12-20 21:27:27

始终有效的最佳方法是提供 2 个参数:

$ touch myfile
$ touch mycommand
$ chmod +x mycommand
$ touch yearfiles
$ find / -type f -name year* >> yearfiles
$ nano mycommand
$ touch foo

键入:

#/bin/bash
head -n $1 $2 >> myfile
less -n 1 myfile >> foo

使用 ^Xy,然后输入进行保存。然后运行 ​​mycommand:

$ ./mycommand 2 yearfiles
$ cat foo
year2

假设您的 year 文件是:

year1, year2, year3

另外,现在您已经完成设置,从现在开始您只需使用 $ ./mycommand LINENUMBER FILENAME 即可。

The best way that always works, provided you provide 2 arguments:

$ touch myfile
$ touch mycommand
$ chmod +x mycommand
$ touch yearfiles
$ find / -type f -name year* >> yearfiles
$ nano mycommand
$ touch foo

Type this:

#/bin/bash
head -n $1 $2 >> myfile
less -n 1 myfile >> foo

Use ^X, y, and enter to save. Then run mycommand:

$ ./mycommand 2 yearfiles
$ cat foo
year2

Presuming your year files are:

year1, year2, year3

Additionally, now you have setup, you just have to use $ ./mycommand LINENUMBER FILENAME from now on.

淡水深流 2024-12-20 21:27:27

干得好

sed ${index}'q;d' ${input_file} > ${output_file}

Here you go

sed ${index}'q;d' ${input_file} > ${output_file}
诗化ㄋ丶相逢 2024-12-20 21:27:27

您的任务有两个子任务:查找所有年份文件的名称,然后提取第 N 行。考虑以下脚本:

for file in `find foo -name 'year*'`; do
     YEAR=`echo $file | sed -e 's/.*year\([0-9]*\)$/\1/'`
     head -n $YEAR $file | tail -n 1
done

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:

for file in `find foo -name 'year*'`; do
     YEAR=`echo $file | sed -e 's/.*year\([0-9]*\)$/\1/'`
     head -n $YEAR $file | tail -n 1
done

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文