[英]Shell script to extract a filename from log message lines
I have txt file which listed with some line. 我有列出一些行的txt文件。 Each line has image names included. 每行都包含图像名称。 What I want is, The shell script that edit same txt file OR copy particular image name to new file. 我想要的是,编辑相同txt文件或将特定图像名称复制到新文件的shell脚本。
This is my txt file having list of images with their paths. 这是我的txt文件,其中包含图像列表及其路径。
And I want output like this: 我想要这样的输出:
I want only image names should be extract from those lines. 我只希望从这些行中提取图像名称。
Using gnu sed
you can do: 使用gnu sed
您可以执行以下操作:
sed -r 's~^[^[:blank:]]*/([^/ ]+) .*$~\1~' file
1.png
1@2x.png
2.png
2@2x.png
3.png
您可以使用此awk
,
awk '{ split($1,arr,"/"); print arr[length(arr)] }' yourfile > output.txt
You can do something like: 您可以执行以下操作:
cat the_file.txt|while read file; do
echo $(basename $file)
done
And (if needed) redirect the output on another file. 然后(如果需要)将输出重定向到另一个文件。
while read fn rest
do
basename "$fn"
done < file.txt
This will read your input line by line. 这将逐行读取您的输入。 It will put the filename (including path) into the fn
variable, and whatever is on the rest of the line into rest
. 它将把文件名(包括路径)放入fn
变量中,并将该行rest
部分中的所有内容放入rest
。 Then it uses the basename
command to strip off the path and just print out the filename itself. 然后,它使用basename
命令删除路径,仅打印出文件名本身。
This is another sed
solution that doesn't use extended regular expression (more portable solution): 这是另一个不使用扩展正则表达式的sed
解决方案(更具可移植性的解决方案):
sed 's/^.*\/\([^[:blank:]\/]*\)[[:blank:]].*$/\1/' sourceFile > destFile
You have to replace sourceFile
and destFile
white the path to oridinal and destination file respectively. 您必须将原始文件和目标文件的路径分别替换为sourceFile
和destFile
。
The command look for any string without blank char or slashes \\([^[:blank:]\\/]*\\)
preceeded by a slash ^.*\\/
and followed by a blank char [[:blank:]].*$
than the patter is substitituded with the first matching string /\\1/
. 该命令查找没有空格或斜杠\\([^[:blank:]\\/]*\\)
任何字符串,其前加斜杠^.*\\/
,后跟空白char [[:blank:]].*$
patter被第一个匹配的字符串/\\1/
取代。
You could read a quick sed
reference here . 您可以在此处阅读快速的sed
参考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.