简体   繁体   English

使用find和sed将文件名插入文件

[英]Insert file names into files using find and sed

I have hundreds .htm files and i need to replace <title>sometext</title> with the actual file name. 我有数百个.htm文件,我需要用实际的文件名替换<title>sometext</title> For example: i have file called records-england.htm and i need to replace <title>sometext</title> in this file with file name like so <title>records-england.htm</title> 例如:我有一个名为records-england.htm文件,我需要将此文件中的<title>sometext</title>替换为<title>records-england.htm</title>这样的文件名。

Even better if without extension - but that does not matter. 如果没有扩展甚至更好-但这没关系。 I'v tried like this but it's not working: 我已经尝试过了,但是没有用:

find . -name *.htm | while read file; do sed "s|<title>sometext</title>|<title>$file</title>|g" {} \; done

Any advice? 有什么建议吗?

You can use awk s FILENAME. 您可以使用awk的FILENAME。 Needs to be gnu awk >= 4.1 due to -i inplace 由于-i inplace ,需要gnu awk > = 4.1

awk -i inplace '{a=FILENAME;sub(/\.htm$/,"",a);gsub(/<title>[^<]*<\/title>/,"<title>"a"</title>")}1' *.htm

This will change <title>sometext</title> (where sometext can be anything) to <title>filename(without extension)</title> 这会将<title>sometext</title> (其中sometext可以是任何东西)更改为<title>filename(without extension)</title>

To fix the problem in your code, you can try this : 要解决代码中的问题,您可以尝试以下操作:

find -name "*.htm" | while read file; do sed "s|<title>sometext</title>|<title>${file##*/}</title>|g" -i $file; done

Sed need the -i option to specify which files you want to edit and I use a parameter expansion to only display the basename of the file. sed需要使用-i选项来指定要编辑的文件,并且我使用参数扩展来仅显示文件的基本名称。

这样尝试

's/\<title\>.*\<title\>/title>$file<title/g'

This would work for any arbitrary title text. 这将适用于任意标题文本。 If you don't want it to be any text but some fixed text then change <title>.*</title> to <title>sometext</title> . 如果您不希望它是任何文本,而是一些固定文本,则将<title>.*</title>更改为<title>sometext</title>

 find -type f -name '*.htm' -printf '%P\0%f\0' | xargs -0 -n2 sh -c 'fil="${2/\.htm/}"; sed -i -e "s;<title>.*</title>;<title>$fil</title>;" $1' replace

It transfers the result of find to sh using xargs ; 它使用xargsfind的结果传输到sh bash strips the extension off and give the arguments to sed which does the replacement. bash删除扩展名,并将参数提供给sed进行替换。

Arguments: 参数:

To find find

  • -type f find files -type f查找文件
  • -name '*.htm' which has extension htm -name '*.htm' ,扩展名为htm
  • printf '%P\\0%f\\0' output full file path then a ASCII null, followed by just the filename (stripping the path) and again an ASCII nul printf '%P\\0%f\\0'输出完整的文件路径,然后输出ASCII空值,然后仅输出文件名(剥离路径),再输出ASCII码空值

To xargs xargs

  • -0 use ASCII nul as the delimiter -0使用ASCII nul作为分隔符
  • -n2 pick up at most 2 arguments from the list for each execution of the command -n2每次执行命令时从列表中最多选择2个参数

To sh sh

  • -c reads commands from the first non-option argument -c从第一个非选项参数读取命令
  • a bash script which takes $1 (file path) and $2 (file name) as arguments; 一个bash脚本,它以$1 (文件路径)和$2 (文件名)为参数; does regex replacement to snip the extension. 进行正则表达式替换以剪切扩展名。 Passing a dummy string replace for it to take $0 传递一个虚拟字符串replace$0

To sed sed

  • -i in-place replace in file -i在文件中就地替换
  • -e the script to execute -e执行脚本

You could also try this: 您也可以尝试以下方法:

#!/bin/bash
outFile=/tmp/myOutFile.out
touch $outFile
while read line
do
    name=$line
    echo "<title>$name</title>" >> $outFile
done < $1
  1. Create a new .sh (eg modifyFile.sh) file with the code above. 使用上面的代码创建一个新的.sh(例如ModifyFile.sh)文件。
  2. Grant it execute rights - chmod +x modifyFile.sh 授予它执行权限-chmod + x ModifyFile.sh
  3. Run it with the file which contains your file names or w/e - ./modifyFile.sh myInputFile.txt 使用包含您的文件名或w / e-./modifyFile.sh myInputFile.txt的文件运行它
  4. You will get results in /tmp/myOutFile.out 您将在/tmp/myOutFile.out中获得结果

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

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