偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

在 Linux 中使用 cat 命令

系統(tǒng) Linux
??cat?? 命令用于打印文本文件的文件內(nèi)容。至少,大多數(shù) Linux 用戶都是這么做的,而且沒(méi)有什么問(wèn)題。

cat 命令的用途不僅僅是顯示文件內(nèi)容。

cat 命令用于打印文本文件的文件內(nèi)容。至少,大多數(shù) Linux 用戶都是這么做的,而且沒(méi)有什么問(wèn)題。

cat 實(shí)際上代表 “連接concatenate”,創(chuàng)建它是為了 合并文本文件。但只要有一個(gè)參數(shù),它就會(huì)打印文件內(nèi)容。因此,它是用戶在終端中讀取文件而無(wú)需任何其他選項(xiàng)的首選。

在 Linux 中使用 cat 命令

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

cat [options] Filename(s)

這里:

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

為了簡(jiǎn)單起見(jiàn),我將在本指南中使用名為 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)

那么,在沒(méi)有任何選項(xiàng)的情況下使用時(shí),輸出會(huì)是什么? 好吧,讓我們看一下:

cat Haruki.txt

use cat command in Linuxuse cat command in Linux

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

但你可以做的遠(yuǎn)不止這些。讓我向你展示一些實(shí)際例子。

1、創(chuàng)建新文件

大多數(shù) Linux 用戶使用 touch 命令來(lái) 創(chuàng)建新文件,但使用 cat 命令也可以完成相同的操作!

在這種場(chǎng)景下,cat 命令比 touch 命令有一個(gè)優(yōu)勢(shì),因?yàn)槟憧梢栽趧?chuàng)建文件時(shí)向文件添加文本。聽(tīng)起來(lái)很酷。不是嗎?

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

cat > Filename

例如,在這里,我創(chuàng)建了一個(gè)名為 NewFile.txt 的文件:

cat > NewFile.txt

當(dāng)你這樣做了,就會(huì)有一個(gè)閃爍的光標(biāo)要求你寫(xiě)一些東西,最后,你可以使用 Ctrl + d 來(lái)保存更改。

如果你想創(chuàng)建一個(gè)空文件,則只需按 Ctrl + d 而不進(jìn)行任何更改。

Using cat commandUsing cat command

這就好了!現(xiàn)在,你可以使用 ls 命令來(lái)顯示 當(dāng)前工作目錄的內(nèi)容

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、將文件內(nèi)容復(fù)制到另一個(gè)文件

考慮一個(gè)場(chǎng)景,你要將 FileA 的文件內(nèi)容重定向到 FileB。

當(dāng)然,你可以復(fù)制和粘貼。但是如果有幾百或幾千行怎么辦?

簡(jiǎn)單。你可以使用 cat 命令來(lái)重定向數(shù)據(jù)流。為此,你必須遵循給定的命令語(yǔ)法:

cat FileA > FileB

?? 如果使用上述語(yǔ)法重定向文件內(nèi)容,它將刪除 FileB 的文件內(nèi)容,然后重定向 FileA 的文件內(nèi)容。

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

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

現(xiàn)在,如果我使用從 FileA 到 FileB 的重定向,它將刪除 FileB 的數(shù)據(jù),然后重定向 FileA 的數(shù)據(jù):

cat FileA > FileB

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

同樣,你可以對(duì)多個(gè)文件執(zhí)行相同的操作:

cat FileA FileB > FileC

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

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

3、將一個(gè)文件的內(nèi)容附加到另一個(gè)文件

有時(shí)你想要將數(shù)據(jù)附加到現(xiàn)有數(shù)據(jù),在這種情況下,你必須使用 >> 而不是單個(gè) >

cat FileA >> FileB

例如,在這里,我將把兩個(gè)文件 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

如你所見(jiàn),它保留了 FileC.txt 的數(shù)據(jù),并將數(shù)據(jù)附加在末尾。

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

4、顯示行數(shù)

你可能會(huì)遇到這樣的情況,你想查看行數(shù),這可以使用 -n 選項(xiàng)來(lái)實(shí)現(xiàn):

cat -n File

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

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

5、刪除空行

在文本文檔中留下多個(gè)空白行? cat 命令將為你修復(fù)它!

為此,你所要做的就是使用 -s 標(biāo)志。

但使用 -s 標(biāo)志有一個(gè)缺點(diǎn)。你仍然留有一行空白:

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

正如你所看到的,它有效,但結(jié)果接近預(yù)期。

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

cat File | grep -v '^$'

這里,-v 標(biāo)志將根據(jù)指定的模式過(guò)濾掉結(jié)果,'^$' 是匹配空行的正則表達(dá)式。

以下是我在 Haruki.txt 上使用它時(shí)的結(jié)果:

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

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

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

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

這就是你到目前為止所學(xué)到的

以下是我在本教程中解釋的內(nèi)容的快速摘要:

命令

描述

cat <Filename>

將文件內(nèi)容打印到終端。

cat >File

創(chuàng)建一個(gè)新文件。

cat FileA > FileB

FileB 的文件內(nèi)容將被 FileA 覆蓋。

cat FileA >> FileB

FileA 的文件內(nèi)容將附加到 FileB 的末尾。

cat -n File

顯示行數(shù),同時(shí)省略文件的文件內(nèi)容。

cat File | more

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

cat File | less

將 cat 命令通過(guò)管道傳輸?shù)?nbsp;less 命令,這與上面類似,但它允許你雙向滾動(dòng)。

cat File | grep -v '^$'

從文件中刪除所有空行。

??? 練習(xí)時(shí)間

如果你學(xué)到了新東西,用不同的可能性來(lái)執(zhí)行它是最好的記憶方式。

為此,你可以使用 cat 命令進(jìn)行一些簡(jiǎn)單的練習(xí)。它們將是超級(jí)基本的,就像 cat 一樣是最基本的命令之一

出于練習(xí)目的,你可以 使用 GitHub 上的文本文件。

  • 如何使用 cat 命令創(chuàng)建空文件?
  • 將 cat 命令生成的輸出重定向到新文件 IF.txt
  • 能否將三個(gè)或更多文件輸入重定向到一個(gè)文件? 如果是,該如何做?
責(zé)任編輯:龐桂玉 來(lái)源: Linux中國(guó)
相關(guān)推薦

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

2013-05-14 10:13:06

WindowsLinux操作系統(tǒng)

2010-06-24 14:08:25

Linux Cat命令

2022-10-25 09:07:28

Linuxxargs命令

2021-01-04 05:43:59

LinuxBasename命令

2018-05-16 10:32:06

Linux命令find

2018-06-26 09:15:24

Linux命令history

2022-11-18 10:16:26

Linuxwc 命令

2021-04-16 11:18:56

LinuxlsusbUSB

2010-06-24 14:12:20

Linux Cat命令
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)