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 quotes, and then to the last two characters of the line and put them in a new file. I can do two parts separately, but I cannot combine them and make them 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:

Text 1 d2
Text 2 30
Text 3 d2
TextA 00
TextB d2
TextC 00
+4
source share
4 answers

You can link the result using $ 2 for the text between quotation marks along with the result of the last two characters, as shown below:

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

. ($ NF), , , 2 :

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

2<blank>, . , : , , , , , 2 ?

+3
$ 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
0

sed (BRE):

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

sed (ERE):

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

awk:

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

- . gensub , ( ).

-1

Source: https://habr.com/ru/post/1609478/


All Articles