eTechtips

Tech Tips and Insights

Tag: Shell

  • Counting lines, words and characters with wc

    There is a simple Linux utility for counting characters, lines and words in files called wc. This allows you to give a file as an argument or a stream and get the counts of the aforementioned items.

    wc <filename>

    or

    cat <filename> | wc

    The options that wc accepts

    -c,–bytes print the byte counts
    -m,–chars print the character counts
    -l,–lines print the line counts
    -L,–max-line-length print the length of the longest line
    -w,–words print the word counts

    The ouput with no arguments will be three numbers (words,lines,characters. Otherwise the the number will reflect the argument you have passed.

    And if you pass multiple filenames the output displays counts for all files and totals.

     >wc -l HelloWorld.java sample.txt
        8 HelloWorld.java
       25 sample.txt
       33 total
    0 Comments
  • Linux:Script to find files

    I regularly have to find where a file is located and usually use find:

    find . | grep 
    

    Where is replaced with the filename or partial name to match. So I finally decided to write a script to just run this and allow me to print the matching names and even potentially pass the -i flag to grep.

    #!/bin/sh
    
    
    if [ $# -lt 1 ]; then
      echo 1>&2 Usage: $0 ""
      exit 127
    fi
    icase=
    
    while [ $# -ge 1 ]; do
       case $1 in
         -i)  icase=$1;;
          *)  search=$1 ;;
       esac
       shift
    done
    
    find .  | grep $icase $search
    

    This is just a simple script which I have named ffind that searches starting from the current directory. This lets me search with the following commands:

    ffind bak$
    

    This will search for all files that end with “bak” .

    ffind -i edr
    

    This will search for all files with “edr” regardless of case

    0 Comments
  • Using find and grep to search files

    I have been searching through source code and files for years. And I used to use find with a few arguments to get what I was looking for.

    find . -exec grep  {} ; -print

    I used the -print after the exec arguments to show the file only when the grep succeeded.  This worked but was a bit to type each time I used it. So I came up with a little script that would help me to search for the an argument and even allow a case insensitive search as well.
     

    #!/bin/sh
    
    if [ $# -lt 1 ]; then
      echo 1>&2 Usage: $0 ""
      exit 127
    fi
    icase=
    search=
    
    while [ $# -ge 1 ]; do
      case $1 in 
         -i) icase=$1;;
          *) search=$1 ;;
      esac
      shift
    done
    
    find . -type f -print0 | xargs -0 grep $icase $search

    The script takes at least one argument and excepts a ‘-i’ argument to make grep use a case insensitive search. This will print the file name and the line that matches the search term. It can be easily adapted to show the count of matched patterns (‘-c’) or the line number (‘-n’).

    0 Comments
  • Gallery of Hello World!

    Here is a list of the Hello World Programs for different languages:

    C

    #include <stdio.h>
    int main()
    {
    printf("Hello World!");
    }

    C++

    #include <iostream.h>
    int main()
    {
    cout << "Hello World!" << endl;
    }

    C#

    public class HelloWorld
      public static void Main()
      {
        System.Console.WriteLine("Hello World!");
      }

    Java

    class HelloWorld {
    static void main(String[] args)
    {
    System.out.println("Hello World!");
    }

    SHELL

    echo "Hello World"

    Python 2

    print "Hello World!\n"

    Python 3

    print ("Hello World!")

    Ruby

    puts "Hello World!"

    Perl

    print "Hello World!n";

    PHP

      <?php      
        print "Hello World!";
      ?>

    Rust

    fn main() {
        println("Hello World!");
    }

    This is the simplest form of Hello World for most of these languages.

    0 Comments