use*_*060 28 text command-line editing
我是 Linux 新手。我只需.conf要从打开的终端编辑文件,而不需要使用任何文本编辑器。也就是说,我可以从打开的终端将单词和句子添加到配置文件中吗?
示例:command /home/.../file.conf -add 'abcd'到第 23 行,依此类推。最后,保存它。
是否可以仅使用命令在该配置文件中搜索特定单词并将新文本添加到该配置文件的下一行?
Far*_*ron 50
当我编写脚本以执行与您所要求的相同但以编程方式执行的操作时,我通常会这样做。
echo "Hello you!" >> myfile.txt
echo "this is 2nd line text" >> file.txt
echo "last line!" >> file.txt
Run Code Online (Sandbox Code Playgroud)
瞧!你说对了。重要的是要注意>>意味着向现有文件添加新行,同时>只需简单地覆盖所有内容。
Sto*_*rux 22
使用 sed 可以轻松实现从打开的终端向配置文件添加单词和句子。
sed -i '23iabcd' file.conf
Run Code Online (Sandbox Code Playgroud)
在第 23 行将文本abcd插入文件file.conf
-i直接对文件进行修改file.conf。
如果你想使用,awk那么:
awk -v n=23 -v s="abcd" 'NR == n {print s} {print}' file > file.conf
Run Code Online (Sandbox Code Playgroud)
以下在 SearchPattern 后添加一行。
sed -i '/SearchPattern/aNew Text' SomeFile.txt
Run Code Online (Sandbox Code Playgroud)
它在包含 SearchPattern 的每行下方插入一行新文本。
要添加两行,您可以使用 a\并在键入新文本时输入换行符。
sed -i '/pattern/a \
line1 \
line2' inputfile
Run Code Online (Sandbox Code Playgroud)
小智 5
您也可以使用该printf命令。
在文件中添加行
$ printf "\nThis is a new line to your document" >> file.txt
Run Code Online (Sandbox Code Playgroud)
覆盖文件
$ printf "This overwrites your file" > file.txt
Run Code Online (Sandbox Code Playgroud)
awk '{if ($1 ~ /regex/) print $1 "content to be added"; else print $1}' < inputfile > outputfile
Run Code Online (Sandbox Code Playgroud)
笔记:
怎么运行的:
inputfile用于读取输入线,清除outputfile并打开它用于写入输出线| 归档时间: |
|
| 查看次数: |
202879 次 |
| 最近记录: |