编程问答
(-凯发ag旗舰厅登录网址下载
2019独角兽企业重金招聘python工程师标准>>>
we've seen how to redirect from a command to a file. we can also redirect the other way, from a file to a command. this involves redirecting from thestandard output of the file to the standard inputof the command.
in our last screen, the file beer.txt ends up looking like this:
99 bottles of beer on the wall...
take one down, pass it around, 98 bottles of
beer on the wall...
the linux sort command will sort the lines of a file in alphabetical order. if we pass the -r flag, the lines will be sorted in reverse order.
sort < beer.txt
the above code will sort each of the lines inbeer.txt in order.
instructions
- use the sort command to sort the lines of beer.txt in reverse order
/home/dq$ sort -r < beer.txt
take one down,pass it around,98 bottles of beer on the wall...
00 bottles of beer on the wall....
###################################################################
3: the grep command
sometimes, we'll want to search through the contents of a set of files to find a specific line of text. we can use the grep command for this.
grep "pass" beer.txt
the above command will print any lines inbeer.txt where the string pass appears, and highlight the string pass.
we can specify multiple files by passing in more arguments:
grep "beer" beer.txt coffee.txt
this will show all lines from either file that contain the string beer.
instructions
-
make a file called coffee.txt that has two lines of text in it:
coffee is almost as good as beer, but i could never drink 99 bottles of it -
use the grep command to search beer.txt and coffee.txtfor the string bottles of
~$ echo 'coffee is almost as good as beer,\nbut i could never drink 99 bottles of it' > coffee.txt
~$ grep "bottles of" beer.txt coffee.txt
##########################################################################
4: special characters
like we did in the last screen, sometimes we'll want to execute commands on a set of files. there were only 2 files in the last screen though,beer.txt and coffee.txt. but what if we wanted to search through all 1000 files in a folder? we definitely wouldn't want to type out all of the names. let's say we have the following files in a directory:
beer.txt
beer1.txt
beer2.txt
coffee.txt
better_coffee.txt
if we wanted to search for a string in beer1.txtand beer2.txt, we could use this command:
grep "beer" beer1.txt beer2.txt
we could also use a wildcard character, ?. ? is used to represent a single, unknown character. we could perform the same search we did above like this:
grep "beer" beer?.txt
the wildcard above will match both beer1.txtand beer2.txt. we can use as many wildcards as we want in a filename.
instructions
- create empty files called beer1.txt and beer2.txt.
- use grep and the ? wildcard character to search for beer in both beer1.txt and beer2.txt
~$ touch beer1.txt
~$ touch beer2.txt
~$ grep "beer" beer?.txt
####################################################
5: the star wildcard
we learned about the ? wildcard character in the last screen, but there are also other wildcard characters. let's say we again have the following files in a directory:
beer.txt
beer1.txt
beer2.txt
coffee.txt
better_coffee.txt
we can use the * character to match any number of characters, including 0.
grep "beer" beer*.txt
the above command will search for the stringbeer in beer.txt, beer1.txt, andbeer2.txt. we can also use the wildcard to match more than 1 character:
grep "beer" *.txt
the above command will search for the stringbeer in any file that has a name ending in.txt.
we can use wildcards anytime we would otherwise enter a filename. for example:
ls *.txt
the above command will list any files with names ending in .txt in the current directory.
instructions
- use grep and the * wildcard character to search for beer in all the files ending in .txt in the home directory
~$ grep "beer" *.txt
转载于:https://my.oschina.net/bettyty/blog/747158
总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的(--3198)2: redirecting from a file( piping and redirecting output的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。
- 上一篇: yum与rpm的详解 --用来安装软件
- 下一篇: