成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

在 Linux 中使用 cat 命令

系統 Linux
??cat?? 命令用于打印文本文件的文件內容。至少,大多數 Linux 用戶都是這么做的,而且沒有什么問題。

cat 命令的用途不僅僅是顯示文件內容。

cat 命令用于打印文本文件的文件內容。至少,大多數 Linux 用戶都是這么做的,而且沒有什么問題。

cat 實際上代表 “連接concatenate”,創建它是為了 合并文本文件。但只要有一個參數,它就會打印文件內容。因此,它是用戶在終端中讀取文件而無需任何其他選項的首選。

在 Linux 中使用 cat 命令

要使用 cat 命令,你必須遵循給定的命令語法:

cat [options] Filename(s)

這里:

  • [options] 用于修改 cat 命令的默認行為,例如使用 -n 選項獲取每行的數字。
  • Filename 是你輸入要使用的文件的文件名的位置。

為了簡單起見,我將在本指南中使用名為 Haruki.txt 的文本文件,其中包含以下文本行:

Hear the Wind Sing (1979)
Pinball, 1973 (1980)
A Wild Sheep Chase (1982)
Hard-Boiled Wonderland and the End of the World (1985)
Norwegian Wood (1987)
Dance Dance Dance (1990)
South of the Border, West of the Sun (1992)
The Wind-Up Bird Chronicle (1994)
Sputnik Sweetheart (1999)
Kafka on the Shore (2002)
After Dark (2004)
1Q84 (2009-2010)
Colorless Tsukuru Tazaki and His Years of Pilgrimage (2013)
Men Without Women (2014)
Killing Commendatore (2017)

那么,在沒有任何選項的情況下使用時,輸出會是什么? 好吧,讓我們看一下:

cat Haruki.txt

use cat command in Linuxuse cat command in Linux

正如你所看到的,它打印了整個文本文件!

但你可以做的遠不止這些。讓我向你展示一些實際例子。

1、創建新文件

大多數 Linux 用戶使用 touch 命令來 創建新文件,但使用 cat 命令也可以完成相同的操作!

在這種場景下,cat 命令比 touch 命令有一個優勢,因為你可以在創建文件時向文件添加文本。聽起來很酷。不是嗎?

為此,你需要使用 cat 命令,將文件名附加到 > 后面,如下所示:

cat > Filename

例如,在這里,我創建了一個名為 NewFile.txt 的文件:

cat > NewFile.txt

當你這樣做了,就會有一個閃爍的光標要求你寫一些東西,最后,你可以使用 Ctrl + d 來保存更改。

如果你想創建一個空文件,則只需按 Ctrl + d 而不進行任何更改。

Using cat commandUsing cat command

這就好了!現在,你可以使用 ls 命令來顯示 當前工作目錄的內容

use the ls command to list the contents of the current working directoryuse the ls command to list the contents of the current working directory

2、將文件內容復制到另一個文件

考慮一個場景,你要將 FileA 的文件內容重定向到 FileB

當然,你可以復制和粘貼。但是如果有幾百或幾千行怎么辦?

簡單。你可以使用 cat 命令來重定向數據流。為此,你必須遵循給定的命令語法:

cat FileA > FileB

?? 如果使用上述語法重定向文件內容,它將刪除 FileB 的文件內容,然后重定向 FileA 的文件內容。

例如,我將使用兩個文本文件 FileA 和 FileB,其中包含以下內容:

check the file contents using the cat commandcheck the file contents using the cat command

現在,如果我使用從 FileA 到 FileB 的重定向,它將刪除 FileB 的數據,然后重定向 FileA 的數據:

cat FileA > FileB

redirect the file content using the cat commandredirect the file content using the cat command

同樣,你可以對多個文件執行相同的操作:

cat FileA FileB > FileC

redirect file content of multiple files using the cat commandredirect file content of multiple files using the cat command

可以看到,上面的命令刪除了 FileC 的數據,然后重定向了 FileA 和 FileB 的數據。

3、將一個文件的內容附加到另一個文件

有時你想要將數據附加到現有數據,在這種情況下,你必須使用 >> 而不是單個 >

cat FileA >> FileB

例如,在這里,我將把兩個文件 FileA 和 FileB 重定向到 FileC

cat FileA.txt FileB.txt >> FileC.txt

redirect file content without overriding using the cat commandredirect file content without overriding using the cat command

如你所見,它保留了 FileC.txt 的數據,并將數據附加在末尾。

?? 你可以使用 >> 向現有文件添加新行。使用 cat >> filename 并開始添加所需的文本,最后使用 Ctrl+D 保存更改。

4、顯示行數

你可能會遇到這樣的情況,你想查看行數,這可以使用 -n 選項來實現:

cat -n File

例如,在這里,我將 -n 選項與 Haruki.txt 一起使用:

get the number of the lines in the cat commandget the number of the lines in the cat command

5、刪除空行

在文本文檔中留下多個空白行? cat 命令將為你修復它!

為此,你所要做的就是使用 -s 標志。

但使用 -s 標志有一個缺點。你仍然留有一行空白:

remove blank lines with the cat commandremove blank lines with the cat command

正如你所看到的,它有效,但結果接近預期。

那么如何刪除所有空行呢? 通過管道將其傳遞給 grep 命令:

cat File | grep -v '^$'

這里,-v 標志將根據指定的模式過濾掉結果,'^$' 是匹配空行的正則表達式。

以下是我在 Haruki.txt 上使用它時的結果:

cat Haruki.txt | grep -v '^$'

remove all the blank lines in text files using the cat command piped with grep regular expressionremove all the blank lines in text files using the cat command piped with grep regular expression

當獲得完美的輸出,你可以將其重定向到文件以保存輸出:

cat Haruki.txt | grep -v '^$' > File

save output of cat command by redirectionsave output of cat command by redirection

這就是你到目前為止所學到的

以下是我在本教程中解釋的內容的快速摘要:

命令

描述

cat <Filename>

將文件內容打印到終端。

cat >File

創建一個新文件。

cat FileA > FileB

FileB 的文件內容將被 FileA 覆蓋。

cat FileA >> FileB

FileA 的文件內容將附加到 FileB 的末尾。

cat -n File

顯示行數,同時省略文件的文件內容。

cat File | more

將 cat 命令通過管道連接到 more 命令以處理大文件。請記住,它不能讓你向上滾動!

cat File | less

將 cat 命令通過管道傳輸到 less 命令,這與上面類似,但它允許你雙向滾動。

cat File | grep -v '^$'

從文件中刪除所有空行。

??? 練習時間

如果你學到了新東西,用不同的可能性來執行它是最好的記憶方式。

為此,你可以使用 cat 命令進行一些簡單的練習。它們將是超級基本的,就像 cat 一樣是最基本的命令之一

出于練習目的,你可以 使用 GitHub 上的文本文件

  • 如何使用 cat 命令創建空文件?
  • 將 cat 命令生成的輸出重定向到新文件 IF.txt
  • 能否將三個或更多文件輸入重定向到一個文件? 如果是,該如何做?
責任編輯:龐桂玉 來源: Linux中國
相關推薦

2023-07-04 16:36:03

Linuxcd 命令

2023-08-12 15:05:26

Linuxcp 命令

2010-06-24 11:16:17

Linux Cat命令詳解

2020-12-07 06:25:14

Linux Truncate 命令

2018-08-21 09:00:30

Linuxtop命令

2023-01-13 23:21:29

netcat命令Linux

2022-08-10 13:12:04

Linuxcat命令

2010-01-15 19:37:36

Linux命令

2010-01-05 16:49:34

2022-10-18 10:00:09

Linuxtcpdump命令

2018-11-05 13:50:44

Linux命令tcpdump

2010-06-24 14:08:25

Linux Cat命令

2013-05-14 10:13:06

WindowsLinux操作系統

2021-01-04 05:43:59

LinuxBasename命令

2022-10-25 09:07:28

Linuxxargs命令

2018-05-16 10:32:06

Linux命令find

2018-06-26 09:15:24

Linux命令history

2022-11-18 10:16:26

Linuxwc 命令

2010-06-24 14:12:20

Linux Cat命令

2021-04-16 11:18:56

LinuxlsusbUSB
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 蜜臀久久 | 欧美久久久 | av入口 | 国产成人精品免费 | 国产精品一区二区三区四区 | 久草在线 | 91久久国产综合久久 | 日韩一区二区在线视频 | 91精品久久久久久久久 | 狠狠操电影 | 91久色 | 免费黄色av | 国产免费黄网 | 久久成人av | 日韩欧美一区二区在线播放 | 久久久久久黄 | 久久精品久久久久久 | 欧美中文字幕一区 | 欧美一级片在线播放 | 中文字幕在线观看日韩 | 337p日本欧洲亚洲大胆 | 亚洲精品一 | 一本久久a久久精品亚洲 | 欧美电影免费观看 | 成人黄色av网站 | 中文字幕亚洲一区二区三区 | 9久久婷婷国产综合精品性色 | 女人天堂av | 精品一二区 | 亚洲国产视频一区二区 | 午夜影院视频 | 成人免费看黄 | 在线欧美 | 亚洲在线免费 | 最新国产福利在线 | 少妇一级淫片免费播放 | 亚洲精品成人网 | 亚洲欧美在线观看 | 国产精品婷婷 | 中文av在线播放 | 国产成人99久久亚洲综合精品 |