Re: get lines with digit(s) at the end from a file ( delimited by field separator ) (280 Views)
Reply
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 1 of 8 (319 Views)
Accepted Solution

get lines with digit(s) at the end from a file ( delimited by field separator )

hello,

 

- field separator can be variabel  like "|" or ";"

 

i want to get lines , which have digits (1-N) at the end , delimited by field separator .

like :

.............;01234

 

file:

|||||||||||||||||||||||
host1|192.31.41.18|192.30.41.18|20111231|20051001|20141231|||131
host2|192.31.41.17|192.30.41.17|20111231|20051001|20141231|||132
|||||||||||||||||||||||
host3|192.31.41.19|192.30.41.19|20111231|20051001|20141231||||

 

test:

SFS="|"   # <- field separator

grep "${SFS}[0-9][0-9]*$"  file

 

i use infos from this thread:

 

get the last digit(s) from a line or file

Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 2 of 8 (313 Views)

Re: get lines with digit(s) at the end from a file ( delimited by field separator )

$ perl -ne'm/[;|][0-9]+$/ and print' file
Enjoy, Have FUN! H.Merijn
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,971
Registered: ‎03-06-2006
Message 3 of 8 (308 Views)

Re: get lines with digit(s) at the end from a file (delimited by field separator)

>I want to get lines, which have digits (1-N) at the end, delimited by field separator.

grep "${SFS}[0-9][0-9]*$"  file

 

If the separator is only ";" and "|", change to:

grep "[;|][0-9][0-9]*$"  file

 

If it is in a variable, what's wrong with what you had?

Please use plain text.
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 4 of 8 (295 Views)

Re: get lines with digit(s) at the end from a file ( delimited by field separator )

perl -ne'm/[;|][0-9]+$/ and print' file

 i change it to

 

SFS="|" perl -ne'm/[;$ENV{SFS}][0-9]+$/ and print'  file

 

and it works !

 

last question :

 

how can i check , if in field 4  are only digits ( it is a date format with YYYYMMDD)

 

example:

host1|192.31.41.18|192.30.41.18|20111231|20051001|20141231|||131

 

it should report : in line N field 4  are characters <> digits

 

regards

Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 5 of 8 (291 Views)

Re: get lines with digit(s) at the end from a file ( delimited by field separator )

$ perl -aF"\Q$SFS" -nle'$F[3]=~m/^[0-9]{4}/ or print"Field 4 is wrong in line $."' file

 

Enjoy, Have FUN! H.Merijn
Please use plain text.
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 6 of 8 (283 Views)

Re: get lines with digit(s) at the end from a file ( delimited by field separator )

hello,

 

i tested with

 

SFS="|"
perl -aF"\Q$SFS" -nle'$F[3]=~m/^[0-9]{4}/ or print"Field 4 is wrong in line $."' file

 and for every line i get "Field 4 is wrong"

i add testfile

 

regards

Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 7 of 8 (280 Views)

Re: get lines with digit(s) at the end from a file ( delimited by field separator )

[ Edited ]

Sorry. \Q doesn't seem to work in command line option :(

 

I don't know if that is a bug.

 

A workaround could be

 

perl -aF"[$SFS]" -nle'$F[3]=~m/^[0-9]{4}/ or print"Field 4 is wrong in line $."' test.txt

note the brackets around the $SFS. Now every character in $SFS will be a valid field separator

Enjoy, Have FUN! H.Merijn
Please use plain text.
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 8 of 8 (256 Views)

Re: get lines with digit(s) at the end from a file ( delimited by field separator )

perfect, thx

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation