I wrote a shell script like this:
#! /bin/sh ... ls | grep "android" ...
and the output is :
android1 android2 xx_android ...
I want to add a number in each file, like this:
1 android1 2 android2 3 XX_android ... please choose your dir number:
and then wait for the user input line number x, the script reads the line number back then process the corresponding dir. How can we do this in shell ? Thanks !
The -n ( or --line-number ) option tells grep to show the line number of the lines containing a string that matches a pattern. When this option is used, grep prints the matches to standard output prefixed with the line number.
1. Using the read built-in command. One line is read from the standard input, or from the file descriptor supplied as an argument to the -u option.
nl prints line numbers:
ls | grep android | nl
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With