Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write to a file and read the file again

Status
Not open for further replies.

alhassani54

Programmer
Aug 1, 2002
27
GB
Hi

I want to write the list of files from a directory in order to a file (file name = sds) and read the file (sds) line by line to print the list.
i.e.
Directory contains the following files
A10
A100
A150
A20
A200
A250
A30
A300
A350
I want to write them to a file (sds) in order
A10
A20
A30
A100
A150
A200
A250
A300
A350
Then read them from the file (sds) as the same way as the order on the file (sds).

Thank you

 
Code:
cd /to/your/dir
ls | grep -v sds | sort -k 1.2n >sds
while read file
do
 echo $file
done <sds

Also check out your man page for sort (not sure about sort key)


HTH,

p5wizard
 
I have tried the script but it did not do the sort.
Anyone can help with the sort
Thank you

ls | grep sds | sort -k 1.2n >sds
while read file
do
echo $file
done <sds

cat sds
sds10
sds100
sds150
sds20
sds200
sds250
sds30
sds300
sds350
 
ls | grep sds | sort -k 1.[!]4[/!]n >sds

Tip: man sort

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Well, that's different data than in your OP...

try this:

sort -k 1.4n

Or read the man page for sort.

-k 1.2n works fine for "Ann", "Annn" lines as in your OP.

Code:
meaning: -k X.Yn
            X=field number X
              Y=Yth starting position of field X
               n=modifier for numeric sort


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top