non awk solution
#!/bin/sh
field=$1
value=$2
cut -f$field /some/file | grep -c $value
Main Topics
Browse All TopicsHi all,
I have text file on unix server in which the fields are seperated by any delimeter like pipe or comma etc. My question is i want to extract those records only which satisfies certain condition.
For eample if the file contains follwoing records
A| ABC|123|0093
B|CDE|233|0987
C|ABC|344|2345
my requirement is I want the count of the records where the second field is "ABC" and those records.
Here I have to pass the field number and field value dynamically to the script or as parameters.
Thanks in advance,
Sharath
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi Tintin,
My file has a delimeter like pipe or comma to seperate fields. You are not taking care of that. Because of that its not cutting the exact field from the file.
For example:
My file is test.txt whith the data as
a|bcd|cde|123
c|bcd|abc|
||bcd|swd
cut -f2 "test.txt" | grep "bcd" command is giving output as
a|bcd|cde|123
c|bcd|abc|
||bcd|swd
It should not give the 3rd record in which the "bcd" is occuring in 3rd column.
But stil ther ia some problem with your commands.
It won't give the complete record.
try to execute the your command
line=`cut -d'|' -f$field /some/file | grep $value`
you are cutting a particular field from the file and searching for the value.
The input to the grep is the column only. How can you get the complete record?
you need a trick like below ( add magic field at start as extra field, grep , then cut it out )
$ cat x1.txt
A|ABC|YZQ|0093
B|CDE|233|0987
C|ABC|656|0001
C|AXC|344|2345
C|AFC|ABC|8982
C|ABC|551|0002
$ cat x2.ksh
#!/bin/ksh
P1=$1
if [ "${2}HH" == "HH" ]; then
P2=1
else
P2=$2
fi
cut -f$P2 -d"|" x1.txt | sed -e "s/$/|/" > x1_extra.txt
paste x1_extra.txt x1.txt | sed -e "s/ //" > x1_total.txt
grep "^${P1}|" x1_total.txt | cut -f2- -d"|"
echo Count of ${P1} at field ${P2} is `grep "^${P1}|" x1_total.txt|wc -l`
$ x2.ksh ABC 2
A|ABC|YZQ|0093
C|ABC|656|0001
C|ABC|551|0002
Count of ABC at field 2 is 3
$ x2.ksh AXC 2
C|AXC|344|2345
Count of AXC at field 2 is 1
$ x2.ksh ABC 3
C|AFC|ABC|8982
Count of ABC at field 3 is 1
Hi Tintin,
I am getting desired result with your code. One more concern is can we parameterise the delimeter in the awk also? ( we can parametrise the delimeter in the cut command)
Because my files are some times comma delimeted and some times pipe delimeted.
Hi HamdyHassan,
I have not check your code. I will check and let you know whether it is working fine or not.
regards,
Sharath
----- some times comma delimeted and some times pipe delimeted
Yes you can do that
instead of -d"|"
you use -d$MyDel
make sure you check $3 as I did for $2, may be you can use default "|" in case $3 is empty
if [ "${2}HH" == "HH" ]; then
P2=1
else
P2=$2
fi
also when you can at command prompt, you need to put " " around | otherwise shell consider it as pipline
$ x2.ksh ABC 2 "|"
Business Accounts
Answer for Membership
by: ahoffmannPosted on 2006-09-26 at 06:03:38ID: 17600709
awk -F'|' '($2=="ABC"){print}' file
Note that this does not match *your* line A
> .. I have to pass the field number and field value dynamically to the script or as parameters
# assuming following in a script where $par contains your pattern
awk -F'|' '($2=="'$par'"){print}' file