Operating System - HP-UX
1748099 Members
5495 Online
108758 Solutions
New Discussion юеВ

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

 
SOLVED
Go to solution
support_billa
Valued Contributor

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

7 REPLIES 7
H.Merijn Brand (procura
Honored Contributor

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
Dennis Handly
Acclaimed Contributor

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?

support_billa
Valued Contributor

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

H.Merijn Brand (procura
Honored Contributor

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
support_billa
Valued Contributor

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

H.Merijn Brand (procura
Honored Contributor
Solution

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

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
support_billa
Valued Contributor

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

perfect, thx