awk + how to get latest numbers in file but exclude number until 4 digit

Viewed 72

we have file like the following

more file

-rwxrwxrwt   3 hdfs hdfs         30 2021-06-09 07:10 /noij/gtdf/license_alerts/alert_1_1623222654527
-rwxrwxrwt   3 hdfs hdfs         30 2021-06-09 07:10 /noij/gtdf/license_alerts/alert_1_1623222654679
-rwxrwxrwt   3 hdfs hdfs         30 2021-06-09 07:10 /noij/gtdf/license_alerts/alert_1_1623222654744
-rwxrwxrwt   3 hdfs hdfs         30 2021-06-09 08:02 /noij/gtdf/license_alerts/alert_1_1623225746040_69
-rwxrwxrwt   3 hdfs hdfs         30 2021-06-09 08:02 /noij/gtdf/license_alerts/alert_1_1623225746059_66
-rwxrwxrwt   3 hdfs hdfs         30 2021-06-09 08:02 /noij/gtdf/license_alerts/alert_1_1623225746061_65
-rwxrwxrwt   3 hdfs hdfs         30 2021-06-09 08:02 /noij/gtdf/license_alerts/alert_1_1623225746162_65

while I need to take the last numbers but exclude if the number contain 1-3 digit

expected results should be

1623222654527
1623222654679
1623222654744

so my approach until now is

sed s'/_/ /g' file | awk '{print $NF}'  

but its print

1623222654527
1623222654679
1623222654744
69
66
65
65

how to improve my syntax i n order to exclude the latest number that have 1 or 2 or 3 digit?

so we get the output like this

1623222654527
1623222654679
1623222654744
3 Answers

Like this?:

$ awk -F_ 'length($NF)>3{print $NF}' file

Output:

1623222654527
1623222654679
1623222654744

With _ as field separator, if the length of the last field $NF is greater than 3, output the last field.

With grep:

$ grep -oE '[0-9]{4,}$' ip.txt
1623222654527
1623222654679
1623222654744

If there should be an underscore present, using gnu grep with -P for Perl-compatible regular expressions:

grep -oP ".*_\K\d{4,}$" file

The pattern matches:

  • .*_ Match until the last underscore
  • \K Clear the match buffer
  • \d{4,}$ Match 4 or more digits until the end of the string

Output

1623222654527
1623222654679
1623222654744

Or using sed

sed -nE 's/.*_([0-9]{4,})$/\1/p' file
  • -E extended regex
  • -n suppress automatic printing
  • /p print the line

Output

1623222654527
1623222654679
1623222654744
Related