[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: "cut" command not working as expected

On Tue, Jun 17, 2003 at 08:54:59PM +0100, David selby wrote:
> I need to get the first two file names from a directory ...

If that's the spec, then I'd do

ls | head -2

and that'd give you the first two files, unless you have 'ls' aliased to 
something else (ls -C, perhaps ..?)

> My code
> directory=$(ls -r --format=single-column)
> works perfect and gives me ...
> 20030617Jun17.tar.gz 20030616Jun16.tar.gz 20030615Jun15.tar.gz 
> 20030614Jun14.tar.gz 20030613Jun13.tar.gz 20030612Jun12.tar.gz 
> 20030611Jun11.tar.gz 20030610Jun10.tar.gz 20030609Jun09.tar.gz 
> 20030608Jun08.tar.gz 20030607Jun07.tar.gz 20030606Jun06.tar.gz 
> 20030605Jun05.tar.gz 20030604Jun04.tar.gz 20030603Jun03.tar.gz 
> 20030602Jun02.tar.gz 20030601Jun01.tar.gz 20030531May31.tar.gz 
> 20030530May30.tar.gz 20030529May29.tar.gz 20030528May28.tar.gz 
> 20030527May27.tar.gz 20030526May26.tar.gz 20030525May25.tar.gz 
> 20030524May24.tar.gz 20030523May23.tar.gz 20030522May22.tar.gz 
> 20030521May21.tar.gz 20030520May20.tar.gz 20030519May19.tar.gz 
> 20030518May18.tar.gz 20030517May17.tar.gz 20030516May16.tar.gz 
> 20030515May15.tar.gz 20030514May14.tar.gz 20030513May13.tar.gz 
> 20030512May12.tar.gz 20030511May11.tar.gz 20030510May10.tar.gz 
> 20030509May09.tar.gz 20030508May08.tar.gz 20030507May07.tar.gz 
> 20030506May06.tar.gz 20030505May05.tar.gz 20030504May04.tar.gz 
> 20030503May03.tar.gz 20030426Apr26.tar.gz 20030419Apr19.tar.gz 
> 20030412Apr12.tar.gz 20030405Apr05.tar.gz 20030329Mar29.tar.gz 
> 20030322Mar22.tar.gz 20030315Mar15.tar.gz 20030308Mar08.tar.gz 
> 20030301Mar01.tar.gz 20030222Feb22.tar.gz 20030215Feb15.tar.gz 
> 20030208Feb08.tar.gz 20030205Feb05.tar.gz
> I want to cut the first two file names from the list ... To my way of 
> thinking this should be easy ...

So you mean the first two files from each triple?
That's not what your "ls -r --format=single-column" is going to give 
you, I'm afraid.

> cut -d' ' -f2 $directory

Notwithstanding what I've just said, I'd do the following instead of 
that line:

echo $directory | cut -d' ' -f2

> The xterm goes nuts and ends up in hyroglyphics ! At a guess I would say 
> that the white space between .gz & 200... may not be space but may have 
> a different ascii value. I have not found a utility to display raw ASCII 
> for a file yet to check this out.

No idea, sorry.  Have you any reason to believe that the filenames have 
wierd characters in them?

> I am a relative begginer at learning bash ...

Don't worry - there's a definate "ah HA" point when it clicks :-)


Reply to: