Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

First off, I'm not allowed awk, sed or perl.

For my assignment I was given a file of employee list:

Marketing  Ranjit Singh   FULLEagles       Dean Johnson   
Marketing  Ken Whillans   FULLEagles       Karen Thompson 
Sales      Peter RobertsonPARTGolden TigersRich Gardener  
President  Sandeep Jain   CONTWimps        Ken Whillans   
Operations John Thompson  PARTHawks        Cher           
Operations Cher           CONTVegans       Karen Patel    
Sales      John Jacobs    FULLHawks        Davinder Singh 
Finance    Dean Johnson   FULLVegans       Sandeep Jain   
EngineeringKaren Thompson PARTVegans       John Thompson  
IT         Rich Gardener  FULLGolden TigersPeter Robertson
IT         Karen Patel    FULLWimps        Ranjit Singh   

Long story short, the user is prompted to give a name or portion of a name and that word is searched for ONLY in the second 'column'. If it is found there then the user is then asked whether they want the person's team-name (third column, word next to "PART" or "FULL" etc.) or the person's partner (last column).

The end result is then only to show the full name alongside the team name or partner.

I can't figure out the last step... Cutting only the lines with the matches to the original search and displaying only the needed 'columns'.

while :
do
    echo Enter the name of the player you want to search for:
    read name
    if (cut -c12-26 emplist | grep -n ${name} > namefile)
    then
        while :
        do
            echo 'See partner (P/p) or team name (T/t)?'
            read answer
            if [ ${answer} = "T" -o ${answer} = "t" ]
            then
                **#Steps for showing only the full name and the team name** 
                break
            elif [ ${answer} = "P" -o ${answer} = "p" ]
            then
                **#Steps for showing only the full name and the partner name**
                break
            else
                echo Please enter only T or M.
            fi
            done
    elif [ ${name} = "ZZZ" ]
    then
        break
    else
        echo Name not found.
    fi
done
share|improve this question
    
I assume there are actually spaces between Engineering and Karen and the three other cases where your columns are fused right? Are the columns separated by tabs? How can you know whether there will be a full name (John Doe) or not (Cher) otherwise? –  terdon May 23 at 10:11
    
@terdon, AFAICS, columns have fixed width, there's no separator. –  Stéphane Chazelas May 23 at 11:50
    
Don't forget to quote your variables. You have the quotes in the wrong places. [ ${name} = "ZZZ" ] should be [ "$name" = ZZZ ]. The {} around name and "" around zzz are not necessary, the quotes around $name (or ${name}) are. quotes don't serve the same purpose in shells as they do in other languages. Consider using case instead of if/elif/... here. –  Stéphane Chazelas May 23 at 12:23
    
I rolled back your edit. Why did you remove the input file? We need to see that in order to be able to help! –  terdon May 23 at 16:12

2 Answers 2

You could do something like (here for searching John and reporting the team):

$ cut -c12-26 <emplist | paste -d: - emplist | grep '^[^:]*John' | cut -c1-16,47-59
John Thompson  :Hawks
John Jacobs    :Hawks
Dean Johnson   :Vegans
share|improve this answer

Stephane already gave you one approach using cut but if you want to do this purely in bash, you could try something like:

#!/usr/bin/env bash

## Declare the associative arrays that will
## hold your data
declare -A partner;
declare -A team;

## Read the data
while IFS= read line
do
    ## Extract the relevant column
    fullname="${line:11:15}"

    ## save the other columns in their arrays
    partner["$fullname"]="${line:43}"
    team["$fullname"]="${line:30:13}"
done < emplist

echo "Enter the name of the player you want to search for:"
read name

## Check that the name exists or exit
if ! grep -q "$name" emplist 
then
     echo "Name not found"
     exit 
fi


## Read what we're after
while true;
do
    echo "See partner (P/p) or team name (T/t)?"
    read answer
    case $answer in
        [tTpP])
            break
            ;;
        *)
            echo "Please enter only T or M."
    esac          
done
## Now go through each of the names in the 
## second column and check if they match the
## name requested.
for fullname in "${!partner[@]}"
do
    if [[ $fullname =~ $name ]]; 
    then 
            case $answer in
                [tT])
                    printf "%s\t%s\n" "$fullname" "${team[$fullname]}"
                    ;;
                [pP])
                    printf "%s\t%s\n" "$fullname" "${partner[$fullname]}"
                    ;;
            esac
    fi
done
share|improve this answer
    
This helps me a lot. I think I forgot to mention, though, that if there are more than 1 instances of, for example, "John" in the list (John Jacobs and Dean Johnson) both names along with their partner or team name should be displayed at the end. Similar to what Stephane's result looks like –  user68335 May 23 at 16:06
    
@user68335 OK, see the updated answer. I changed the approach a bit and it now prints all matching lines. As long as your file is not too huge to fit into memory, you'll be fine. If the file is big, this will be slow since it needs to read the whole thing before returning output. However, if your file is too big for this approach, you really shouldn't be doing it in bash in the first place. –  terdon May 23 at 16:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.