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?

John M.
  • 245
  • 1
  • 3
  • 13
  • i think you can find a solution here: https://stackoverflow.com/questions/19951369/how-to-store-grep-command-result-in-some-variable-in-shell – rosco Jan 17 '18 at 14:46
  • 1
    Use `awk` as: `awk -v pin="$pin" '$1 == pin { print $2, $3 }' data.txt` – anubhava Jan 17 '18 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. – John M. Jan 17 '18 at 14:51

3 Answers3

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
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
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
Viktor Khilin
  • 1,760
  • 9
  • 21
  • 1
    no need to use `grep` here as `awk` could search a specific pattern with shell variable's value itself too see this https://stackoverflow.com/a/48303804/5866580 – RavinderSingh13 Jan 17 '18 at 14:49
  • 1
    Nice! Any chance this can work with using only grep? I don't know how to use awk. – John M. Jan 17 '18 at 14:53
  • @JohnM. I don't see the way using only `grep`. But if you don't like `awk`, added another solution with `cut`. – Viktor Khilin Jan 17 '18 at 14:56
  • Nice, second option using cut is exactly what i was looking for. I simply needed to swap "${pin}" with "^$pin" to make it work. – John M. Jan 17 '18 at 15:05
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 \)

romaric crailox
  • 564
  • 1
  • 3
  • 15