3

I'm new to linux and i decided to learn shell scripting. I have created a file data.txt that contains the following text:

12345 Nick Abrams A 10900
67890 George Kennedy I 20000
(text goes on...)

The first field is a card's pin number, the second is the name of the client, third is surname, fourth indicates whether a card is active (or inactive) and the last field is the client's balance. I need to write a script that receives the client's pin from keyboard and if that pin is written in the text file then the script should print the client's name and surname on the screen. I have used grep like this

grep "^$pin" data.txt

But it returns all the details of a client. I only need to print the second and third field and ignore everything else. Is there any parameter to isolate the words i need?

3
  • i think you can find a solution here: stackoverflow.com/questions/19951369/… Commented Jan 17, 2018 at 14:46
  • 1
    Use awk as: awk -v pin="$pin" '$1 == pin { print $2, $3 }' data.txt Commented Jan 17, 2018 at 14:46
  • I believe i didn't explain well what i really want. I meant that if a user inputs 12345, the script should print "Nick Abrams". It's not necessary to save it in a variable, just to print it on screen. BTW i prefer to use grep because i haven't reached far enough to learn how to use awk. Commented Jan 17, 2018 at 14:51

3 Answers 3

2

Could you please try following and let me know if this helps you.

cat script.ksh
echo "Please enter your choice:"
read value

awk -v val="$value" '$1==val{print $2,$3}' Input_file

EDIT: Adding a solution with grep and cut in a script too here.

cat script.ksh
echo "Please enter your choice:"
read value

grep "$value" Input_file | cut -d" " -f2,3
Sign up to request clarification or add additional context in comments.

2 Comments

It works thanks. However is it possible to use grep as an equivalent?
@down voter, please add your reason of down vote. It is not fair :(
1

Better use sed :

sed -n 's/^'"$pin \([^ ]* [^ ]*\).*"'/\1/p' data.txt

This command match a line that start by $pin, and write only part that follow regex between \( and \)

2 Comments

OP needs to have a logic to ask user input too in code.
@RavinderSingh13 I answer initial question that is only on how to print the second and third field and ignore everything else
1
#!/bin/bash

echo "Enter PIN, please"
read pin

grep "${pin}" pins.txt | awk '{print $2" "$3}'

Input: 12345

Output: Nick Abrams

Or with cut:

#!/bin/bash

echo "Enter PIN, please"
read pin

grep "${pin}" pins.txt | cut -d' ' -f2,3

4 Comments

no need to use grep here as awk could search a specific pattern with shell variable's value itself too see this stackoverflow.com/a/48303804/5866580
Nice! Any chance this can work with using only grep? I don't know how to use awk.
@JohnM. I don't see the way using only grep. But if you don't like awk, added another solution with cut.
Nice, second option using cut is exactly what i was looking for. I simply needed to swap "${pin}" with "^$pin" to make it work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.