-3

So I want to grab only the rows that has 2 or more than 2 leading zeroes in the ID column ($2) between the 5th and 10th character. For example column 2 has ID 156700923134, so from 5th to 10th character 1567-009231-34 i.e. 009231. In this case we do see the leading zeroes. However in the second row we have 777754635373, so grab 546353, which does not have leading zeroes. I am working on a pipe delimited file.

Ex: 1 | 156700923134 | hkohin | 23


4 | 777754635373 | hhkdys | 45


3 | 678387700263 | ieysff | 09

Expected output: 1 | 156700923134 | hkohin | 23 
                        --OR-- 
                     156700923134 

So far I have the substring 009231, 546353, 877002 as output but I don't know how to check for leading zeroes.

This is what I used to get to the above result:
awk -F'|' '{print $2, substr($2, 5, 6) }' file.dat | head -5
HKohin
  • 27
  • 4
  • 2
    Ummm ... you need to work on your language. A leading zero is one at the beginning of a string. Even if you go by your substrings none of the numbers in your sample data have leading zeros. What exactly are you looking for? – tink Nov 21 '22 at 18:33
  • @tink - "more than 2 leading zeroes between 5th to 10th character (in the column ID)". I did mention it in the description. Sorry if it wasn't very clear. 156700923134 --> 1567-009231-34 --> in this case 009231 does have the leading zeroes and thats what I was interested in finding. – HKohin Nov 21 '22 at 21:50

1 Answers1

1

() for test condition allows any valid expression

awk -F'|' '( match($2,"^....00") ) { print print $2, substr($2, 5, 6) }' file.dat

Answer #2:

Takes more lines to be generic:

zstart=5
zcnt=3
zeros=$(eval printf '0%.0s' {1..$zcnt})

echo 'xxx|1234000890|end' |
  awk -F'|' -vzstart=$zstart -vzcnt=$zcnt -vzeros="$zeros" '
     ### debug { print substr($2, zstart, zcnt); }
     (zeros == substr($2, zstart, zcnt)) { print }'
Gilbert
  • 3,740
  • 17
  • 19
  • Thanks so much @Gilbert this worked!! But this is only showing the IDs with 2 leading zeroes. Is there a way to customize this to two or more leading zeroes? – HKohin Nov 21 '22 at 22:01
  • And, as usual, if you pick something as an answer, flag it that way. – Gilbert Nov 21 '22 at 23:37
  • Thanks so much the first solution worked pretty well for the other scenario too. I am new to using stackOF, flagged it now!!! – HKohin Nov 21 '22 at 23:51