I'm having a file called sam1.txt and it has data is deliminted by "~".Find count of center under specific column called category?
Asked
Active
Viewed 62 times
-1
1 Answers
0
Here's how I would do it:
- Get the first line using
head
. - In that line, get the string up to
"Category"
usinggrep -o
. - Count the number of occurrences of
~
in that string usinggrep -c
to figure out which column number contains the category. - Get that column using
awk -F'~'
(possibly usingtail
to get rid of the first line). - Get the categories containing
center
usinggrep
, being careful to anchor the string on both sides to avoid matching things like "off-center" or "centered". - Finally, count the number of lines (that is, entries) with a category of "center" using
wc -l
.
These commands are all well documented in their man
pages.

l0b0
- 51,350
-
-
1@SunithaBist It's quite clear that the answer specifically avoids spoon feeding you with code that you can just run, preferring that you instead write and understand it. Feel free to update your question with your work-in-progress code so that specific problems can be addressed. – Jul 17 '17 at 12:07
-v cols=$(head -1 file.csv)
and thengrep -c *your patt*
for counting – igiannak Jul 17 '17 at 11:06