Skip to content

Practice Exercise: Text Manipulation with grep and strings

Objective

Learn how to manipulate text in Linux using the grep and strings commands.

Task 1: Searching Text with grep

  • Open a terminal window.
  • Create a sample text file named sample.txt with several lines of text containing different words and phrases.
  • Use the grep command to search for a specific word or phrase within sample.txt. For example, find all occurrences of the word "Linux."
  • Experiment with different grep options such as case-insensitive search and displaying line numbers.
    # Create a sample text file named sample.txt with several lines of text
    [intern@intern-a1t-inf-lnx1 ~]$ cat > sample.txt <<EOF
    Linux is an open-source operating system.
    Linux commands are powerful.
    You can learn Linux.
    Linux is case-sensitive.
    Linux is fun to learn.
    EOF
    
    # Use the grep command to search for the word "Linux" in sample.txt
    [intern@intern-a1t-inf-lnx1 ~]$ grep "Linux" sample.txt
    Linux is an open-source operating system.
    Linux commands are powerful.
    You can learn Linux.
    Linux is case-sensitive.
    Linux is fun to learn.
    
    # Use grep with the -i option for a case-insensitive search
    [intern@intern-a1t-inf-lnx1 ~]$ grep -i "linux" sample.txt
    Linux is an open-source operating system.
    Linux commands are powerful.
    You can learn Linux.
    Linux is case-sensitive.
    Linux is fun to learn.
    
    # Use grep with the -n option to display line numbers
    [intern@intern-a1t-inf-lnx1 ~]$ grep -n "Linux" sample.txt
    1:Linux is an open-source operating system.
    2:Linux commands are powerful.
    3:You can learn Linux.
    4:Linux is case-sensitive.
    5:Linux is fun to learn.
    

Task 2: Recursive Search with grep

  • Create a directory named text_files and populate it with multiple text files.
  • Use the grep command with the -r or -R option to perform a recursive search for a specific text pattern within the text_files directory.
    # Create a directory named text_files
    [intern@intern-a1t-inf-lnx1 ~]$ mkdir text_files
    
    # Change to the text_files directory
    [intern@intern-a1t-inf-lnx1 ~]$ cd text_files
    
    # Create multiple text files with sample content
    [intern@intern-a1t-inf-lnx1 text_files]$ echo "This is a sample file 1." > file1.txt
    [intern@intern-a1t-inf-lnx1 text_files]$ echo "Sample content in file 2." > file2.txt
    [intern@intern-a1t-inf-lnx1 text_files]$ echo "Another file with sample text." > file3.txt
    [intern@intern-a1t-inf-lnx1 text_files]$ echo "This is file 4." > file4.txt
    [intern@intern-a1t-inf-lnx1 text_files]$ echo "File 5 contains sample text." > file5.txt
    
    # Return to your home directory
    [intern@intern-a1t-inf-lnx1 text_files]$ cd ..
    
    # Use the grep command with -r option to perform a recursive search
    [intern@intern-a1t-inf-lnx1 ~]$ grep -r "sample" text_files
    text_files/file2.txt:Sample content in file 2.
    text_files/file5.txt:File 5 contains sample text.
    text_files/file3.txt:Another file with sample text.
    text_files/file1.txt:This is a sample file 1.
    

Task 3: Extracting Text from Binary Files with strings

  • Create a binary file named binary_data.bin. Simply copy and linux binary file
  • Use the strings command to extract readable text from the binary_data.bin file.
  • Observe how strings extracts human-readable text from a binary source.
    [intern@intern-a1t-inf-lnx1 ~]$ cp $(which cp) binary_data.bin
    
    [intern@intern-a1t-inf-lnx1 ~]$ strings binary_data.bin | head
    /lib64/ld-linux-x86-64.so.2
    libselinux.so.1
    _ITM_deregisterTMCloneTable
    __gmon_start__
    _ITM_registerTMCloneTable
    context_str
    mode_to_security_class
    getcon
    is_selinux_enabled
    context_type_set
    

Task 4: Combining grep and strings

  • Use the copy of cp bin named binary_data.bin
  • Use grep in conjunction with strings to search for a specific text pattern within binary_text.bin.
    [intern@intern-a1t-inf-lnx1 ~]$ strings binary_data.bin | grep -i usage -A30
    Usage: %s [OPTION]... [-T] SOURCE DEST
      or:  %s [OPTION]... SOURCE... DIRECTORY
      or:  %s [OPTION]... -t DIRECTORY SOURCE...
    Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
    Mandatory arguments to long options are mandatory for short options too.
      -a, --archive                same as -dR --preserve=all
          --attributes-only        don't copy the file data, just the attributes
          --backup[=CONTROL]       make a backup of each existing destination file
      -b                           like --backup but does not accept an argument
          --copy-contents          copy contents of special files when recursive
      -d                           same as --no-dereference --preserve=links
      -f, --force                  if an existing destination file cannot be
                                     opened, remove it and try again (this option
                                     is ignored when the -n option is also used)
      -i, --interactive            prompt before overwrite (overrides a previous -n
                                      option)
      -H                           follow command-line symbolic links in SOURCE
      -l, --link                   hard link files instead of copying
      -L, --dereference            always follow symbolic links in SOURCE
      -n, --no-clobber             do not overwrite an existing file (overrides
                                     a previous -i option)
      -P, --no-dereference         never follow symbolic links in SOURCE
      -p                           same as --preserve=mode,ownership,timestamps
          --preserve[=ATTR_LIST]   preserve the specified attributes (default:
                                     mode,ownership,timestamps), if possible
                                     additional attributes: context, links, xattr,
                                     all
      -c                           deprecated, same as --preserve=context
          --no-preserve=ATTR_LIST  don't preserve the specified attributes
          --parents                use full source file name under DIRECTORY
      -R, -r, --recursive          copy directories recursively
    

Conclusion

In this lab exercise, you've practiced text manipulation in Linux using the grep and strings commands. You've learned how to search for text patterns in text files, perform recursive searches within directories, and extract readable text from binary files. These skills are valuable for tasks such as log analysis, data extraction, and examining binary files in a Linux environment.