如何在Linux中获取并格式化一堆文本文件(md5sums)的内容?

huangapple go评论52阅读模式
英文:

How do I get and format the contents of a bunch of text files (md5sums) in Linux?

问题

I have a bunch of md5 files, which have a hash and a path/filename. I want to output the hash and filename, but not the path.

Example file contents:

d7b7494a0602d91b391848dfaebec7ac  /home/develop/md5sums/file1.md5

Desired output:

d7b7494a0602d91b391848dfaebec7ac file1.md5

My current attempts from searches so far are:

cat *.md5 | xargs -0 sed -i 's/\/home\/develop\/md5sums\///g'

(Gives an error e.g. for file3.md5: sed: 2: "bf60fb26cbe1f4e9e4001aa ...": extra characters at the end of d command)

cat *.md5 | xargs -I '{}' sed -i 's/\/home\/develop\/md5sums\////g'

(Gives an error e.g. for file3.md5: sed: -I or -i may not be used with stdin)

I can probably figure out how to solve it with a for loop, but ideally I'd like to keep it as a piped one-liner if possible, and I think there should be a way for cat/xargs/sed to work, I just can't figure it out!

The path is hardcoded, so I don't feel the need to use basename, particularly as the md5 file contains more than just the path/file, which I think makes it more tricky!

英文:

I have a bunch of md5 files, which have a hash and a path/filename. I want to output the hash and filename, but not the path.

Example file contents:

d7b7494a0602d91b391848dfaebec7ac  /home/develop/md5sums/file1.md5

Desired output:

d7b7494a0602d91b391848dfaebec7ac file1.md5
dd036a1e1c16b3488309a75f80e5eb92 file2.md5
bf60fb26cbe1f4e9e4001aa485b89ff8 file3.md5

My current attempts from searches so far are:

cat *.md5 | xargs -0 sed -i 's/\/home\/develop\/md5sums\///g'

(Gives an error e.g. for file3.md5: sed: 2: "bf60fb26cbe1f4e9e4001aa ...": extra characters at the end of d command)

cat *.md5 | xargs -I '{}' sed -i 's/\/home\/develop\/md5sums\////g'

(Gives an error e.g. for file3.md5: sed: -I or -i may not be used with stdin)

I can probably figure out how to solve it with a for loop, but ideally I'd like to keep it as a piped one-liner if possible, and I think there should be a way for cat/xargs/sed to work, I just can't figure it out!

The path is hardcoded, so I don't feel the need to use basename, particularly as the md5 file contains more than just the the path/file, which I think makes it more tricky!

答案1

得分: 2

尝试:

sed 's: .*/: :' *.md5

输出:

d7b7494a0602d91b391848dfaebec7ac file1.md5

你的尝试没有成功,因为你使用了`-i`选项,这会原地编辑文件。但是,要做到这一点,sed需要直接访问这些文件。当从管道中读取cat时,sed只能看到文件的串联内容,无法知道它们的位置,因此无法编辑它们。这就是你得到的错误的含义。但是当我们直接将文件列表传递给sed时,它就能够编辑它们。
英文:

Try:

sed 's: .*/: :' *.md5

Output:

d7b7494a0602d91b391848dfaebec7ac file1.md5

Your attempts didn't work, because you used the -i option, which edits the files in-place. But to do that, sed needs to access those files directly. When reading from a piped cat, sed can only see the concatenated contents of the files, it cannot know their location, and therefore can't edit them. That's what the error you got meant. But when we pass the list of files directly to sed, it's able to edit them.

答案2

得分: 1

这里捕获哈希值、路径和路径的最后一部分,并将其替换为哈希值(\1)和最后一部分(\3)。

英文:

You can use this:

sed -E 's:([0-9a-f]+)[[:space:]]+/(.+/)*([^/]+)$: :g' file.md5

Here we capturing hash, path and last segment of a path and replacing it with hash (\1) and last segment (\3).

huangapple
  • 本文由 发表于 2023年4月13日 22:23:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定