0% found this document useful (0 votes)
22 views8 pages

Linux Filter Commands Explained

The document describes an experiment aimed at learning various filter commands in the Linux operating system. It lists and provides brief explanations of common Linux commands like grep, cat, comm, diff, find, sort, tail, head, cut, paste, uniq, tr, sed, and tee; and provides examples of using each command to filter, compare, edit, and manipulate file contents.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

Linux Filter Commands Explained

The document describes an experiment aimed at learning various filter commands in the Linux operating system. It lists and provides brief explanations of common Linux commands like grep, cat, comm, diff, find, sort, tail, head, cut, paste, uniq, tr, sed, and tee; and provides examples of using each command to filter, compare, edit, and manipulate file contents.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Scripting Language Lab

Experiment 2
Debdatta Praharaj
21MVD1027

AIM: To learn the filter commands


in the Linux operating system.

• Grep : The grep filter searches a


file for a particular pattern of
characters, and displays all lines
that contain that pattern

• Cat > [Link] add contents


• cat > [Link] add contents

• comm: compare two sorted files line by line and


write to standard output

• Diff : diff [Link] [Link]


a - Add the lines., c - Change the lines., d - Delete the
lines
• Find . -name [Link] : it searches [Link] in scripts
directory
• Find . name *.txt It will give all files which have ‘.txt’
at the end.
• Find . -size +1000c ##searches and displays files with size
greater than 1000bytes.
• Find . -atime -l : searches and displays all files which were
accessed in less than a day.
• Find . -mtime +1 : searches and displays all the files which
were modified 1 day ago.

• Sort : sort command sorts the contents of a file, in


numeric or alphabetic order, and prints the results to
standard output.
• Tail [Link]
• Tail -3 [Link] : displays the last three lines
• Tail +2 [Link] : moves tail part up by 2

• head [Link]
• head [Link] : displays 10 lines from head part of the file
• head -3 [Link] : displays first 5 lines from head part of
file
• Cut -c2-2 [Link] : cuts the characters before 2 nd
position and after 5 th position of content of file

• Paste –d: uses a single delimiter to separate contents of


different files.
• Paste -s : displays contents of each file in each column
separated by a space.

• Uniq: searches the data and filters the data by eliminating


duplicate content present in file.
• Tr : used to transform the data in the specified file


• Sed: stream editor. Used to edit (searching, find and
replace, insertion, deletion) the data.

• Tee : Takes the content from one file and writes output to
multiple files.
• Tac [Link] | tee [Link] : displays the content in
reverse order by creating a new file [Link]

Common questions

Powered by AI

The 'paste' command merges lines from multiple files horizontally, placing them side by side in columns, rather than sequentially as concatenation tools do. Using options such as '-d' for specifying delimiters and '-s' for serial output, 'paste' allows for controlled formatting of combined data, which is beneficial for organizing logs or report data in a tabular format . In contrast, traditional concatenation commands like 'cat' simply append file contents, one after the other, without additional structuring.

The 'find' command in Linux can differentiate searches by using specific options. For searching files by name, the '-name' option is employed, as in 'find . -name test2.txt', which searches for files named 'test2.txt' starting from the current directory. This type of search is useful for quickly locating files with specific names within a directory structure . In contrast, searching by size uses the '-size' option, with a specific size parameter, such as '+1000c' to find files larger than 1000 bytes, which is helpful for file management tasks like identifying large files for archival or deletion .

The 'uniq' command removes adjacent duplicate lines in a file and is often used in combination with the 'sort' command, which organizes file content into a specified order. By sorting a file's contents first, 'uniq' can then effectively eliminate any duplicate entries, producing a list of unique lines. This combination is powerful for data cleaning tasks, particularly when dealing with large datasets where duplicates might not be immediately adjacent .

The 'tac' command is used to reverse the order of lines in a file. When combined with the 'tee' command, it can output this reversed data to multiple files simultaneously. This combination allows for quickly generating alternative representations of data, such as reversed logs, while maintaining a duplicate for regular processing. This flexibility is useful in scenarios where reverse-ordered data provides a beneficial perspective or analytical angle .

The 'tee' command reads from standard input and simultaneously writes to standard output and one or more files, which is beneficial for logging or data monitoring without disrupting the data flow . It provides real-time data duplication, allowing for immediate file creation and review. However, the command's limitation lies in its potential to overwrite files if used without caution, leading to data loss. Careful management of its write operations is necessary to ensure data integrity.

The 'cut' command is used to extract specific sections of each line in a file, whereas the 'head' command displays the beginning segment of a file. Combining 'cut' and 'head' enhances data extraction processes by allowing precise focus on both the horizontal (columns) and vertical (lines) axes of text data. For instance, 'cut' can extract a specific column of data from the top lines returned by 'head', providing a streamlined view of key data points for analysis .

The 'tr' command is utilized to transform characters in text data by translating or deleting characters from standard input. A common use case is converting text to uppercase ('tr a-z A-Z') or replacing specific characters, such as changing delimiter formats in datasets (replacing commas with tabs). This is particularly effective when preparing data for input into other programs that require specific formats.

The 'diff' command is used to compare two files line-by-line, highlighting the differences between them. The output uses specific symbols to denote the type of difference: 'a' indicates lines that need to be added to one of the files to make them identical, 'c' represents lines that need to be changed, and 'd' shows lines that should be deleted from one file . This detailed feedback facilitates understanding of exactly how two files differ and supports effective file version management and merging efforts.

The 'sed' (stream editor) command in Linux provides robust text processing capabilities. Its primary functions include searching for and replacing text, inserting and deleting lines, and implementing complex transformations on stream data. 'sed' operates on each line of input, applying specified patterns to edit textual data programmatically, which is especially useful for automating the processing and transformation of large datasets or configuration files .

The 'grep' command is primarily used to search for specific patterns within files, displaying all lines that contain a specified string of characters. Practical applications include filtering log files for error messages, searching through codebases for specific function calls or variable names, and quickly extracting data from large datasets based on patterns . By filtering out non-relevant data, 'grep' aids in efficient data management and analysis.

You might also like