Tutorials Hut

  • Unix For Testers

       Introduction to Unix
       Unix System Architecture
       Unix File System Structure
       Absolute and Relative Path
       Basic Unix Commands
       cal command in Unix
       who command in Unix
       date command in Unix
       clear command in Unix
       more command in Unix
       whoami command in Unix
       uname command in Unix
       man command in Unix
       echo command in Unix
       Unix File System Commands
       ls command in Unix
       cat command in Unix
       cp command in Unix
       mv command in Unix
       pwd command in Unix
       cd command in Unix
       mkdir command in Unix
       rmdir command in Unix
       rm command in Unix
       touch command in Unix
       dirname command in Unix
       tar command in Unix
       Unix Links(ln)
       Hard Link
       Soft Link
       Regular Expressions
        Basic Regular Expressions
       Interval Regular Expressions
       Pipes and Filters
       Unix Text Processing Commands
       cmp command in Unix
       diff command in Unix
       comm command in Unix
       cut command in Unix
       Paste command in Unix
       head command in Unix
       tail command in Unix
       wc command in Unix
       sort command in Unix
       grep command in Unix
       Process Related Command
       top command in Unix
       ps command in Unix
       nice command in Unix
       Kill command in Unix
       nohup command in Unix
       time command in Unix
       File Tranfer Commands in Unix
       file transfer using scp command
       file transfer using rlogin command
       file transfer using telnet command
       ssh(Secure Shell) command in Unix
       ftp file transfer command
       sftp file transfer command
       chmod command in Unix
       File Permission and File Security in Unix



  • Text Processing Unix Commands: sort and wc

    Text processing Unix commands  are those affecting text and text files. For example, commands like cut, paste, cmp, sort, comm, head, tail, wc, diff, grep are few in the list. While editing files, you may lose track of what changes you have made to which files.

    In this article, we will cover text processing Unix commands: sort and wc

    text processing Unix commands: sort and wc command in Unix

    sort command in Unix

    • Ordered arrangement.
    • sorts lines of text alphabetically or numerically, default sorting is alphabetical

    Rules for sorting are:

    1. Lines starting with a number will appear before lines starting with a letter. 2. Lines starting with letters are sorted alphabetically, lowercase case appears before tan uppercase.

    Sorting rules can be changed using OPTIONS.

    SYNTAX:
    sort [OPTION]..[FILE]..

    Option -d dictionary order sort Option -n arithmetic/numeric order Option -r reverse order Option –k n Sorts on field n Option –b ignore leading blanks Option –m Merge (not sort)

    Example:

    $cat file1.txt
    apples
    trees
    plants
    sun
    bananas

    sort the lines in this file alphabetically

    use the following command:

    $sort file1.txt
    apples
    bananas
    pears
    sun
    trees

    Note that this command does not actually change the input file, file1.txt. If you want to write the output to a new file, file2.txt, redirect the output like this:

    $sort file1.txt > file2.txt
    $cat file2.txt
    apples
    bananas
    pears
    sun
    trees

    Sorting In Reverse Order

    You can perform a reverse-order sort using the -r flag.
    $ sort -r file1.txt
    Output:
    trees
    sun
    pears
    bananas
    apples

    wc command in Unix

        • Counts and displays the number of lines, words and characters in a file.
        • By default it displays four-columnar output.
        • 4 columns are :the number of lines, words, byte counts and the name of the file passed as argument.
    SYNTAX:
    wc [OPTION].. [FILE]..
    Options

    Use

    -l

    Displays the number of lines in a file.

    -w

    Displays the number of words in a file.

    -c

    Displays the count of bytes in a file.

    -m

    Prints the count of characters from a file.

    -L

    Prints only the length of the longest line in a file.

    Example:

    $ wc /myData/file1
    Output:
    112 3345 23234 /myData/file1

    112 is the number of lines. 3345 is the number of words. 2323 is the number of characters.

    $ wc  -w /myData/file1
    Output:
    3345 /myData/file1

    -w option displays the word count.

    $ wc  -l /myData/file1
    Output:
    112 /myData/file1

    -l option displays the line count.

    Display information of more than one File using wc command

    You can see information of more than one file  by passing filename to the argument.

    SYNTAX:
    wc [OPTION].. [FILE1][FILE2]

    Example:

    $ wc /myData/file1 /myData/file2
    Output:
    112 3345 23234 /myData/file1
    101 2344 23201 /myData/file2
    213 5689 46435 total

    wc command with other Unix commands

    wc command can be used in combination wit other commands using piping to get information.

    Count records in CSV files

    To count the number of records in files the wc can used with pipes. Find out the records in all csv files(There are 5 csv files). This can be achieved by piping the output of the cat command to wc.

    $ cat *.csv | wc -l 
    2122
    There are 2122 records across the 5 files.

    Count the number files in a directory

    To count the number of folders and files in a directory wc can be combined with the ls command. By passing the -1 options to ls it will list one file per line. This can be piped to wc to give a count.

    $ ls -1 | wc -l
    16

    count number of times word appeared in a file

    You can use grep command to count the number of times “word” appears in the file.

    $ grep -o -i smart example.txt | wc -l
    Output: 3

    Using grep -c alone will count the number of lines that contain the matching word instead of the number of total matches. To know about about grep refer here. The -o option is what tells grep to output each match in a unique line and then wc -l tells wc to count the number of lines. This is how the total number of matching words is deduced.

    Recommended Articles:



  • Unix For Testers

       Introduction to Unix
       Unix System Architecture
       Unix File System Structure
       Absolute and Relative Path
       Basic Unix Commands
       cal command in Unix
       who command in Unix
       date command in Unix
       clear command in Unix
       more command in Unix
       whoami command in Unix
       uname command in Unix
       man command in Unix
       echo command in Unix
       Unix File System Commands
       ls command in Unix
       cat command in Unix
       cp command in Unix
       mv command in Unix
       pwd command in Unix
       cd command in Unix
       mkdir command in Unix
       rmdir command in Unix
       rm command in Unix
       touch command in Unix
       dirname command in Unix
       tar command in Unix
       Unix Links(ln)
       Hard Link
       Soft Link
       Regular Expressions
        Basic Regular Expressions
       Interval Regular Expressions
       Pipes and Filters
       Unix Text Processing Commands
       cmp command in Unix
       diff command in Unix
       comm command in Unix
       cut command in Unix
       Paste command in Unix
       head command in Unix
       tail command in Unix
       wc command in Unix
       sort command in Unix
       grep command in Unix
       Process Related Command
       top command in Unix
       ps command in Unix
       nice command in Unix
       Kill command in Unix
       nohup command in Unix
       time command in Unix
       File Tranfer Commands in Unix
       file transfer using scp command
       file transfer using rlogin command
       file transfer using telnet command
       ssh(Secure Shell) command in Unix
       ftp file transfer command
       sftp file transfer command
       chmod command in Unix
       File Permission and File Security in Unix

















  • Leave a Reply

    Your email address will not be published. Required fields are marked *