简体   繁体   English

了解sed命令

[英]Understanding the sed command

I'm trying to change every first line in all files contained in a parent directory so that they inherit the pathname of the directory that they're in. For example I have a file with the format: 我正在尝试更改父目录中包含的所有文件中的第一行,以便它们继承其所在目录的路径名。例如,我有一个格式为:

2000-01-18 2000-01-18
Tuesday 星期二
Livingston 利文斯顿
42178 42178

This particular file is in a directory named 18, inside another directory named 01, which is in another directory named 2000, which is in a directory called filesToSort. 该特定文件位于名为18的目录中,位于名为01的另一个目录中,该目录位于名为2000的另一个目录中,该目录名为filesToSort。

I managed to use this code as a console command to change the first line of the file: 我设法将以下代码用作控制台命令来更改文件的第一行:

perl -pi -w -e 's/2000-01-18/Test/g;' ff_1177818640

This changed the file to 这将文件更改为

Test 测试
Tuesday 星期二
Livingston 利文斯顿
42178 42178

Is it possible for me to change the "date" in this command to select all dates, I tried to use it like this: 我是否可以在此命令中更改“日期”以选择所有日期,所以我尝试使用它:

perl -pi -w -e 's/*/Test/g;' ff_1177818640

But it didn't like that at all. 但这一点都不喜欢。
My current though process is that if I can make this command select all dates in the initial input, then some how find a way to implement the pathname into the second part where I currently have "Test" using something like this: 我目前的处理过程是,如果我可以使此命令选择初始输入中的所有日期,那么一些方法将找到一种方法来实现到第二部分的路径名,在第二部分中,我目前使用以下方法进行“测试”:

path=/filesToSort/2000/01/18/ff_1177818640
file=$(basename "$path")

I should in theory be able to run this entire code through my parent directory and all sub directories, therefore changing every date value in the files, which apear on line 1 of every single file, in these directories to mirror the file path that they're in, effectively turning a file that looks like this: 从理论上讲,我应该能够通过父目录和所有子目录运行整个代码,因此可以更改这些目录中文件的每个日期值(该文件出现在每个文件的第1行),以反映它们的文件路径重新输入,有效地打开一个看起来像这样的文件:

2000-xx-18 2000-xx-18
Tuesday 星期二
Livingston 利文斯顿
42178 42178

Contained in directory /filesToSort/2000/01/18 into this: 包含在目录/ filesToSort / 2000/01/18中:

2000/01/18 2000/01/18
Tuesday 星期二
Livingston 利文斯顿
42178 42178

I'm not sure if I'm just using the sed command wrong here and that there is another command that I should be using instead but I've been trying to get this to work for 4 hours now and I can't seem to nail it. 我不确定我是否在这里错误地使用了sed命令,是否应该使用另一个命令来代替,但是我一直在尝试使它工作4个小时,而我似乎无法给办了。 Thanks in advance for the help! 先谢谢您的帮助!

Looks to me that what you want to do is basically translate "-" for "/". 在我看来,您要做的基本上是将“-”转换为“ /”。 You could, find the file, take a backup of it (always a good idea) and then use : 您可以找到文件,对其进行备份(总是一个好主意),然后使用:

    sed 's|-|/|g' <path/backup_file >path/modified_file

That said, if it is the same assignement, you could use that command to copy to its new directory and modified the file at the same time. 也就是说,如果分配相同,则可以使用该命令将其复制到新目录并同时修改文件。

You haven't posted a sed command, so it's hard to know what will work. 您尚未发布sed命令,因此很难知道它将起作用。 Let's take this in small steps. 让我们一步步来做。 Try this: 尝试这个:

sed -i '1s/^/X/' ff_1177818640

and see if that modifies the file (adding 'X' to the beginning of the first line). 并查看是否修改了该文件(在第一行的开头添加“ X”)。 If your version of sed doesn't like that, try this: 如果您的sed版本不满意,请尝试以下操作:

sed -i "" '1s/^/X/' ff_1177818640

Once you have the syntax working, we must tackle the problem of converting a path into a date. 一旦语法生效,我们必须解决将路径转换为日期的问题。 Try this: 尝试这个:

echo some/path/filesToSort/2000/01/18/ff_1177818640 | sed 's|.*/filesToSort/||; s|/[^/]*$||'

If that produces "2000/01/18", post a comment, and we can put it all together. 如果产生“ 2000/01/18”,请发表评论,我们可以将其放在一起。

EDIT: putting it all together. 编辑:将它们放在一起。 Abracadabra! 胡言乱语!

find . -type f -exec sed -i "1{s|.*|{}|;s|.*/filesToSort/||;s|/[^/]*$||;}" {} \;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM