Introduction

The tutorial is about how to use cat,sed and awk commands to display file contents . when we start with linux basic commands, we generally introduced with cat command to show file contents. But with the help of sed and awk command also, you can see the file contents. The output will be similar to cat command.

How to show file contents on Linux/Unix systems

With the help of 3 commands cat, sed and awk we can read or show the file contents.

(1) Cat command

cat command is used for concatenating files and print on the standard output .
Use the below given syntax to display file contents –

cat /path/of/file

Example:

cat command

(2) Sed Command

For stream editor for filtering and transforming text we use sed command. The command is very powerful. Knowledge of regex is must for using sed command.

Use the below given syntax to display file contents –

sed '' /path/of/file

Example:

sed command

Awk command

awk is a pattern scanning and text processing language.

Use the below given syntax to display file contents –

awk '{print $0}' /path/of/file

Example:

awk command

We hope these commands will be helpful for you. We can use these commands in scripting as well.

Read Some More Articles

One Comment

  1. Hi sharad,

    I am planning to verify data content and extension of a file matches. but for excel sheet, I need to check whether “CMD” command is there inside the file without using grep condition. Please find the below piece of code:
    filename=$1
    file_ext=`echo $filename | cut -d “.” -f2`

    if [ $file_ext == “xlsx” ]; then
    temp_ext=” Microsoft Excel 2007+”
    elif [ $file_ext == “docx” ]; then
    temp_ext=” Microsoft Word 2007+”
    elif [ $file_ext == “pdf” ]; then
    temp_ext=” PDF document”
    elif [ $file_ext == “doc” ]; then
    temp_ext=” document”
    elif [ $file_ext == “xls” ]; then
    temp_ext=” CDF V2 Document”
    fi

    result=$(file $filename|cut -d “,” -f1)
    if [[ $result == $temp_ext ]]; then
    echo “Valid file format, Extension and Data content is $temp_ext and $result”
    else
    echo “Not a valid file format!! Extension and Data content is $temp_ext and $result”
    fi

    Here before the last else statement, need to check whether the cmd command is therein the file without using grep. please help me with this.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.