extract string from filename

Larry Cook lcook at sybase.com
Fri Jan 13 11:10:01 EST 2006


Zhao Peng wrote:
> My goal is to :
> 1, extract string2 from each file name
> 2, then sort them and keep only unique ones
> 3, then output them to a .txt file. (one unique string2 per line)

It is really interesting how many ways there are to do things in *nix.  My 
first reaction, if this is a one time event, is to just use vi:

% ls *.sas7bdat > string2.txt
% vi string2.txt
:%s/^[^_]*_//
:%s/_.*$//
:%!sort -u
:wq

The first regex removes the first underscore and everything in front of it, 
while the second regex removes what is now the first underscore (was the 
second originally) and everything after it.  And then I do the unique sort 
right in vi.

Larry



More information about the gnhlug-discuss mailing list