Linux input and output redirection and file lookup values grep command

Source: Internet
Author: User

Linux input and output redirection and file lookup values grep command

One, the file descriptor
The Linux shell command, which can refer to some files by file descriptor, is usually used to 0,1,2 the file descriptor. Linux systems actually have 12 file descriptors, usually using a file description such as

File File descriptor
Input file-Standard input 0 (default is keyboard, 0 o'clock is the output of file or other command)
Output file-standard output 1 (default is screen, 1 o'clock is file)
Error output file-standard error 2 (default is screen, 2 o'clock is file)


Second, file redirection: Change the program running input and output, that is, the input source and output location, the specific explanation is as follows
Output redirection:

Command > FileName REDIRECT standard output to a new file when filename does not exist
Command >> filename REDIRECT standard output to a file (append)
Command > FileName REDIRECT standard output to a file
Command > FileName 2>&1 redirect standard output and errors together into a file
Command 2 > FileName redirect standard errors to a file
Command 2 >> filename REDIRECT standard output to a file (append)
Command >> filename2>&1 redirect standard output and errors together to a file (append)

Enter redirection:

Command < filename > filename2 command commands use the filename file as the standard input, with the filename2 file as the standard output
Command < filename command commands to use the filename file as the standard input
Command << Delimiter Read in from standard input, know to encounter delimiter delimiter

Binding redirection:

Command >&m redirect standard output to file descriptor M
Command < &- Turn off standard input
Command 0>&- Ditto


Iii. Some advanced usage in the shell
1: Output all standard errors to a black hole in Linux, known as an empty device (/dev/null)
[Email protected] ~]# ls/home/ins 2>/dev/null
[Email protected] ~]#

2: REDIRECT standard output and standard error execution to Out.put
[Email protected] ~]# Ls/home/ind >out.put 2>&1
[email protected] ~]# cat Out.put
Ls:cannot access/home/ind:no such file or directory
[Email protected] ~]#

3: Write "Hello World" in the A.txt file using standard input
[email protected] ~]# cat >a.txt<<eof
> HELLO World!
> EOF
[email protected] ~]# cat A.txt
HELLO World!
4: New user awk, set password using standard input 123456
[Email protected] ~]# Useradd awk
[Email protected] ~]#
[Email protected] ~]# echo 123456 | passwd--stdin awk
Changing password for user awk.
Passwd:all authentication tokens updated successfully.
[Email protected] ~]# Su-inds

Iv. grep command
grep is a powerful text-search tool in Linux that can use regular expressions to search for text and print matching lines. The grep full name is global Regular expression Print, which represents the globally regular expression version, and its use rights are for all users.
[Options] Main parameters:
-C: Outputs only the count of matching rows.
-I: Case insensitive (only for single-character).
-H: The file name is not displayed when querying multiple files.
-L: Only file names that contain matching characters are output when querying multiple files.
-N: Displays matching lines and line numbers.
-S: does not display error messages that do not exist or have no matching text.
-V: Displays all lines that do not contain matching text.
Pattern Regular Expression Main parameters:
\: Ignores the original meaning of special characters in regular expressions.
^: matches the start line of the regular expression.
$: Matches the end line of the regular expression.
\&lt: Starts from the line that matches the regular expression.
\&gt: End of line to match regular expression.
[]: A single character, such as [a], a meets the requirements.
[-]: range, such as [A-z], i.e. A, B, C to Z all meet the requirements.
。 : all the individual characters.
*: There are characters, the length can be 0.

Simple example Description
1. Filter all lines in the/etc/passwd file that have shutdown endings
[Email protected] ~]# grep shutdown$/etc/passwd
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
2, </eof
Search in/etc/passwdThe line where awk is located, and gives the awk a color, along with the line number
[Email protected] ~]# grep-n awk--color=auto/etc/passwd
45:awk:x:1158:1001::/home/awk:/bin/bash
[Email protected] ~]#

GREP uses concise and regular expressions http://www.linuxidc.com/Linux/2013-08/88534.htm

Basic application of Shell Programming--grep command under Linux http://www.linuxidc.com/Linux/2013-06/85525.htm

grep command details and related cases http://www.linuxidc.com/Linux/2014-07/104041.htm

The Linux Base command grep detailed http://www.linuxidc.com/Linux/2013-07/87919.htm

Set grep highlighting matches http://www.linuxidc.com/Linux/2014-09/106871.htm

Linux grep command Learning and summarizing http://www.linuxidc.com/Linux/2014-10/108112.htm

example of a 14 grep command http://www.linuxidc.com/Linux/2015-05/117626.htm

This article permanently updates the link address : http://www.linuxidc.com/Linux/2016-04/

Linux input and output redirection and file lookup values grep command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.