Kuk Linux 2023
Q4(a) :- What is use of sed in linux and write commands to delete all lines in file and replace a Particular word in that file.
The Use of sed in Linux
sed (Stream Editor) is a powerful and versatile command-line utility used in Unix-like operating systems for parsing and transforming text. It operates on text streams (such as files or input from pipelines), allowing for non-interactive editing. sed is widely used in shell scripting and for performing batch processing tasks on text data.
Key Features of sed:
- Text Substitution: Replace text patterns using regular expressions.
- Line Deletion: Delete specific lines or ranges of lines.
- Text Insertion: Insert text at specific locations.
- Text Extraction: Extract portions of text based on patterns.
- In-place Editing: Modify files directly without the need for intermediate files.
- Efficient and Lightweight: Performs tasks efficiently, even on large files.
Commands to Delete All Lines in a File and Replace a Particular Word
1. Deleting All Lines in a File
To delete all lines in a file using sed, you can use the following command:
Explanation:
-i: Edits the file in-place, making changes directly to the file.'d': Deletes all lines in the file.
Example:
- This command will delete all lines in
example.txt, leaving it empty.
2. Replacing a Particular Word in a File
To replace a specific word in a file with another word using sed, you can use the substitution command s/pattern/replacement/g.
Command
Explanation:
-i: Edits the file in-place.'s/oldword/newword/g': Substitutes all occurrences ofoldwordwithnewwordglobally (on each line of the file).
Example:
- This command replaces every occurrence of the word “hello” with “world” in
example.txt.
Q4(b) :- What do you mean by filter in linux and write commands for
(i) Case insensitive search.
(ii) Displaying the count of number of matches.
(iii) Show line number while displaying the output.
Filters in Linux
In Unix-like operating systems, a filter is a program that processes an input stream of data and produces an output stream. Filters are commonly used in pipelines, where the output of one command is passed as input to another command, allowing for complex data processing workflows. Some typical examples of filters include grep, sed, awk, sort, uniq, cut, and tr.
Filters are essential in shell scripting and command-line operations because they allow users to manipulate and transform data efficiently. Each filter command performs a specific task, such as searching, sorting, or modifying text, and they can be combined to achieve more complex operations.
Commands for Specific Tasks
(i) Case Insensitive Search
To perform a case-insensitive search using grep, you use the -i option. This option tells grep to ignore case distinctions in both the pattern and the input data.
Command:
Example:
grep -i "hello" example.txt- This command searches for the word “hello” in
example.txt, ignoring case distinctions. It will match “hello”, “Hello”, “HELLO”, etc.
(ii) Displaying the Count of Number of Matches
To display the count of the number of matches, you use the -c option with grep. This option tells grep to count the number of lines that match the given pattern.
Command:
Example:
grep -c “hello” example.txt
- This command counts the number of lines that contain the word “hello” in
example.txt.
(iii) Show Line Number While Displaying the Output
To show line numbers along with the matching lines, you use the -n option with grep. This option tells grep to prefix each line of output with its line number in the input file.
Command
grep -n “pattern” filename
Example:
grep -n “hello” example.txt
This command searches for the word “hello” in example.txt and prefixes each matching line with its line number
