# Unix Script- Cut the portion of a file



## digitalage (Oct 15, 2010)

Hi 

I am having a file with the following content(its created by ls of a directory): 

-rw-r-----   1 321  321           0 Oct 14 10:41 xcv23456
-rw-r-----   1 321  321           0 Oct 14 10:41 xcv23457
-rw-r-----   1 321  321           0 Oct 14 10:41 xcv23458
-rw-r-----   1 321  321           0 Oct 14 12:19 xcv23459
-rw-r-----   1 321  321           0 Oct 15 11:19 xcv23460



last column ie 9th column contains the file names.

I am searching for a script which can cut the last 5 digits of each row and store it in a seperate variable.

Thanks in advance


----------



## Liverpool_fan (Oct 15, 2010)

```
#!/usr/bin/env python
L = [i.rstrip().split()[8][-5:] for i in open('old.txt')]
print L
```
The data is stored in the list.
Not really an elegant way though.

EDIT: I guess this will do. All those last 5 characters from each row in the 9th column will be stores in the list L


----------



## pulkitpopli2004 (Oct 19, 2010)

the same cud be written  in PERL langauge
considering file name to "datafile"

code:
@result="";
$path="<dir_name>/datafile";
open(FILE, "datafile");
while($line=<FILE>) {
@words=split(/ /,$line);
if($words[8] =~ /([\d]+)/) {
push result,$1;
}
print("@result \n");


----------

