0% found this document useful (0 votes)
23 views

Ch7 - Search and Text Manipulation

The document discusses searching and manipulating text using grep and sed commands in Linux. It provides examples of using grep to search files for patterns and sed to perform operations like substituting, deleting, and inserting lines.

Uploaded by

Sarah Amiri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Ch7 - Search and Text Manipulation

The document discusses searching and manipulating text using grep and sed commands in Linux. It provides examples of using grep to search files for patterns and sed to perform operations like substituting, deleting, and inserting lines.

Uploaded by

Sarah Amiri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

CHAPTER 7: SEARCH AND

TEXT MANIPULATION
IT403(Open Source Course)
TABLE OF CONTENTS:

1. Searching for Data: Grep 10. Editing Files in Place


2. How to Use Grep Command in Linux 11. The SED Addresses
3. Manipulating Text: Sed 12. The SED Addresses Types
4. Example- Search and Replace 13. The SED Substitution Flags
5. Example- Delete Lines
6. Example- Delete Multiple Lines
7. Example- Delete Specific
8. Example- Insert Lines
9. Example- Insert Lines(Specific)
1. SEARCHING FOR DATA: GREP

• The ‘grep’ command searches one or more text files, or stdin if no files are named, for lines
containing a match to a given pattern.
• The 'grep' command is used to search for specific words or patterns within one or more text files.
If you don't use any options, grep will show you all the lines in the file(s) that contain the word
or pattern you searched for.
• For example, let's say you have two files called "file1.txt" and "file2.txt" and you want to search
for the word "apple" in both files. You would type:
• If "apple" appears in a line in "file1.txt", and "file2.txt", grep will show you those lines, along with the name of the
file they came from:

• The name of the file and colon before each line helps you know which file the line came from.
• The syntax is: grep [options] pattern [file-list]

• Examples:
○ grep “three” file1
○ history | grep name
○ grep -r “eth0” /etc/*
2- HOW TO USE GREP COMMAND IN LINUX

• ‘-i’ : This option tells grep to perform a case-insensitive search. For example:

• This command would search for the word "apple" in the "fruits.txt" file, ignoring whether it
appears in uppercase or lowercase letters.
• -n : This option tells grep to show the line number of each match. For example:

• This command would search for the word "banana" in the "fruits.txt" file and display each line
where it appears, along with the line number of each match.
• -v : This option tells grep to invert the search, meaning it will show lines that don't match the
pattern. For example:

• This command would search for lines in the "fruits.txt" file that don't contain the word "apple"
and display them.
• -r : This option tells grep to search for the pattern recursively in all files and directories under a
given directory. For example:

• -w : This option tells grep to search for whole words that match the pattern, rather than just
substrings. For example:

• This command would search for the word "apple" in the "fruits.txt" file, but would not match
lines that contain the word "pineapple" or "applesauce".
• You can see a full list of options and their descriptions by typing man grep in your terminal.
• -f : This option allows you to specify a file that contains one or more patterns to search for in the
target files. The patterns should be separated by newlines. For example:
• Let's say we have a file called "pattern_file" that contains the following patterns:

• We can use this file to search for these patterns in other files by running the following command:

• This command will search for the patterns "aaa" and "bbb" in both "file1.txt" and "file2.txt".
• -e : This option allows you to specify multiple patterns to search for in the target files, separated
by the "or" operator (|). For example:

• This command will search for lines in "file.txt" that contain either "pattern1" or "pattern2".
• Alternatively, we can specify multiple patterns using a single -e option by separating them with
the | character, like this:

• This command will search for lines in "file.txt" that contain either "alice" or "bob".
• -l : This option will suppress the normal output of grep and instead print only the names of the files
that contain the specified pattern. For example:

• This command will search for the pattern "apple" in both "fruits.txt" and "fruits2.txt", but instead of
displaying the matching lines, it will only print the names of the files that contain the pattern.
• -L : This option is similar to -l, but it will print only the names of the files that do not contain the
specified pattern. For example:

• This command will search for the pattern "apple" in both "fruits.txt" and "fruits2.txt", but instead of
displaying the matching lines, it will only print the names of the files that do not contain the pattern.
3- MANIPULATING TEXT: SED

• Sed is a tool that helps you change text in files or input that you give it. It can do things like:
• finding and replacing text
• adding or deleting lines
• changing the way the text looks

• You tell sed what changes to make by giving it a set of commands in a script.
• Sed reads the text line by line, applies the commands to each line, and gives you the changed
text as output. It's a useful tool for working with text files on a Unix/Linux system.
• sed [options] [script] [input-file]
1. -n (suppress output) : show only modified lines.
For example, if you want to show only the lines that contain the word "example" in a text file called "file.txt",
you can use the following command:

This will output only the lines that contain the word "example".
2. -i (edit files in place): modify the file.
For example, if you want to replace all instances of the word "example" with "sample" in a file called
"file.txt", you can use the following command:

This will replace all instances of the word "example" with "sample" in the file "file.txt".
3. -e (specify a script inline): allows you to specify a sed script inline instead of in a separate file.
For example, if you want to delete all lines that contain the word "example" in a file called
"file.txt", you can use the following command:

This will delete all lines that contain the word "example" in the file "file.txt".
4. -f (script file) : used to specify a script file that contains the sed commands.
Let's say you have a file called "script.sed" that contains the following sed commands:
To apply these commands to a file called "input.txt", you can use the following command:

This will apply the sed commands from the "script.sed" file to the "input.txt" file, replacing all
instances of "hello" with "hi" and "world" with "planet".

Note that you can use any file name for your sed script file, not just "script.sed". The important
thing is to use the -f option followed by the file name to specify the script file to sed.
• script: This is the sed script that contains the editinĀ commands to be applied to the input text.
• input-file: Name of the input file to be processed by sed. If no input file is specified, sed will
read from standard input.
For example, if you wanted to use sed to process the output of the ls command (which lists the files
in the current directory), you could use a pipeline to send the output of ls to sed:

This command uses ls to list the files in the current directory, and then pipes the output to sed. The
sed command uses a regular expression to replace the ".txt" file extension with ".md" for each line
of output from ls. Because we didn't specify an input file for sed, it reads the output of ls from the
standard input.
• Some ‘sed’ commands:
o p to print the line
For example, if you want to print the first line of a file called "file.txt", you can use the following
command:

This will print the first line of the file "file.txt".


o d command is used to delete the line in sed.
If you want to delete the first line instead of printing it, you can use the following command:
o (substitute) used to replace text in sed.

For example, if you want to replace the first instance of the word "example" with "sample" in a file
called "file.txt", you can use the following command:
This will replace the first instance of the word "example" with "sample" in the file "file.txt".
4- EXAMPLE: SEARCH & REPLACE
5- EXAMPLE: DELETE MULTIPLE LINES
6- DELETE SPECIFIC
• sed -e '2d' -e '/line 3/d’ applies two editing commands:
1. The first editing command, -e '2d', deletes the second line of the input. The 2 specifies the line
number to delete, and the d command deletes it.

2. The second editing command, -e '/line 3/d', deletes any line that contains the text "line 3". The /line 3/
is a regular expression that matches any line containing "line 3", and the d command deletes those
lines.
7- EXAMPLE: INSERT LINES
8- EXAMPLE: INSERT LINES(FILE)
9- EDITING FILES IN PLACE:

For example, consider a file named example.txt that contains the following lines:

If you run the following sed command with the -i option:


It will replace all occurrences of the word "line" with "LINE" in the example.txt file, and the
contents of the file will be updated to:

In summary, the -i option in sed allows you to edit files in place, without having to create a new file
with the edited text.
10- THE SED ADDRESSES

• In sed, addresses are used to specify particular locations or ranges in a file where a command
should be applied.
• If you don't specify any address, sed will perform its operation on every line in the file.
• Syntax is: addr1[,addr2] where addr1 and addr2 are either line numbers or regular expressions
that match lines.
• The line which addr1 matches will always be accepted, even iÿ addr2 selects an earlier line.
• If an exclamation mark (!) is inserted after the addresses, it specifies that the command should
only be executed if the address range does not match.
For example, consider the following input file named example.txt:

If you want to apply the same substitution command to lines 2 to 4, you can use the following sed
command:
This command tells sed to apply the substitution command to lines 2 to 4. The output will be:
12- THE SED ADDRESSES TYPES:
• first~step will match every step‘th line starting with line first.
○ sed ‘2~3d’ /etc/passwd (starts from second line , then every 3 lines)

For example, if we have a file /etc/passwd with the following contents:

And we want to delete every 3rd line starting from line 2, we can use the command:
• In this case, 2~3 is the address range, which means start from line 2 and then match every 3rd
line. Sed will match line 2 and then every 3rd line after that, which includes lines 5 and 8.
13- THE SUBSTITUTION FLAGS

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy