-2

I have a file which has N number of line For example

This/is/workshop/1
This/is/workshop/2
This/is/workshop/3
This/is/workshop/4
This/is/workshop/5

How to get the below result using uniq command:

This/is/workshop/ =5
January
  • 16,320
  • 6
  • 52
  • 74
varun kumar
  • 133
  • 2
  • 8
  • Your question is unclear. What is the common pattern and what is the repeated word? Do you only search for this particular string (`This/is/workshop/`), or do you have a range of strings? – January Jul 23 '19 at 10:45
  • I meant "This/is/workshop/" is common to every line.Also i have range of strings – varun kumar Jul 23 '19 at 10:48
  • Is there a pattern for your “pattern” (the prefix string)? – randomir Jul 23 '19 at 10:49
  • Yes In this example "This/is/workshop/" is my pattern .This include "/" also – varun kumar Jul 23 '19 at 10:52
  • So, which repeated word are you trying to count? The numbers that come after your pattern are not repeated. Also, which range of strings? Can you give us an example? – January Jul 23 '19 at 10:58
  • Sorry I post my question in wrong way .Now i will explain with Clear picture I have a file which has following lines This/is/workshop/1 This/is/workshop/2 This/is/workshop/3 This/is/workshop/4 if we use uniq -c file_name i will get 1 This/is/workshop/1 1 This/is/workshop/2 .. But i need following result 5 This/is/workshop Is this possible using uniq. If not which command should i use in Linux. – varun kumar Jul 23 '19 at 11:00

1 Answers1

0

Okay so there are a couple tools you can utilize here. Familiarize yourself with grep, cut, and uniq. My process for doing something like this may not be ideal, but given your original question I'll try to tailor the process to the lines in the file you've given.

First you'll want to grep the file for the relevant strings. Then you can pass it through to cut, declaring the fields you want to include by specifying the delimiter and also the number of fields. Lastly, you can pipe this through to uniq to count it.

Example:

Contents of file.txt

This/is/workshop/1 This/is/workshop/2 This/is/workshop/3 This/is/workshop/4 This/is/workshop/5

Use grep, cut and uniq

$ grep "This/is/workshop/" file.txt | cut -d/ -f1-3 | uniq -c 5 This/is/workshop

To specify the delimiter in cut, you use the -d flag and the delimiter you want to use. Each field is what exists between delimiters, starting at 1. For this, we want the first three. Then just pipe it through to uniq to get the count you are after.

kegn
  • 756
  • 6
  • 6