1.Tenth Line
How would you print just the 10th line of a file?
For example, assume this file.txt has the following content:
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Your script should output the tenth line, which is:
Line 10
-------------------
# Read from the file file.txt and output the tenth line to stdout. #Solution One: #head-n-file.txt | Tail-n +10#solution: #awk ' nr==10 ' file.txt#solution three:sed-n 10p file.txt
Knowledge points involved:
Thehead is used to display the beginning of the file to the standard output, and the default head command prints the first 10 lines of its corresponding file.
Syntax format: head [parameters] ... [File] ...
Command parameters:
-Q Hide file names
-V Show file name
-c< bytes > Display bytes
-n< rows > rows displayed
TheTail command is used to display the content at the end of the specified file, and is processed as input when no file is specified. Common view log files.
Command parameters:
-F Loop Read
-Q does not display processing information
-V displays detailed processing information
Number of-c< > bytes displayed
-n< lines > Display rows
--pid=pid is shared with-F, which means that the process ends after the id,pid dies.
-Q,--quiet,--silent never output the header of the file name
-S,--sleep-interval=s is combined with-F, which means sleep s seconds at each repetition interval
Refer to: Tail of the Linux command I used-output file tail/dynamic Monitor file tail
awk is a powerful text analysis tool that is especially powerful when it comes to analyzing data and generating reports, compared to grep lookups and sed editing . To put it simply, awk reads the file line- by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut.
Syntax format:
awk ' {pattern + action} ' {filenames}
Pattern represents what AWK looks for in the data, and the action is a series of commands that are executed when a match is found
Refer to: Linux awk command details
Sed is an online editor that processes a single line of content at a time . When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file.
Syntax format:
sed [-hnv][-e<script>][-f<script file >][text file]
Shell of Leetcode