sed ?s/[sub_str]/[sub_str]\n/g? [text_file] | wc -l

count the number of times you match a substring in a larger text file


-3
By: opexxx
2013-03-28 12:00:56

These Might Interest You

  • -c will count the number of times your search matches in the file. Show Sample Output


    0
    grep -c "search_string" /path/to/file
    meatflag · 2013-12-10 18:13:54 0
  • Reduce the number of keystrokes it takes to open a file in vim. First of all, you just need to type "v", which is less than half the number of characters (!), and second-of-all, you only need to enter a substring of the file you want to open. For example, if you want to open the file, homework.txt, then type "v hom" to open it. Good tip is to use the lowest unique substring, otherwise you'll open multiple files in different buffers (which is sometimes desirable). Use Ctrl-^ to switch between buffers.


    0
    function v { if [ -z $1 ]; then vim; else vim *$1*; fi }
    kFiddle · 2009-04-11 23:06:43 2
  • Join all sequentially named files in the directory. Use this for files split by utilities like hjsplit and similar. This command does not do/perform _any_ sanity checks before acting, except that it won't run unless there is a file that matches "*.001". - The outfile should not already exist. - There should be more than one file. (*.002 should exist as well as *.001) - The file-count should match the number in the name of the last file in the series. - None of the files should be empty. - All files should be the same size, except for the last, which should usually be smaller, but never larger than the rest. A safer altenative can be found here: http://pastebin.com/KSS0zU2F


    0
    x=(*.001); cat "${x%.001}."* > "${x%.001}" #unsafe; does not check that all the parts are there, or that the file-sizes make sense!
    Jessehz · 2011-08-24 04:10:20 0
  • Count how many times a pattern is present into a stream. It can be one or more lines. No overlapping. It means searching for aa on aaa will output 1 not 2.


    0
    echo something | awk '{ total += gsub(/yourstring/,"") } END { print total }'
    bugmenot · 2014-12-16 20:58:42 0
  • This command produces no output, but its exit status is 0 ("true") if $file is text, non-0 ("false") if $file is binary (or is not accessible). Explanation: -q suppresses all the output of grep -I is the trick: if a binary file is found, it is considered a non-match -m 1: limit "output" to first match (speed up for big files) .: the match string, "." stands for any character Usage: e.g. run editor only on text files grep -qIm 1 . $file && vi $file


    0
    grep -qIm1 . $file
    anon1251 · 2013-03-28 14:11:51 0
  • I often find the need to number enumerations and other lists when programming. With this command, create a new file called 'inputfile' with the text you want to number. Paste the contents of 'outputfile' back into your source file and fix the tabbing if necessary. You can also change this to output hex numbering by changing the "%02d" to "%02x". If you need to start at 0 replace "NR" with "NR-1". I adapted this from http://osxdaily.com/2010/05/20/easily-add-line-numbers-to-a-text-file/. Show Sample Output


    1
    awk '{printf("/* %02d */ %s\n", NR,$0)}' inputfile > outputfile
    lucasrangit · 2011-01-04 19:13:55 0

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: