Re: get lines with digit(s) at the end from a file ( delimited by field separator ) (340 Views)
Reply
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 1 of 8 (403 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

Honored Contributor
H.Merijn Brand (procura
Posts: 6,189
Registered: ‎10-13-1997
Message 2 of 8 (397 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
Acclaimed Contributor
Dennis Handly
Posts: 25,296
Registered: ‎03-06-2006
Message 3 of 8 (392 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?

Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 4 of 8 (379 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

Honored Contributor
H.Merijn Brand (procura
Posts: 6,189
Registered: ‎10-13-1997
Message 5 of 8 (375 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
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 6 of 8 (367 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

Honored Contributor
H.Merijn Brand (procura
Posts: 6,189
Registered: ‎10-13-1997
Message 7 of 8 (364 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
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 8 of 8 (340 Views)

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

perfect, thx

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.