BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

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

SOLVED
Go to Solution
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
Highlighted
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
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?

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

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
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

Honored Contributor

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

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

perfect, thx