UNIX\LINUX:如何在文件的每一行中添加目录文本? [英] UNIX\LINUX: How to add a directory text to each line inside a file?

查看:198
本文介绍了UNIX\LINUX:如何在文件的每一行中添加目录文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UNIX\LINUX:如何向文件中的每一行添加目录文本?

UNIX\LINUX: How to add a directory text to each line inside a file?

注意:我只是在使用shell(LINUX REDHAT EPIC的CMD工具)没有其他...

NOTE: I am just using shell(CMD TOOL OF LINUX REDHAT EPIC) no other...

您看到我有很多日志文件(.txt.gz),我可以通过以下方式打开所有文件:

You see I have many log files(.txt.gz) and I was able to open all of them just by using:

foreach i (./*/*dumpfiles.txt.gz_*)
> foreach? zcat $i
> foreach? grep "-e" $i
> foreach? END

含义:我正在浏览所有这些文件夹,找到文件 dumpfiles.txt.gz_

Meaning I am going through all those folders finding a file dumpfiles.txt.gz_

输出如下:

0x4899252 move x -999
0x4899231 move y -0
0x4899222 find scribe
0x4899231 move x -999

等。

问题是我需要将目录设置为文件的每一行...

The problem is that I need the directory to be set to each line of the file... I could get the directory by the command pwd.

我的问题是如何在文件的每一行上添加目录名称?
示例:

(directory) (per line of all files)
machine01   0x4899252 move x -999
machine01   0x4899231 move y -0
machine09   0x4899222 find scribe
machine09   0x4899231 move x -999

等。

我尝试使用$ sed,但找不到解决方法...:(

I tried using $ sed but I cant find the solution... :(

谢谢...

解决方案

下面是一个Perl脚本,它可以满足您的要求(输入是文件名):

here's a little perl script that does what you ask for (input is the filename):

$file = shift;
$path = `pwd`;
chomp($path);
open(TRY, "< $file");
while ($line = <TRY>) { print ($path . $line);}
close(TRY);

当然,这会打印到屏幕上,但是您可以将其倒入文件并在最后重命名将该脚本复制到 $ file

of course this prints to the screen, but you can pour it to file and rename it at the end of the script to $file

如果要在整个目录上运行它,并且可以向下运行

if you want to run it on the entire dir and downyou can run

find . -exec scriptname {} \;

如果希望仅将其放在当前目录中,则需要添加 -maxdepth 1 标记到'。'之后的 find

if you want it to be on the current dir only, you need to add a -maxdepth 1 flag to the find after the '.'

更新:

这也有效(没有脚本,只有一个shell行):

this also works (with no script, just a shell line):

perl -pi -e 's/^/$ENV{PWD} /g' 

这篇关于UNIX\LINUX:如何在文件的每一行中添加目录文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆