日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

(--3198)2: Redirecting From A File( Piping and redirecting output

發布時間:2025/1/21 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (--3198)2: Redirecting From A File( Piping and redirecting output 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

總結

以上是生活随笔為你收集整理的(--3198)2: Redirecting From A File( Piping and redirecting output的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。