[英]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
; 它使用
xargs
将find
的结果传输到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
从第一个非选项参数读取命令 $1
(file path) and $2
(file name) as arguments; $1
(文件路径)和$2
(文件名)为参数; does regex replacement to snip the extension. 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.