0

I want to create bash script, which will find all words starting with user entered letter in specified file (specified in script or create user input for it too). I am a little bit more than total beginner in linux, my code is:

    #! /bin/bash

echo 'Please enter starting letter of Name'
read name
result=$(awk '/$name/ {print}' /home/beka/scripts/names.txt)
echo "$(result)"

this results error like:

    Please enter starting letter of Name
G
/home/beka/scripts/test.sh: line 6: result: command not found

what I am doing wrong? I tried to search awk examples but can't find exact solution. Thank you in advance.


EDITED CODE

#! /bin/bash

echo 'Please enter starting letter of Name'
read name

if [[ $name == [A-Z] ]]
then 
awk "/$name/{print}" /home/beka/scripts/names.txt
else
echo '0'
fi

EDIT names.txt is a list of names

Michael
Christopher
Jessica
Matthew
Ashley
Jennifer
Joshua

ANOTHER EDIT

#! /bin/bash

echo 'Please enter starting letter (Uppercase) of name'
read name

if [[ $name == [A-Z] ]]
then 
echo "---Names starting with $name---"
awk "/$name/{print}" /home/beka/scripts/names.txt
elif [[ $name == [a-z] ]]
then
awk "/$name/{print}" /home/beka/scripts/names.txt
else
echo '---------'
echo 'Names not found'
fi
12
  • echo "$(result)" should be echo "$result". And $name is not known to awk when you put it in single quotes.
    – pLumo
    Commented Sep 24, 2020 at 11:32
  • You need to use double quotes instead of single quotes in order to properly expand variables Commented Sep 24, 2020 at 11:36
  • 1
    How do you define word? is it sequence of alnums and underscore, sequences of non-whitespace? How are they stored in names.txt? Commented Sep 24, 2020 at 11:36
  • @user1700494 in this case i would use awk -v option and not double quotes... especially when depending on user input.
    – pLumo
    Commented Sep 24, 2020 at 11:37
  • @pLumo, -v has problems of its own with backslash. Better to use an environment variable and awk's ENVIRON array. Commented Sep 24, 2020 at 11:38

1 Answer 1

2

echo "$(result)" is trying to execute a command named result courtesy of the $(result) part, hence the error message result: command not found.

Try this (untested):

#!/usr/bin/env bash

result=''
while [[ -z "$result" ]]; do
    echo 'Please enter starting letter of Name'
    read name

    if [[ $name == [A-Z] ]]
    then 
        result=$(awk -v name="$name" 'index($0,name)==1' /home/beka/scripts/names.txt)
    else
        echo '0'
    fi
done
echo "$result"

To make the search case-insensitive:

awk -v name="$name" 'index(tolower($0),tolower(name))==1' /home/beka/scripts/names.txt

Obviously you'll also have to change $name == [A-Z] to $name == [a-zA-Z] or $name == [[:alpha:]] if you want to be able to accept a lower case letter as the search character.

8
  • it lacks "done" instead of "fi"before last echo, but it works fine as loop. Commented Sep 24, 2020 at 13:53
  • it is case sensitive as I see, is there any way to search e.g. James with j ( I mean to search with lowercase and find Uppercase as starting letter)? Commented Sep 24, 2020 at 13:56
  • it makes no difference. Commented Sep 24, 2020 at 14:00
  • 1
    now it's clear to me, changing code piece and adding a-z to search criteria made the magic :) Thank you very much. Commented Sep 24, 2020 at 14:09
  • 2
    Chamelon questions are discouraged in this forum, please always just accept an answer to the question you asked and ask a new followup question if you have one.
    – Ed Morton
    Commented Sep 24, 2020 at 14:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .