将 unix 文件最后一行的第一个字符替换为文件名
我们需要一个 shell 脚本来检索当前目录中的所有 txt 文件,并检查每个文件是否为空文件或其中包含任何数据(我相信可以使用 wc 命令来完成)。
如果它是空的,则忽略它,因为在我们的情况下,该目录中的所有 txt 文件要么为空,要么包含大量数据,其中文件的最后一行将如下所示:
Z|11|21
We need a shell script that retrieves all txt files in the current directory and for each file checks if it is an empty file or contains any data in it (which I believe can be done with wc command).
If it is empty then ignore it else since in our condition, all txt files in this directory will either be empty or contain huge data wherein the last line of the file will be like this:
Z|11|21
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也就是说最后一行有字符
Z
然后|
然后一个整数
然后|
然后一个整数
然后是一定数量的|
符号。如果文件不为空,那么我们就假设它具有这种格式。最后一行之前的数据是乱码,对我们来说不是必需的,但最后一行之前至少有一行,即如果文件非空,则保证至少有两行。
我们需要一个代码,其中如果文件非空,则它会获取该文件,用“filename.txt”替换最后一行中的“Z”,并将新数据写入另一个文件(例如 tempfile)中。因此,最后一行将变为:
该行的其余部分保持不变。从临时文件中,取出最后一行,即
filename.txt|int|int|||||
并合并到最终文件中。 tempfile 的内容被清除,以便从同一目录中的下一个 filename.txt 接收数据。 Finalfile 具有该目录中所有非空 txt 文件最后几行的编辑版本。例如: file1.txt 的数据为
, file2.txt 的数据为
运行脚本后,file1.txt 的新数据变为
这将被写入一个新文件,例如 temp.txt,该文件最初为空。从那里,最后一行被合并到文件 Final.txt 中。所以,final.txt中的数据是:
这次合并后,temp.txt中的数据被清空了
file2.txt 的新数据变为
这将被写入同一个文件 temp.txt 中。从那里,最后一行被合并到同一个文件 Final.txt 中。
因此,final.txt 中的数据是
在考虑了 N 个返回为 txt 类型且非空且位于同一目录内的文件后,final.txt 中的数据变为
对于某些条件,我已经知道命令,例如
用于在文本类型的目录中查找文件,
用于获取最后一行并将其合并到另一个文件中
That is the last line has the character
Z
then|
thenan integer
then|
thenan integer
then certain numbers of|
symbols.If the file is not empty, then we just assume it to have this format. Data before the last line are garbled and not necessary for us but there will be at least one line before the last line, i.e. there will be at least two lines guaranteed if the file is non-empty.
We need a code wherein, if the file is non-empty, then it takes the file, replaces the 'Z' in the last line with 'filename.txt' and writes the new data into another file say tempfile. The last line will thus become as:
Remaining part of the line remains same. From the tempfile, the last line, i.e.,
filename.txt|int|int|||||
is taken out and merged into a finalfile. The contents of tempfile is cleared to receive data from next filename.txt in the same directory. finalfile has the edited version of the last lines of all non-empty txt files in that directory.Eg: file1.txt has data as
and file2.txt has data as
After running the script, new data of file1.txt becomes
This will be written into a new file say temp.txt which is initially empty. From there the last line is merged into a file final.txt. So, the data in final.txt is:
After this merging, the data in temp.txt is cleared
New data of file2.txt becomes
This will be written into the same file temp.txt. From there the last line is merged into the same file final.txt.
So, the data in final.txt is
After considering N number of files that was returned to be as of type txt and non-empty and within the same directory, the data in final.txt becomes
For some of the conditions, I already know the command, like
For finding files in a directory of type text,
For taking the last line and merging it into another file
您可以使用“sed”来替换“z”字符。您将处于循环中,因此您可以使用其中的文件名。这只是删除 Z,然后回显该行和文件名。
祝你好运。
编辑:
您是否先运行了
find
命令并看到了输出?当然,它在每行的开头都有一个./
。这会破坏 sed,因为 sed 使用/
作为分隔符。它也不适用于您的问题陈述,因为文件名之前没有额外的“/”。你说的是当前目录,你给出的命令将遍历ALL子目录。尝试简单一点并使用LS
。You can use 'sed' to replace the "z" character. You'll be in a loop, so you can use the filename that you have in that. This just removes the Z, and then echos the line and filename.
Good luck.
Edit:
Did you run your
find
command first, and see the output? It has of course a./
at the start of each line. That will break sed, since sed uses/
as a delimiter. It also will not work with your problem statement, which does not have an extra "/" before the filename. You said current directory, and the command you give will traverse ALL subdirectories. Try being simple and usingLS
.