Unix & Linux Asked by Darioit on February 16, 2021
I got this record as fileinp
:
1 ABCDEFGHILM 12345678901234567 ABCD X
1 CDEFGHILMNO 34567890123456789 BCDE Y
1 EFGHILMNOPQ 56789012345678901 CDEF Z
1 GHILMNOPQRS 78901234567890123 DEFG W
When last character is Y or Z, I want to print from char 14 to char 47, appended by the last character matched.
Results expected as fileout
:
34567890123456789Y
56789012345678901Z
I tried many codes but all fail,
gawk "{print /Y/ substr($1,14,33, length($1)-0)}" fileinp > fileout
Could you help me please?
In real word this is a full record, that’s why I search for last chars
1 QWERTYUIOPASDFGHJK ZXCVBNMLKJHGFDSAP 1234 12345678 12345ABCDEFGHIL12 202000Y
For the sample input you provided where the last field is 1 chatracter:
$ awk '$NF~/[YZ]/{print substr($0,14,33) $NF}' file
34567890123456789Y
56789012345678901Z
For the real line of data you provided afterwards where the last field is multiple characters and making no assumptions about where the Y or Z might exist in that last field:
$ awk 'match($0,/[YZ][^[:space:]]*$/){print substr($0,14,33) substr($0,RSTART,1)}' file
34567890123456789Y
56789012345678901Z
or if that Y or Z is always the last char in the line:
$ awk '/[YZ]$/{print substr($0,14,33) substr($0,length($0))}' file
34567890123456789Y
56789012345678901Z
Correct answer by Ed Morton on February 16, 2021
i try this:
gawk "$NF~/Y|Z/{print substr($0,14,33) substr($0,194,1)}" fileinp > fileout
but I get this blank chars before last chars
34567890123456789 Y
56789012345678901 Z
I also try this and is works, but it's not really last chars:
gawk "$NF~/Y|Z/{print substr($0,14,33) substr($NF,7,1)}" fileinp > fileout
34567890123456789Y
56789012345678901Z
Answered by Darioit on February 16, 2021
this is almost right, but missing Y or Z after new record
gawk "$NF~/Y|Z/{print substr($0,14,33)}" fileinp > fileout
Results:
34567890123456789
56789012345678901
Results expected:
34567890123456789Y
56789012345678901Z
Answered by Darioit on February 16, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP