List files with number to select based on number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List files with number to select based on number
# 1  
Old 08-10-2016
List files with number to select based on number

Hi experts,

I am using KSH and I am need to display file with number in front of file names and user can select it by entering the number.
I am trying to use following command to display list with numbers. but I do not know how to capture number and identify what file it is to be used for further scripting.
Code:
ls -1 | cat -n

# 2  
Old 08-10-2016
Did you consider ksh's select command:
Quote:
select vname [ in word ... ] ;do list ;done

A select command prints on standard error (file descriptor 2) the set of words, each preceded by a number.
?
# 3  
Old 08-10-2016
Code:
PS3="Enter file number: "
echo "File list:"
select file in *
do
  [[ -a "$file" ]] && break
  REPLY=""
  echo "Invalid number."
  echo ""
  echo "File list:"
done
echo "file selected: $file"


Last edited by rdrtx1; 08-10-2016 at 07:18 PM..
# 4  
Old 08-10-2016
Quote:
Originally Posted by rdrtx1
Code:
PS3="Enter file number: "
echo "File list:"
select file in *
do
  [[ -a "$file" ]] && break
  REPLY=""
  echo "Invalid number."
  echo ""
  echo "File list:"
done
echo "file selected: $file"

Thanks for the input. Somehow it shows me error "Select: not found".
# 5  
Old 08-10-2016
add to top of script:

Code:
#!/bin/ksh

This User Gave Thanks to rdrtx1 For This Post:
# 6  
Old 08-10-2016
Quote:
Originally Posted by rdrtx1
add to top of script:

Code:
#!/bin/ksh

This worked for me. Thanks! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Select and copy .csv files based on row and column number

Dear UNIX experts, I'm a command line novice working on a Macintosh computer (Bash shell) and have neither found advice that is pertinent to my problem on the internet nor in this forum. I have hundreds of .csv files in a directory. Now I would like to copy the subset of files that contains... (8 Replies)
Discussion started by: rcsapo
8 Replies

2. UNIX for Dummies Questions & Answers

Sort Files based on the number(s) on the file name

Experts I have a list of files in the directory mysample1 mysample2 mysample3 mysample4 mysample5 mysample6 mysample7 mysample8 mysample9 mysample10 mysample11 mysample12 mysample13 mysample14 mysample15 (4 Replies)
Discussion started by: dsedi
4 Replies

3. Shell Programming and Scripting

How to count number of files in directory and write to new file with number of files and their name?

Hi! I just want to count number of files in a directory, and write to new text file, with number of files and their name output should look like this,, assume that below one is a new file created by script Number of files in directory = 25 1. a.txt 2. abc.txt 3. asd.dat... (20 Replies)
Discussion started by: Akshay Hegde
20 Replies

4. UNIX for Dummies Questions & Answers

Command to split the files based on the number of lines in it

Hello Friends, Can anyone help me for the below requirement. I am having a file called Input.txt. My requirement is first check the count that is wc -l input.txt If the result of the wc -l Input.txt is less than 10 then don't split the Input.txt file. Where as if Input.txt >= 10 the split... (12 Replies)
Discussion started by: malaya kumar
12 Replies

5. Shell Programming and Scripting

Make new files based on the number extracted

Hi All, I have a big file which looks like this: 0 4.5 6.7 0 3.4 6 1 5 6 1 6 4 2 9 4.44 Above is just a miniature version of the file. In fact, considering the first column of the file that is 0 0 1 1 2, the numbers in actual go until 10442. This means my actual file looks like... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

6. UNIX for Dummies Questions & Answers

How to list number of files through Crontab?

Hello All, I am trying to put this line in Crontab (Linux box- x86_64), but the command is not running. Can you please help me out? My requirement is- To find out the number of .csv files older than 1 day in directory /stage/landing. There are 2 other subdirectories under this directory... (6 Replies)
Discussion started by: NARESH1302
6 Replies

7. Shell Programming and Scripting

Creating folders based on number of files

I have hundreds of files numbered in consecutive number in one single folder What I would like to do is to make as many subfolders as needed (dependeing on the number of individual files) and name them Folder01, Folder02, etc. Then, move file01 to folder01, file02 to folder02 so on and so... (3 Replies)
Discussion started by: Xterra
3 Replies

8. Shell Programming and Scripting

Script to split files based on number of lines

I am getting a few gzip files into a folder by doing ftp to another server. Once I get them I move them to another location .But before that I need to make sure each gzip is not more than 5000 lines and split it up . The files I get are anywhere from 500 lines to 10000 lines in them and is in gzip... (4 Replies)
Discussion started by: gubbu
4 Replies

9. Shell Programming and Scripting

Split single file into multiple files based on the number in the column

Dear All, I would like to split a file of the following format into multiple files based on the number in the 6th column (numbers 1, 2, 3...): ATOM 1 N GLY A 1 -3.198 27.537 -5.958 1.00 0.00 N ATOM 2 CA GLY A 1 -2.199 28.399 -6.617 1.00 0.00 ... (3 Replies)
Discussion started by: tomasl
3 Replies
Login or Register to Ask a Question