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

Re: "cut" command not working as expected



David selby wrote:

> I need to get the first two file names from a directory ...
> 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 ...
> 
> cut -d' ' -f2 $directory
> 
> 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.

Read the man page for cut(1) again very carefully. The text to be cut is
_not_ taken from the command line. The command line specifies a
_filename_ to read from. So cut is reading from 20030617Jun17.tar.gz,
which isn't what you want.

This will work:

echo $directory | cut -d ' ' -f 1-2

Craig

Attachment: pgpt3mIDPqSq9.pgp
Description: PGP signature


Reply to: