简体   繁体   English

使用awk或sed将文件的特定区域输出到另一个文件?

[英]Output to specific areas of a file to another file using awk or sed?

I have a file that looks like this: 我有一个看起来像这样的文件:

d "Text 1":6,64;1 /filesys1/db1.d2
d "Text 2":6,64;1 /filesys1/db1.d2 f 730
d "Text 3":6,64;1 /filesys1/db1.d2 
d "TextA":6,64;1 /filesys1/db1.d2 f 46000
d "TextB":6,64;1 /filesys1/db1.d2
d "TextC":6,64;1 /filesys1/db1.d2 f 120000
...

I need to get everything from between the quotes and then the last 2 characters of the line and put it in a new file. 我需要从引号之间获取所有内容,然后获取该行的最后2个字符并将其放入新文件中。 I can do the two pieces separately but I can't combine them and get it to work. 我可以单独做两件,但我不能将它们组合起来并让它起作用。

awk -F'"' '$0=$2' datatmp4 > dataout2

will get me: 会得到我:

Text 1
Text 2
Text 3
TextA
TextB
TextC

and

awk '{ print substr( $NF, length($NF) -1, length($NF) ) }' datatmp4 > dataout

will get me: 会得到我:

d2
30
d2
00
d2
00

what I need is: 我需要的是:

Text 1 d2
Text 2 30
Text 3 d2
TextA 00
TextB d2
TextC 00

您可以使用$ 2连接引号之间的文本以及最后2个字符的结果,如下所示:

awk -F '"' '{print $2, substr($NF, length($NF)-1, length($NF))}' datatmp4 > dataout

You're making things too hard on yourself. 你在自己身上做得太难了。 There's no reason to care about or try to operate on the last field on the line ($NF) when all you want is the last 2 characters of the whole line: 当你想要的只是整行的最后2个字符时,没有理由关心或尝试操作线上的最后一个字段($ NF):

$ awk -F'"' '{print $2, substr($0,length()-1)}' file
Text 1 d2
Text 2 30
Text 3 2
TextA 00
TextB d2
TextC 00

The third line of output ends in 2<blank> because that's what was in your input file. 输出的第三行以2<blank>结尾,因为这是输入文件中的内容。 That doesn't match your posted desired output though so be clear - do you want the last chars of each line as I've shown and you said you wanted, or do you want the last 2 non-blank chars as implied by your posted desired output? 这与你发布的所需输出不符,但是要清楚 - 你是否想要我显示的每行的最后一个字符,你说你想要的,或者你想要你发布的暗示的最后2个非空白字符吗?期望的输出?

$ awk -F"\"" '{match($NF,/..$/,a); print $2,a[0]}' last2
Text 1 d2
Text 2 30
Text 3 2
TextA 00
TextB d2
TextC 00

With sed (BRE): 使用sed(BRE):

sed 's/^[^"]*"\([^"]*\).*\(.[^ ]\)/\1 \2/;' file

Another way with sed (ERE): sed(ERE)的另一种方式:

sed -E 's/^[^"]*"|"[^ ]*( ).*(.[^ ])/\1\2/g' file

With awk: 用awk:

awk -F'"' '{ print $2 " " gensub(/.*(.[^ ])/, "\\1", 1)}' file

The field separator is a quote. 字段分隔符是引用。 gensub replaces all characters from line except the 2 last characters (the second must not be a space). gensub替换除了最后2个字符之外的所有字符(第二个字符不能是空格)。

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

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