• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

how to filter name w.r.t to town in a notepad file

Hello Geeks,

I need to find the names of all those persons in a text file on the basis of town.

for example if the town matches Djibouti,then it should reflect me the name.

I want this in unix environ ment

NAME  RAJ Kumar

DOB 22121987

TOWN DIGBOI

NAME Shanti

DOB 01111986

TOWN ITANAGAR

NAME ROSY

DOB 07082010

TOWN DJIBOUTI
0
raj2k1
Asked:
raj2k1
  • 2
1 Solution
 
arnoldCommented:
Is perl an option?
Is the data in the file as you posted
Name
Dob
Town

Presumably you are aware that this is a help rather than a seemingly homework assignment that experts here will solve for you?

Can you post what script you attempted and have?
0
 
skullnobrainsCommented:
if the input file looks like the above, this should work

sed -e 's/^\([^[:space:]]\)[[:space:]]\(.*\)$/\1="\2"'/ -e 's/^TOWN/&;if test town = "jibouti" ; then echo -e "NAME $NAME\nDOB $DOB\nTOWN $TOWN\n" ; fi/' | sh

beware this will only work providing
-> the format of the file is the one provided without missing lines or lines in a different order
-> the values never contain double-quotes

----

it would probably be easier using awk, perl, ... but here is the shell way
0
 
skullnobrainsCommented:
oups forgot to explain my mess

you need to pipe the original file into the script

-> the first -e sed transformation takes lines of the form
NAME VALUE
and change them to
NAME="VALUE"

-> the second one adds a conditional echo after the town that will output the 3 values of an entry if and only if the town is jibouti

try it without the "|sh" : at this step, the script builds an sh script

-> and the last step is to execute the script

---

btw, it would be a good exercise to write this in plain sed, but i'm way too lazy
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now