-1

I'm having a file called sam1.txt and it has data is deliminted by "~".Find count of center under specific column called category?

  • Check the solution from: https://unix.stackexchange.com/a/25144/52750 for the column definition you could use -v cols=$(head -1 file.csv) and then grep -c *your patt* for counting – igiannak Jul 17 '17 at 11:06

1 Answers1

0

Here's how I would do it:

  1. Get the first line using head.
  2. In that line, get the string up to "Category" using grep -o.
  3. Count the number of occurrences of ~ in that string using grep -c to figure out which column number contains the category.
  4. Get that column using awk -F'~' (possibly using tail to get rid of the first line).
  5. Get the categories containing center using grep, being careful to anchor the string on both sides to avoid matching things like "off-center" or "centered".
  6. 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
  • can you please share some code so that i can understand easily – Sunitha Bist Jul 17 '17 at 11:14
  • 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