Shell脚本 - 列出文件,读取文件并将数据写入新文件

时间:2013-04-04 11:09:11

标签: regex parsing shell sed read-write

我对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 

应该是新文件中的一行

我希望我解释好我的“项目”。

现在我的问题是,要了解我如何告诉脚本它必须搜索文件,然后使用这些文件中的每个文件读入并使用其中的一些信息将其写入新文件。

我会更好地解释一下 我正在搜索文件,这让我回来了:

  

file1.nfo
  file2.nfo
  file3.nfo

现在好了,在每个文件中,我需要2行之间的信息。即
file1.nfo:

<user>test1</user>

file2.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>之间的信息

我尝试一步一步地继续阅读,但是看起来很困难。

我做错了什么以及列出所有文件的最佳方法是什么,并将文件和两个字符串之间的内容写入文件?

修改-NEW:

好的,这是更多信息的更新。 我现在学到了很多,并在网上搜索我的问题。我可以找到很多信息,但我不知道如何将它们放在一起以便我可以使用它。

现在使用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

3 个答案:

答案 0 :(得分:2)

sed的pat1,pat2符号是基于行的。可以这样想,pat1为其命令设置启用标志,pat2禁用该标志。如果pat1pat2位于同一行,则会设置标记,因此在您的情况下会打印包含<user>行的所有内容。有关详情,请参阅grymoire's sed howto

在这种情况下,sed的替代方法是使用支持环视断言的grep,例如: GNU grep:

find . -type f -name '*.nfo' | xargs grep -oP '(?<=<user>).*(?=</user>)'

如果grep不支持-P,您可以使用grep和sed的组合:

find . -type f -name '*.nfo' | xargs grep -o '<user>.*</user>' | sed 's:</\?user>::g'

输出:

./file1.nfo:test1
./file2.nfo:test2

注意,您应该了解issues involved with passing files on to xargs,并且可能会使用-exec ...

答案 1 :(得分:2)

恰好grep以您需要的格式输出,并且足以用于单行。

默认情况下,grep '' *.nfo会输出如下内容:

file1.nfo:random data  
file1.nfo:<user>test1</user>  
file1.nfo:some more random data  
file2.nfo:not needed  
file2.nfo:<user>test2</user>  
file2.nfo:etc etc  

通过添加-P选项(Perl RegEx),您可以将输出限制为仅匹配:

grep -P "<user>\w+<\/user>" *.nfo

输出:

file1.nfo:<user>test1</user>  
file2.nfo:<user>test2</user>  

现在-o选项(仅显示匹配的内容)可以保存当天,但由于不需要标记,我们需要更高级的RegEx:

grep -oP "(?<=<user>)\w+(?=<\/user>)" *.nfo > /test/database.txt

cat /test/database.txt的输出:

file1.nfo:test1 
file2.nfo:test2  

在此解释RegEx:http://regex101.com/r/oU2wQ1

你的整个剧本只是一个命令。

<强>更新

如果您没有--perl-regexp选项,请尝试:

grep -oE "<user>\w+<\/user>" *.nfo|sed 's#</?user>##g' > /test/database.txt

答案 2 :(得分:1)

您只需要:

find -name '*.nfo' | xargs awk -F'[><]' '{print FILENAME,$3}'

如果您的文件中包含的内容多于您在示例输入中显示的内容,那么这可能就是您所需要的:

... awk -F'[><]' '/<user>/{print FILENAME,$3}' file

试试这个(未经测试):

> outfile
find -name '*.nfo' -printf "%p %Tc\n" |
while IFS= read -r fname tstamp
do
      awk -v tstamp="$tstamp" -F'[><]' -v OFS=":::" '
          { a[$2] = a[$2] sep[$2] $3; sep[$2] = ", " }
          END {
              print a["string1"], FILENAME, tstamp, a["string4"], a["string3"], a["hobby"], a["string2"]
          }
      ' "$fname" >> outfile
done

只有当您的文件名不包含空格时,上述操作才有效。如果他们可以,我们需要调整循环。

如果你的发现不支持-printf(建议 - 认真考虑获得一个现代的&#34;发现&#34;!),那就另外选择:

> outfile
find -name '*.nfo' -print |
while IFS= read -r fname
do
      tstamp=$(stat -c"%x" "$fname")
      awk -v tstamp="$tstamp" -F'[><]' -v OFS=":::" '
          { a[$2] = a[$2] sep[$2] $3; sep[$2] = ", " }
          END {
              print a["string1"], FILENAME, tstamp, a["string4"], a["string3"], a["hobby"], a["string2"]
          }
      ' "$fname" >> outfile
done

如果你没有&#34; stat&#34;然后谷歌寻找替代方案从文件中获取时间戳或考虑解析ls -l的输出 - 它是不可靠的,但如果它是你所有的......