我对 shell 脚本有一个特殊的问题。
简单的脚本编写对我来说没问题,但我对此很陌生,想让我成为一个简单的数据库文件。
所以,我想做的是:
- Search for filetypes (i.e. .nfo) <-- should be no problem :)
- read inside of each found file and use some strings inside
- these string of each file should be written in a new file. Each found file informations
应该是新文件中的一行
我希望我能很好地解释我的“项目”。
我现在的问题是,要了解如何告诉脚本它必须搜索文件,然后使用每个文件读取它并使用其中的一些信息将其写入新文件。
我会解释得更好一点。
我正在搜索文件,这让我回来了:
文件 1.nfo文件
2.nfo 文件
3.nfo
好的,现在在每个文件中,我需要两行之间的信息。即
file1.nfo:
<user>test1</user>
文件2.nfo:
<user>test2</user>
所以在新文件中现在应该有:
file1.nfo:user1
file2.nfo:user2
好的,所以:
find -name *.nfo > /test/database.txt
正在打印文件列表。和
sed -n '/<user*/,/<\/user>/p' file1.nfo
给我完整的文件,而不仅仅是和之间的<user>
信息</user>
我试着一步一步往前走,我读了很多书,但似乎很难。
我做错了什么,列出所有文件并将文件和两个字符串之间的内容写入文件的最佳方法应该是什么?
编辑-新:
好的,这是有关更多信息的更新。我现在学到了很多东西,并在网上搜索了我的问题。我可以找到很多信息,但我不知道如何将它们放在一起以便我可以使用它。
现在使用 awk 是我得到文件名和字符串。
现在这里是完整的信息(我想我可以在一些帮助下自己继续,但我不能:()
这是一个示例:/test/file1.nfo
<string1>STRING 1</string1>
<string2>STRING 2</string2>
<string3>STRING 3</string3>
<string4>STRING 4</string4>
<personal informations>
<hobby>Baseball</hobby>
<hobby>Baskeball</hobby>
</personal informations>
这里是 /test/file2.nof 的一个例子
<string1>STRING 1</string1>
<string2>STRING 2</string2>
<string3>STRING 3</string3>
<string4>STRING 4</string4>
<personal informations>
<hobby>Soccer</hobby>
<hobby>Traveling</hobby>
</personal informations>
我要创建的文件必须看起来像这样。
STRING 1:::/test/file1.nfo:::Date of file:::STRING 4:::STRING 3:::Baseball, Basketball:::STRING 2
STRING 1:::/test/file2.nfo:::Date of file:::STRING 4:::STRING 3:::Baseball, Basketball:::STRING 2
“文件日期”应该是文件的创建日期。这样我就可以看到文件的年龄。
所以,这就是我所需要的,而且似乎并不容易。
非常感谢。
更新错误 -printf
find: unrecognized: -printf
Usage: find [PATH]... [OPTIONS] [ACTIONS]
Search for files and perform actions on them.
First failed action stops processing of current file.
Defaults: PATH is current directory, action is '-print'
-follow Follow symlinks
-xdev Don't descend directories on other filesystems
-maxdepth N Descend at most N levels. -maxdepth 0 applies
actions to command line arguments only
-mindepth N Don't act on first N levels
-depth Act on directory *after* traversing it
Actions:
( ACTIONS ) Group actions for -o / -a
! ACT Invert ACT's success/failure
ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2
ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2
Note: -a has higher priority than -o
-name PATTERN Match file name (w/o directory name) to PATTERN
-iname PATTERN Case insensitive -name
-path PATTERN Match path to PATTERN
-ipath PATTERN Case insensitive -path
-regex PATTERN Match path to regex PATTERN
-type X File type is X (one of: f,d,l,b,c,...)
-perm MASK At least one mask bit (+MASK), all bits (-MASK),
or exactly MASK bits are set in file's mode
-mtime DAYS mtime is greater than (+N), less than (-N),
or exactly N days in the past
-mmin MINS mtime is greater than (+N), less than (-N),
or exactly N minutes in the past
-newer FILE mtime is more recent than FILE's
-inum N File has inode number N
-user NAME/ID File is owned by given user
-group NAME/ID File is owned by given group
-size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.))
+/-N: file size is bigger/smaller than N
-links N Number of links is greater than (+N), less than (-N),
or exactly N
-prune If current file is directory, don't descend into it
If none of the following actions is specified, -print is assumed
-print Print file name
-print0 Print file name, NUL terminated
-exec CMD ARG ; Run CMD with all instances of {} replaced by
file name. Fails if CMD exits with nonzero
-delete Delete current file/directory. Turns on -depth option