Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 从文本文件中提取带引号的字符串,即使行换行_Shell_Unix_Sed - Fatal编程技术网

Shell 从文本文件中提取带引号的字符串,即使行换行

Shell 从文本文件中提取带引号的字符串,即使行换行,shell,unix,sed,Shell,Unix,Sed,嗨,我有一个巨大的文件,比如: Hi这是来自的文件,文件名为=“file1.txt” 你好,找到文件名。。。filename=“此文件的名称太大,并且 转到下一行,但用双引号括起来。txt“ 还有一个文件的大小为filename=“file2.txt” 是“333kb”; 我的预期输出仅为文件名字符串,在管道分隔字符串中不存在换行符,如下所示: file1.txt | the name of this file is too huge and goes to the next line bu

嗨,我有一个巨大的文件,比如:

Hi这是来自的文件,文件名为=“file1.txt”
你好,找到文件名。。。filename=“此文件的名称太大,并且
转到下一行,但用双引号括起来。txt“
还有一个文件的大小为filename=“file2.txt”
是“333kb”;

我的预期输出仅为文件名字符串,在管道分隔字符串中不存在换行符,如下所示:

file1.txt | the name of this file is too huge and goes to the next line but enclosed with double quotes.txt | file2.txt
我使用了下面的sed命令,但结果不是预期的。它只输出同一行中的文件名

sed -n 's/^.*filename="\(.*\)".*/\1/p

请帮助我完成这项工作,提前谢谢。

您可以从以下管道开始:

tr '\n' ' ' < input | grep -o 'filename *= *"[^"]*"'
清理:


对于多字符RS和gensub(),使用GNU awk:

$awk-vrs='\\1?OFS:”),$2}
结束{打印“”}
"档案"
file1.txt |此文件的名称太大,会转到下一行,但用双引号括起来。txt | file2.txt

为什么“使用
sed
”作为先决条件?为什么不让回答问题的人为这项工作推荐最好的工具,即使他们碰巧认为这是
awk
,或者bash中的原生逻辑,或者其他什么呢?另外,现在还不清楚文本中哪些换行符是或不是文字,因为“文件内容”被呈现为包装文本。
s是否应该表示换行符?它还可以用一些实际的数字来量化“巨大的”,以便进行实际的基准测试。确切地了解换行规则对于
filename
是否可以与下面的
=
单独在一行也很有帮助。(您的测试数据在
=
周围的空格完全不一致方面是否真正具有代表性?)?能把它分成三个吗?
filename= "file1.txt"
filename = "the name of this file is too huge and goes to the next line but enclosed with double quotes.txt"
filename="file2.txt"
tr '\n' ' ' < input | grep -o 'filename *= *"[^"]*"' | sed 's/.*"\([^"]*\)"/\1/'
file1.txt
the name of this file is too huge and goes to the next line but enclosed with double quotes.txt
file2.txt
$ awk -v RS='\\<filename\\s*=\\s*"[^"]+"' -F'"' -v OFS=' | ' '
    RT {$0=gensub(/\s+/," ","g",RT); printf "%s%s", (NR>1?OFS:""), $2}
    END {print ""}
' file
file1.txt | the name of this file is too huge and goes to the next line but enclosed with double quotes.txt | file2.txt