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

与世无争的帅哥 提交于 2019-12-08 13:54:23

问题


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

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

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

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

The the output is like:

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

etc..

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.

The question to my problem is how to add a directory name on each line of the file? Example:

(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

etc..

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

Thanks...


回答1:


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

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 {} \;

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

update:

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

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


来源:https://stackoverflow.com/questions/16803855/unix-linux-how-to-add-a-directory-text-to-each-line-inside-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!