search for a string -perl

Hi,

I have a line where i need to get certain part of it.. example..

text txt tt:    1909

thats how exactly it looks and all spaces are to be counted.. i need to retrieve 1909..

Thanks

s/^.* //;

[ Message too short ]

What do you mean spaces are to be counted? Do you need to know how many spaces are there between the "tt:" and "1909"? Are there always three words here? Is it always the last word in the line?

Possible solutions, each all does roughly the same thing:

  m/:\s*/ && print $';
  m/:\s*(\d+)/ && print $1;
  m/^\S+\s+\S+\s+\S+\s+(\S+)$/ && print $1;
  chop; ($junk,$year)=split(':\s+',$_,2);

Of course, you won't need to fire up perl just to do this.

  cut -d ":" -f 4

would generally do the trick. So would

  awk -F "[: ]*" '{ print $4 }'

OP is interested to extract the digits after the last space from the literal
which turns out to be the digits after the space.

yes matrixmadhan is rite.. i just need to retrieve the number '1909'....

thanks

Hey can you explain me what does this do .. thanks again!:slight_smile:

So why not just hard-code "1909". Anyway, his perl solution will fail if there are any spaces after the year. Better to use:

 s/^.*(?:\S)//;

strip off all the characters right from the start of the literal till a space " " is encountered including the space

so what is left out is only the numeric literal

actually thats not a year tats a count.. and it may vary.. from 1 - any number...

thanks for the explanation

unless every line in the file is the same as:

text txt tt: 1909

the suggested solutions will process each line of the file almost regardless of whats in them. Can you explain better what you are trying to do? Are there other lines in the file that you want to skip?

Is the above line always in this format:

aaaa aaa aa: n

where 'a' is alpha characters (4 3 2) followed by a colon and a space and n is any number of digits

don't have to use regular expression if you are still not familiar with it. Until you are,

$s="text txt tt:    1909";
@a=split(/:/,$s);
print $a[1];