我试图从一个字符串中提取所有字符串(按照它们出现的顺序)Taipan 的 BASIC 上市。但是,我还希望定界"
(在字符串的开头和结尾)也出现在输出中,因为某些字符串末尾有空格,我需要能够“看到”它们并对它们进行计数(并且具有最后"
会缓解这一点)。
更复杂的是,我在 macOS 上运行,因此一些 grep 参数将与 GNUgrep 不同。
示例行(包含两个字符串)是
1270 VTAB 19:PRINT " WE'VE CAPTURED A BIGGER SHIP!" : GOSUB 760:VTAB 19:PRINT " WE'RE TRANSFERRING TO IT NOW.": GOSUB 780:G = G + INT ( RND (1) * (G + 1)) + 1:E = SH + INT ( RND (1) * (SH + 150)) + 1: SH = SH + E:MW = MW + E:P=0
我需要的输出是
...
" WE'VE CAPTURED A BIGGER SHIP!"
" WE'RE TRANSFERRING TO IT NOW."
...
我设法只得到包含"
using 1 的行
grep -o '".*"'
但这是一个贪婪的 grep,我在一行中得到两个或多个字符串,并且在字符串之间也捕获非字符串命令,而不是在不同的行上输出多语句行中的各个字符串,如下所示
" WE'VE CAPTURED A BIGGER SHIP!" : GOSUB 760:PRINT TAB(0,18) " WE'RE TRANSFERRING TO IT NOW."
这显然不是我想要的。
以下非贪婪grep
似乎无法正常工作,并在输出中提供了更少的行(出于某种原因)2,可能是因为grep
在 macOS 上没有该-P
选项。
grep -oP '".*?"'
无论如何,继续输出grep -o '".*"'
,如果我将其输入awk
并查找甚至字段3
grep -o '".*"' TAIPAN_BAS.txt | awk -F\" '{print $2}'
grep -o '".*"' TAIPAN_BAS.txt | awk -F\" '{print $4}'
grep -o '".*"' TAIPAN_BAS.txt | awk -F\" '{print $6}'
我会得到更多或更少的结果,除了多语句行上的第二个(或第三个或后续)字符串被输出仅在那之后所有第一次出现的字符串都已列出。
例如,使用它作为输入
1270 VTAB 19:PRINT " WE'VE CAPTURED A BIGGER SHIP!" : GOSUB 760:VTAB 19:PRINT " WE'RE TRANSFERRING TO IT NOW.": GOSUB 780:G = G + INT ( RND (1) * (G + 1)) + 1:E = SH + INT ( RND (1) * (SH + 150)) + 1: SH = SH + E:MW = MW + E:P=0
1280 IF SR < .1 THEN 1300
1281 IF P > 0 THEN 1050
1285 REM ARRIVAL (1290)
1290 CR = 0: SR = 1:L=PO:V(L) = V(L) + 1: B=0:K=0:GOSUB 160:HOME:PRINT:INVERSE:PRINT A$;: NORMAL: PRINT " ARRIVING "; L$(L);" AFTER": PRINT " A VOYAGE OF ";ET;" DAYS.": INVERSE:PRINT A$:NORMAL: GOSUB 780: HOME:GOTO 120
将输出第 n个“批次”中的字符串:
<first command output>
WE'VE CAPTURED A BIGGER SHIP!
ARRIVING
<second command output>
WE'RE TRANSFERRING TO IT NOW.
AFTER
<third command output>
A VOYAGE OF
而如果像这样排序,输出会更好
WE'VE CAPTURED A BIGGER SHIP!
WE'RE TRANSFERRING TO IT NOW.
ARRIVING
AFTER
A VOYAGE OF
然而,即使有了这个“更理想”的输出,仍然存在两个问题:
最后一个字符串 (
" DAYS."
) 丢失 - 我需要一个额外的命令获取第八个字段,就像这样grep -o '".*"' TAIPAN_BAS.txt | awk -F\" '{print $8}'
双引号也丢失了。
这就是我想看到的(带有:双引号;全部琴弦的长度,以及;按出现顺序):
" WE'VE CAPTURED A BIGGER SHIP!"
" WE'RE TRANSFERRING TO IT NOW."
" ARRIVING "
" AFTER"
" A VOYAGE OF "
" DAYS."
笔记:除了撇号之外,所有字符串中都不包含引号(单引号或双引号)。
我的问题是:
- 我怎样才能找到全部多语句行上的偶数字段,而不必发出分离命令为每个字段 - 是否没有一种编程方式来表达
{print $2 $4 $6 ...}
等awk
? - 如何将第二个和第三个字符串拼接到文件中,以便它们按顺序出现,而不是批量出现(所有第一个字符串出现、所有第二个字符串出现、第三个字符串出现等)
我试过这个
grep -o '".*"' TAIPAN_BAS.txt | awk -F\" '{printf $2,"\\n"$4"\\n"$6}'
但所有内容都在一行上输出,仅输出第一个字符串实例,并且换行符 (
\\n
) 似乎被忽略。
- 如何停止
awk
删除字段分隔符(即"
)?
我再次使用 macOS,因此 GNU 解决方案可能没有帮助。另外,我愿意使用sed
或其他东西(我已经尝试过cut
,tr
但他们只是删除了"
并留下了代码负载)。
1来自这个答案到如何从命令的输出中提取带引号的字符串的内容?
2重新检查后,它只给我“使用”消息。
3来自这个答案到提取双引号之间的字符串。
答案1
使用乐(以前称为 Perl_6)
~$ raku -ne '.put for .comb( / \" <-["]>+? \" /);' file
或者:
~$ raku -ne '.put for .comb( / \" ~ \" <-["]>+? /);' file
以下是用 Raku(Perl 编程语言家族的成员)编写的答案。 Raku 具有强大的正则表达式/语法引擎,可用于解析代码(请参阅:“Raku 语法中的简单解析器”)。
您可以通过上面的comb
代码来搜索您想要的模式。要创建自定义字符类,查找<+["]>
用于正选择和<-["]>
负选择的引号。第二个答案使用 Raku 的~
波浪号表示法,这对于提取嵌套文本元素和/或其周围元素很有用。
输入示例:
1270 VTAB 19:PRINT " WE'VE CAPTURED A BIGGER SHIP!" : GOSUB 760:VTAB 19:PRINT " WE'RE TRANSFERRING TO IT NOW.": GOSUB 780:G = G + INT ( RND (1) * (G + 1)) + 1:E = SH + INT ( RND (1) * (SH + 150)) + 1: SH = SH + E:MW = MW + E:P=0
1280 IF SR < .1 THEN 1300
1281 IF P > 0 THEN 1050
1285 REM ARRIVAL (1290)
1290 CR = 0: SR = 1:L=PO:V(L) = V(L) + 1: B=0:K=0:GOSUB 160:HOME:PRINT:INVERSE:PRINT A$;: NORMAL: PRINT " ARRIVING "; L$(L);" AFTER": PRINT " A VOYAGE OF ";ET;" DAYS.": INVERSE:PRINT A$:NORMAL: GOSUB 780: HOME:GOTO 120
1295 REM THAT'S ALL FOLKS (1300-1321)
1300 HOME:NW = C - D:Q = NW / GT: VTAB 4: INVERSE: PRINT A$;: NORMAL: PRINT:PRINT "YOUR SCORE, BASED UPON TIME AND YOUR": PRINT "NET WORTH (EXCLUDING STOCK) IS ";: GOSUB 1330:INVERSE: PRINT A$: NORMAL
1310 IF X$ = "R" THEN PRINT "WOULD YOU LIKE TO PICK UP THIS":PRINT"GAME WHERE YOU LEFT OFF (Y/N)?": GOSUB 60:IF X$ = "Y" THEN HOME:GOTO 120
1320 GOSUB 1340: VTAB 10:PRINT A$;: PRINT "DO YOU WISH TO START OVER (Y/N)?": GOSUB 60:IF X$ = "Y" THEN RUN
1321 END
示例输出:
" WE'VE CAPTURED A BIGGER SHIP!"
" WE'RE TRANSFERRING TO IT NOW."
" ARRIVING "
" AFTER"
" A VOYAGE OF "
" DAYS."
"YOUR SCORE, BASED UPON TIME AND YOUR"
"NET WORTH (EXCLUDING STOCK) IS "
"R"
"WOULD YOU LIKE TO PICK UP THIS"
"GAME WHERE YOU LEFT OFF (Y/N)?"
"Y"
"DO YOU WISH TO START OVER (Y/N)?"
"Y"
上面的示例输出仅供您入门。您还可以记录找到引用字符串的行号:
~$ raku -e ' for lines.kv -> $k,$v { put "$k\t$_" for $v.comb( / \" <-["]>+? \" /)};' file
0 " WE'VE CAPTURED A BIGGER SHIP!"
0 " WE'RE TRANSFERRING TO IT NOW."
4 " ARRIVING "
4 " AFTER"
4 " A VOYAGE OF "
4 " DAYS."
7 "YOUR SCORE, BASED UPON TIME AND YOUR"
7 "NET WORTH (EXCLUDING STOCK) IS "
8 "R"
8 "WOULD YOU LIKE TO PICK UP THIS"
8 "GAME WHERE YOU LEFT OFF (Y/N)?"
8 "Y"
9 "DO YOU WISH TO START OVER (Y/N)?"
9 "Y"
grep
最后,如果您想返回“程序行号”,Raku确实可以帮助您,因为它会过滤掉导致错误的空白行:
~$ raku -e 'for lines.grep(*.chars).map( *.split(" ", 2)) { my $k = .[0]; my $v = .[1]; put "$k\t$_" for $v.comb( / \" <-["]>+? \" /) };' file
1270 " WE'VE CAPTURED A BIGGER SHIP!"
1270 " WE'RE TRANSFERRING TO IT NOW."
1290 " ARRIVING "
1290 " AFTER"
1290 " A VOYAGE OF "
1290 " DAYS."
1300 "YOUR SCORE, BASED UPON TIME AND YOUR"
1300 "NET WORTH (EXCLUDING STOCK) IS "
1310 "R"
1310 "WOULD YOU LIKE TO PICK UP THIS"
1310 "GAME WHERE YOU LEFT OFF (Y/N)?"
1310 "Y"
1320 "DO YOU WISH TO START OVER (Y/N)?"
1320 "Y"
https://docs.raku.org/language/regexes#Tilde_for_nesting_structs
https://docs.raku.org/language/regexes
https://docs.raku.org
https://raku.org
答案2
该-P
选项代表 Perl,并使用 Perl 正则表达式,因此在不可用的情况下,您可以直接使用 Perl...
% perl -lne 'print $& while /".*?"/g' file2.txt
" WE'VE CAPTURED A BIGGER SHIP!"
" WE'RE TRANSFERRING TO IT NOW."
" ARRIVING "
" AFTER"
" A VOYAGE OF "
" DAYS."
($&
是完整的匹配字符串,你已经知道".*?"
是什么了。)
但是,当然,由于结束标记只是一个字符,因此您可以匹配两个所需双引号之间不是双引号的任何内容,即使在常规中grep
:
% grep -oe '"[^"]*"' file2.txt
答案3
在每个 Unix 机器上的任何 shell 中使用任何 awk:
$ awk -F'"' '{for (i=2; i<NF; i+=2) print FS $i FS}' file
" WE'VE CAPTURED A BIGGER SHIP!"
" WE'RE TRANSFERRING TO IT NOW."
" ARRIVING "
" AFTER"
" A VOYAGE OF "
" DAYS."
"
上面假设您引用的字符串中没有s 或换行符。
如果可以的话,正确的解决方案将取决于如何表示"
内部"
分隔字符串(不允许或通过加倍转义""
或通过反斜杠\"
或其他方式转义)以及应如何处理换行符(按原样打印,转换为空白,或其他的东西)。