簡體   English   中英

如何在linux中的多個文件的開頭添加多行?

[英]How to add multiple lines to the beginning of many files in linux?

我有一個文件beginning.txt,其中包含以下行:

Disclaimer Here
This is a Notice
I have something liere

如何執行linux命令將starts.txt中的所有行添加到與擴展名“.customfile”匹配的每個文件的頂部(注意.customfile只是一個文本文件,但這些文件可能在我當前的子目錄中)我想要更新的文件夾)?

我有許多帶有.customfile后綴的文件,我想要追加,所以尋找一種以編程方式執行此操作的方法。 我看到sed命令的示例僅限於一行。

sed -i '1s/^/<added text> /' file

用bash:

for file in $(find . -name "*.customfile"); do
  echo Processing $file

  cat beginning.txt $file > $file.modified

  mv $file.modified $file

done

sed命令r在附加某個位置的文件內容時非常有用,除非該位置是第0行。

在第1行之前插入文本就像在第0行之后添加一樣。如果允許使用sed '0r /path/to/beginning.txt *.customfile ,那將是解決方案。

您可以將進程替換與sed一起使用。 進程替換將生成一個sed命令,可以針對您搜索的任何文件運行該命令。

sed -i -f - file < <(sed 's/^/1i/' /path/to/beginning.txt)

進程sed 's/^/1i/' /path/to/beginning.txt to sed 's/^/1i/' /path/to/beginning.txt將生成sed命令以插入在/path/to/beginning.txt中找到的文本:

1iDisclaimer Here
1iThis is a Notice
1iI have something liere

sed -f -將從文件中讀取sed命令, -表示該文件是stdin

如果需要在多個文件上運行它,可以使用globs或findxargs

使用多個文件的示例:

sed -i -f - /path/to/*.customfile < <(sed 's/^/1i/' /path/to/beginning.txt)

使用find和xargs的示例:

find . -type f -name \*.customfile | xargs -I{} bash -c "sed -f - {} < <(sed 's/^/1i/' /tmp/beginning.txt)"

或者,您可以通過將輸出分配給變量並使用此字符串傳遞給sed命令來避免為每個文件重新運行sed 's/^/1i/' /path/to/beginning.txt

sedcommand=$(sed 's/^/1i/' /path/to/beginning.txt)
sed -i -f - /path/to/*.customfile <<< "$sedcommand"

或者從beginning.txt創建一個command.sed文件,並使用它將文本插入自定義文件。

sed 's/^/1i/' /path/to/beginning.txt > /path/to/command.sed
sed -i -f /path/to/command.sed /path/to/*.customfile

這是讓你入門的東西,你只需要根據自己的喜好進行調整:

for i in *.customfile; do  sed -i '1s/^/<your_text> \n/' $i; done

我想你可以試試這樣的東西

find . -name "*.customfile" -type f | while read i; do cat beginning.txt $i | tee $i 1>/dev/null ; done

也許不是最優雅的使用tee命令,但它完成了工作:)

你有兩個基本問題:

  1. 找到要更改的所有文件( *.customfile
  2. 將行添加到現有文件的開頭

使用find可以輕松解決#1問題。 通過將行添加到臨時文件,然后移動文件,#2可能是最容易做到的。

這是一個單行shell命令,它將執行它,假設您要在名為beginning.txt的文件中添加行:

find . -name "*.customfile" -print | while read fn ; do echo fixing $fn ; cat beginning.txt "$fn" > "$fn".new && mv "$fn" "$fn".old && mv "$fn".new "$fn" ; done

雖然cat是適合這項工作的shell工具(參見:Robert的答案,我建議改為),如果你確實想用多行替換折磨自己,或者在大量的開頭插入免責聲明文本文件,你可以使用sed地址前綴來定位一行的第一個開頭,然后使用sed替換第一行的開頭(例如文件的開頭),使用帶有免責聲明文本的正則表達式 '^'替換。

具體來說,你將使用sed 地址'0,addr2'形式,如果addr2正則表達式 )匹配第一行輸入它導致0,addr2形式在其范圍的末尾並替換(插入) )替換文本一次並退出。

例如,在每個文件的開頭添加免責聲明files (無論您使用通配符或find過程替代選擇需要的文件)將采取以下形式:

## set disclaimer text in `dc` however you like
dc='Disclaimer Here\nThis is a Notice\nI have something liere\n'

## use 0,addr2 Address form to insert at beginning of each file.
sed -i "0,/^/s/^/${dc}/" files

示例文件

$ cat dat/basefile.txt
========
The quick brown fox
jumps over the lazy dog.

示例使用/輸出(以stdout為例)

$ dc='Disclaimer Here\nThis is a Notice\nI have something liere\n' \
sed -e "0,/^/s/^/${dc}/" dat/basefile.txt
Disclaimer Here
This is a Notice
I have something liere
========
The quick brown fox
jumps over the lazy dog.

而不是折磨自己,而不是使用cat ...

這是另一個沒有tmp-File的小解決方案(只有初始文本在一個文件中)

for i in ``ls *.customfile``; do echo -e "``cat prepend.txt``$(cat $i)" > $i; done

請用正常的雙反推替換,我不知道如何在這里顯示正常的反推。

如果您不想要文件prepend.txt ,則可以在文本后面用帶有“\\ n”的行替換cat-command。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM