-1

I'm trying to write a script that takes a string and then reads the file test and prints the names that starts with a pattern (ignoring case) to another file, the test file contains this data:

u001:x:Laith_Budairi
u002:x:laith_buda
u003:x:bara_adnan
u004:x:Basim_khadir
u005:x:bilal_jarrar

this is what i tried to do:

echo type a pattern to find
read s
cat test | cut -d: -f3 | grep -i '^$s' > printfile

but it doesn't print anything there in the file what do I do ?

kabanus
  • 24,623
  • 6
  • 41
  • 74
  • Shell variables inside single quotes are not interpreted. – revo Apr 22 '18 at 08:44
  • *"what do I do ..."* - [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Apr 22 '18 at 12:39

1 Answers1

0

'...' prevent variable expansion. Simply changing '^$s' to "^$s" would work, but there are other things to improve:

  1. You don't need to cat, you can send cut a filename:

    cut -d: -f3 test | grep ...

  2. You do not really need the cut, you could grep immediately:

    grep -i "^u...:x:$s" test > printfile

Though if you only one the name part than it's fine.

kabanus
  • 24,623
  • 6
  • 41
  • 74