Linux Sysadmin Basics 4.1 -- Filtering Output and Finding Things (&&, cut, sort, uniq, wc, grep)

tutoriaLinux
22 Mar 201408:28

Summary

TLDRThis video tutorial delves into advanced shell usage in Linux, focusing on piping and redirection. It explains how to chain commands using pipes to redirect output from one program as input to another. The video also introduces the logical AND operator for conditional command execution. Additionally, it covers variables, data filtering with cut, sort, and grep, and their practical applications for tasks like pattern matching and text processing. The tutorial is designed to help users perform complex shell tasks efficiently.

Takeaways

  • 🔗 Piping is a method to redirect the output of one program as the input to another program.
  • 📄 Output redirection can be done using '>' for creating/overwriting a file or '>>' for appending to a file.
  • 🔄 Input redirection can be used to feed program input from a file instead of standard input.
  • 🤝 The logical AND operator (&&) in the shell allows you to run a second command only if the first command succeeds.
  • 📝 Variables in shell scripting can be used to store data for later use in your scripts.
  • 🔍 Filtering data is a common use for piping, allowing you to process and refine data streams.
  • 📑 The 'cut' command can be used to divide output based on a delimiter and select specific fields.
  • 🔑 The 'sort' command is useful for organizing data alphabetically, with options to ignore case and leading whitespace.
  • 🌐 'uniq' can be used to filter out duplicate lines from a data stream or file.
  • 🔎 'grep' is a powerful command for searching and filtering text data based on patterns.
  • 📁 You can chain commands using pipes to perform complex text processing tasks, such as searching for unique instances of a pattern across multiple files.

Q & A

  • What is piping in the context of Linux shell?

    -Piping is a technique that allows the output of one program to be used as input for another program. It is done using the pipe symbol (|), where the output of the first program is redirected to the input of the second program.

  • How does output redirection work in Linux shell?

    -Output redirection is achieved using the '>' operator to overwrite or create a file with the output, and the '>>' operator to append output to an existing file. This allows you to control where the output of a program is sent.

  • What is input redirection and how is it used?

    -Input redirection is used to provide input to a program from a file instead of the standard input (keyboard). It is done using the '<' operator, where the file's content is used as input for the program.

  • What is the logical AND operator in the shell and how does it work?

    -The logical AND operator in the shell is represented by '&&'. It is used to chain commands, where the second command will only execute if the first command succeeds (returns a success status).

  • Why is the logical AND operator useful when working with files?

    -The logical AND operator is useful when working with files because it allows you to perform actions conditionally. For example, you can write to a file and then only perform further operations on that file if the write operation was successful.

  • What is the purpose of the 'cut' command in Linux?

    -The 'cut' command is used to cut out sections of each line of files and write the result to standard output. It can be used to extract specific fields from text, typically separated by a delimiter.

  • How does the 'sort' command help with organizing data?

    -The 'sort' command is used to sort lines of text alphabetically or numerically. It can be customized to ignore leading whitespace, case, and other sorting options, making it a powerful tool for organizing data.

  • What is the 'unique' command and when would you use it?

    -The 'unique' command filters out repeated lines in a file. It is used when you want to display only unique lines from a file or a stream of data.

  • What is the 'grep' command and what can it do?

    -The 'grep' command is a search utility that allows you to search for specific patterns in files. It can perform pattern matching, search for lines that start or end with a pattern, and even show lines before or after a match.

  • How can you use 'grep' to find files containing a specific word?

    -You can use 'grep' with the '-r' option to search recursively through directories for files containing a specific word. The command will output the filename and the line containing the match.

  • What is meant by 'globbing' in the context of the shell?

    -Globbing refers to the use of wildcard characters to match file names. For example, an asterisk (*) can be used to match any number of characters, allowing you to perform operations on multiple files that match a pattern.

Outlines

00:00

🔗 Piping and Redirection in Linux Shell

This paragraph introduces the concept of piping and redirection in Linux shell. Piping is the process of taking the output of one program and using it as the input for another. The script explains how to chain multiple programs together using pipes, where the output of one program feeds into the next. It also covers output redirection using the '>' operator to create or overwrite files and '>>' to append to files. Additionally, it touches on input redirection to provide input to programs from files. The paragraph emphasizes the logical AND operator (&&), which allows conditional execution of commands based on the success of the previous command. An example is given where 'ls' is used to list a file, and if successful, 'echo' prints a success message. The paragraph also briefly introduces the use of 'cut' for filtering data based on delimiters, setting the stage for more complex shell tasks.

05:06

🔍 Advanced Text Processing with Shell Commands

The second paragraph delves into advanced text processing commands in the Linux shell, such as 'sort', 'uniq', 'wc', and 'grep'. It explains how 'sort' can be used to order lines alphabetically while ignoring leading whitespace and case. 'uniq' is introduced as a command to filter out duplicate lines from input. 'wc' is mentioned for counting lines, words, and characters. The paragraph then focuses on 'grep', a powerful command for searching and filtering text based on patterns. It can be used to display lines that match a certain pattern or to search for specific text within files. The script provides examples of using 'grep' to find lines starting with 'user' and to search for the word 'someone' across all files in a directory. The use of 'grep' in combination with 'uniq' and 'cut' to extract unique filenames containing a specific string is also demonstrated. This section of the script aims to equip viewers with tools for complex data manipulation and searching within the shell environment.

Mindmap

Keywords

💡Piping

Piping, often represented by the '|' symbol, is a method used in the Linux shell to redirect the output of one command to the input of another. This allows for the chaining of commands where the output of one directly feeds into the next. In the video, piping is discussed as a way to take the output from one program and use it as the input for another, creating a powerful workflow for processing data.

💡Output Redirection

Output redirection is the process of sending the output of a command to a file instead of the terminal. This is achieved using the '>' operator to overwrite the file or '>>' to append to it. The video explains how this can be used to control where the results of commands are saved, which is crucial for further processing or record-keeping.

💡Input Redirection

Input redirection allows a program to take its input from a file rather than standard input (like a keyboard). This is done using the '<' operator. The video touches on this concept as a way to feed data from a file into a program, which is useful for automating tasks and handling large datasets.

💡Logical AND Operator

The logical AND operator, represented by '&&' in the shell, is used to chain commands where the second command will only execute if the first command succeeds (exits with a status of 0). This is important for conditional execution of commands, ensuring that subsequent commands only run when certain conditions are met.

💡Variables

Variables in the shell are used to store values that can be used in scripts and commands. They are a fundamental part of programming and automation, allowing for dynamic and reusable code. The video hints at their use in the context of shell scripting and automation.

💡Filtering

Filtering refers to the process of selecting specific data from a larger set based on certain criteria. In the context of the shell, this is often done with commands like 'grep', 'cut', 'sort', etc., to process and analyze text data.

💡Cut

The 'cut' command is used to remove sections from each line of input and write the result to standard output. It can be used with various delimiters to extract specific fields from structured text, such as columns in a CSV file.

💡Sort

The 'sort' command is used to sort lines of text alphabetically or numerically. It can be customized with various options to ignore case, leading whitespace, and more. This is essential for organizing data and preparing it for further analysis.

💡Grep

Grep is a powerful command-line utility for searching text using patterns. It can filter lines that match a specific pattern, making it invaluable for quickly finding relevant information in large datasets.

💡Unique

The 'uniq' command is used to filter out repeated lines in a file or input stream, only printing unique lines. This is useful when dealing with sorted data to remove duplicates and present a clean, distinct list.

💡Word Count

The 'wc' command is used to count the number of lines, words, and bytes in input. It's a simple yet powerful tool for quickly getting statistics about text data.

Highlights

Piping is explained as taking the output of one program and redirecting it to the input of another.

Output redirection is covered with create, append, or overwrite operators.

Input redirection is mentioned for taking program input from files.

Logical AND operator in the shell is introduced to chain commands based on the success of the previous command.

The importance of logical AND for error handling in command chaining is emphasized.

Variables and coding are briefly mentioned as part of shell scripting.

Filtering data using pipes is highlighted as a common use case.

The 'cut' command is introduced for dividing data into fields using a delimiter.

The 'sort' command is explained for ordering data, with options to ignore leading whitespace and case.

The 'unique' command is shown to print only unique lines from input.

The 'word count' command is briefly mentioned for counting lines, words, and characters.

The 'grep' command is introduced as a powerful tool for searching and filtering text.

Grep's ability to perform pattern matching and display lines before and after a match is discussed.

An example is given on how to use grep to find lines starting with 'user'.

Grep is also shown to be useful for searching across files in a directory.

The combination of grep, unique, and cut commands is demonstrated to find unique filenames containing a specific string.

Globbing characters are mentioned as a feature to look out for in future discussions.

The transcript concludes by emphasizing the setup for more complex shell tasks using pipes.

Transcripts

play00:00

welcome to part two of using the shell

play00:02

in Linux so in the last video we covered

play00:04

piping and basic input and output

play00:08

redirection now piping is taking some

play00:13

program and taking its output

play00:15

redirecting it to the input of program

play00:18

two and we can keep doing that so this

play00:21

would take program one's output feed it

play00:24

to program to take that output feed it

play00:26

as the input to program so on and so

play00:28

forth in this video we're just going to

play00:31

expand that a little bit we also covered

play00:34

output redirection by using the create

play00:37

and append or just create an overwrite

play00:39

operators and the input redirection so

play00:42

take your input so program take your

play00:45

input from some files if you need help

play00:50

with that you want to review watch the

play00:52

last video again or just read some

play00:53

documentation good so now we're at a

play00:56

point where we can cover I just want to

play00:58

cover one quick thing and that is the

play01:00

logical and operator on the shell and

play01:03

then I want to talk a little bit about

play01:06

variables and coding and some filtering

play01:09

that you might want to do which is very

play01:11

useful for gluing together different

play01:13

programs with pipes to do some filtering

play01:15

on data it's a very common use for

play01:18

piping good to start with if we take

play01:24

program and the and operator that's two

play01:29

ampersands and then program two what

play01:31

this does is very simple this thing

play01:33

looks at the thing before it and says is

play01:36

this true that is did this run

play01:39

successfully if the answer is yes it

play01:42

runs the next thing if the answer is no

play01:45

it does not run the next thing it simply

play01:47

exits so if program one works program

play01:53

two will run also if program one fails

play01:56

program two will not run this is useful

play01:58

for things like if the first program

play02:00

that you call the first binary that you

play02:02

call does something like do some stuff

play02:06

and then write to a file if it was

play02:07

successful and then program to you does

play02:09

something with that file

play02:10

you know if program one wasn't

play02:12

successful the file never was

play02:13

then running some command on that

play02:15

non-existent file is silly irrelevant

play02:18

and will only lead to errors so this is

play02:21

one common use for this construct so for

play02:25

example if we use lists we've got the

play02:28

file here where am i if we list this

play02:33

file on the desktop it says oh great the

play02:36

file is file dot txt sort of obvious if

play02:40

we say list that file and if you're

play02:43

successful

play02:44

please echo back to us a astonishing

play02:50

success can't spell we'll get back the

play02:58

name of the file and astonishing success

play03:00

because it runs the first command that's

play03:02

successful and if the freshman

play03:05

successful it runs the second one which

play03:06

is echoing astonishing success if I

play03:09

mistyped this or the file doesn't exist

play03:11

or give it some Clovis data astonishing

play03:15

success does not get echoed back to us

play03:17

because the first program exits with an

play03:20

error and says cannot access that file

play03:23

it doesn't exist and so the second part

play03:26

never runs this is just useful for when

play03:29

you're training things together useful

play03:34

things for example if I've got a file

play03:42

that has some pattern in it like for

play03:45

example fields delimited by this little

play03:50

colon character we could do something

play03:53

like we'll take the file let's say

play03:58

that's the output that we get from

play03:59

somewhere and we could cut using a

play04:02

delimiter this use field so what this is

play04:09

going to do is the cut commands takes

play04:11

the output of the first command through

play04:13

the pipe looks for the delimiter colon

play04:19

divides it on that into fields one and

play04:22

two and we're selecting field two so

play04:26

we get this back another useful one is

play04:29

sort ignore the leading whitespace

play04:37

case-insensitive

play04:42

you can see did an alphabetical sort by

play04:48

first letter if you interested in sort

play04:51

it's really useful commands learn this

play04:54

learn cut and sort there are the things

play04:58

like let's say if we say there's a

play05:05

duplicate in here and this is the output

play05:11

of some file has duplicates you can also

play05:13

say unique that will only print unique

play05:16

lines as opposed to everything word

play05:21

count is useful because lines characters

play05:33

also something you might use and then

play05:41

the real heavy weight which is grep grep

play05:46

is a searching finding filtering tool

play05:49

and it can do things like pattern

play05:51

matching and other cool things grep is

play05:53

an amazing command it's large I would

play05:55

invest some time in learning it it's got

play05:57

a lot of features some features you use

play06:00

a little bit some you'll use all the

play06:02

time like find something and then show

play06:04

me five lines before and ten lines after

play06:06

it as well that kind of thing but at its

play06:10

most simple and I suggest you start at

play06:12

its most simple it basically just

play06:16

returns a line that matches what you're

play06:18

looking for so if you have some output

play06:20

and you want to filter it down to let's

play06:22

say just lines start with user you can

play06:24

do that

play06:25

and where's our duplicate line you can

play06:28

see finds that twice you shall

play06:31

automatically highlight set in Ubuntu

play06:32

but you can also use it to search files

play06:35

so if you say

play06:39

it will do the same so if you're looking

play06:42

for something in the file system it can

play06:45

be cool now we've added two lines and if

play06:51

I for example grep for someone in this

play06:57

directory all the files in this

play07:00

directory you can see it shows me the

play07:03

file though it was founded and then the

play07:05

line that returns that pattern so for

play07:09

example if we wanted to see all the

play07:11

files that contain the word someone and

play07:14

then let's say you can see it was found

play07:19

twice in this file so we don't want to

play07:21

see the same file twice we could do

play07:23

something like grab someone in there in

play07:27

all the files in this directory get it

play07:31

unique first so this will just result in

play07:35

two things and then third we want to cut

play07:39

using a delimiter of the colon and

play07:42

select field one which would be the file

play07:46

name right this gives us unique file

play07:50

names that contain the string some one

play07:52

right so we're grabbing for someone in

play07:56

this directory all files this is a

play07:59

globbing character which we'll get into

play08:00

in a second looking only for unique

play08:03

things and then we're cutting that using

play08:07

the colon as the delimiter so here into

play08:13

some fields and we just want the very

play08:16

first field so the leftmost field so

play08:19

they go you have some idea of what it

play08:22

looks like to chain things to get a

play08:23

using pipe and that sets you up for some

play08:26

more complex shell tasks

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
Linux ShellScripting TipsPipingRedirectionVariablesFilteringData ProcessingCommand LineFile HandlingText ManipulationGrep
هل تحتاج إلى تلخيص باللغة الإنجليزية؟