Shell

如何根據行首模式將文本文件拆分為多個文件?

  • June 19, 2021

我有文本文件,我想根據我在各行開頭放置的任意“標籤”將它們分成不同的文件。

範例文本文件:

I CELEBRATE myself, and sing myself,  
And what I assume you shall assume, 
For every atom belonging to me as good belongs to you.

#here I loafe and invite my soul, 
#here I lean and loafe at my ease observing a spear of summer grass.

#there My tongue, every atom of my blood, form'd from this soil, this air,
#there Born here of parents born here from parents the same, and their parents the same, 
#here I, now thirty-seven years old in perfect health begin, 
#here Hoping to cease not till death.

在此範例中,我想刪除以 開頭的每一行#here並將其附加到名為 的文件here.txt中,以 開頭的每一行都添加到名為#there的文件there.txt中,並將每個未標記的行保留在原始文件中。(理想情況下刪除#here #there過程中的標籤。)

我認為使用此解決方案awk可能會有所幫助,但我是一個 Unix 菜鳥,我不知道如何適應我的問題:如何使用關鍵字邊界拆分文件

關於如何進行的任何建議?

PS:我在 OS X 上使用命令行。

您的案例比連結案例更簡單 - 您只需要查看每一行(或 awk 用語中的“記錄”)並決定將其發送到哪裡。所以:

awk '/^#here/{print > "here.txt"; next} /^#there/{print > "there.txt"; next} {print}' input.txt

剩餘的行將被列印到標準輸出;可移植地,您可以將其重定向到第三個文件(rest.txt例如),然後將其重命名為原始文件的名稱。如果你有 GNU awk,你可以使用inplace模組直接修改原始文件:

gawk -i inplace '/^#here/{print > "here.txt"; next} /^#there/{print > "there.txt"; next} {print}' input.txt

引用自:https://unix.stackexchange.com/questions/654975