到目前为止,我已经能够找出如何在文件的开头添加一行,但这并不完全是我想要的。我将用一个例子来说明:

文件内容

some text at the beginning

结果

<added text> some text at the beginning

它很相似,但是我不想用它创建任何新的行…

如果可能的话,我希望用sed来做这件事。


如果文件只有一行,你可以使用:

sed 's/^/insert this /' oldfile > newfile

如果它不止一行。之一:

sed '1s/^/insert this /' oldfile > newfile
sed '1,1s/^/insert this /' oldfile > newfile

我已经包括了后者,以便您知道如何进行行范围。这两种方法都用要插入的文本“替换”受影响行的开始行标记。您还可以(假设您的sed足够现代)使用:

sed -i 'whatever command you choose' filename

进行就地编辑。

Sed可以对地址进行操作:

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

每个答案上神奇的1是什么?行解决!

想在前10行添加<添加的文本> ?

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

或者你可以使用命令分组:

$ { echo -n '<added text> '; cat file; } >file.new
$ mv file{.new,}

嗨,用马车回:

sed -i '1s/^/your text\n/' file

如果你想在文件的开头添加一行,你需要在上面的最佳解决方案中在字符串的末尾添加\n。

最好的解决方案是添加字符串,但是使用字符串,它不会在文件的末尾添加一行。

sed -i '1s/^/your text\n/' file

你可以用cat -

printf '%s' "some text at the beginning" | cat - filename
echo -n "text to insert " ;tac filename.txt| tac > newfilename.txt

第一个tac反向传输文件(最后一行先传输),因此“要插入的文本”出现在最后。第2个tac再次将其换行,因此插入的行位于开头,原始文件保持原始顺序。

注意,在OS X上,sed -i <pattern>文件失败。但是,如果您提供了一个备份扩展名sed -i old <pattern> file,那么file将在file的位置被修改。旧的是创造出来的。然后您可以删除文件。老在你的剧本里。

插入换行符:

sed '1i\\'

在文件顶部添加一行:

sed -i '1iText to add\'

问题:标记一个文件,在文件的顶部,与父目录的基本名称。

例如,对

/mnt/Vancouver/Programming/file1

用Programming标记file1的顶部。

解决方案1——非空文件:

bn=${PWD##*/}    ## bn: basename

sed -i '1s/^/'"$bn"'\n/' <file>

1s将文本放在文件的第1行。

解决方案2——空文件或非空文件:

上面的sed命令在空文件上失败。下面是一个基于https://superuser.com/questions/246837/how-do-i-add-text-to-the-beginning-of-a-file-in-bash/246841#246841的解决方案

printf "${PWD##*/}\n" | cat - <file> > temp && mv -f temp <file>

注意,cat命令中的-是必需的(读取标准输入:有关更多信息,请参阅man cat)。在这里,我相信,它需要接受printf语句的输出(到STDIN),并将其和文件cat到temp…请参见http://www.linfo.org/cat.html底部的解释。

我还在mv命令中添加了-f,以避免在覆盖文件时被要求确认。

递归遍历一个目录:

for file in *; do printf "${PWD##*/}\n" | cat - $file > temp && mv -f temp $file; done

还要注意,这将用空格分隔路径;有解决方案,在其他地方(例如文件通配符,或查找。-type f…-type解决方案)。

回复:我的最后一个评论,这个脚本将允许你递归在路径中有空格的目录:

#!/bin/bash

## https://stackoverflow.com/questions/4638874/how-to-loop-through-a-directory-recursively-to-delete-files-with-certain-extensi

## To allow spaces in filenames,
##   at the top of the script include: IFS=$'\n'; set -f
##   at the end of the script include: unset IFS; set +f

IFS=$'\n'; set -f

# ----------------------------------------------------------------------------
# SET PATHS:

IN="/mnt/Vancouver/Programming/data/claws-test/corpus test/"

# https://superuser.com/questions/716001/how-can-i-get-files-with-numeric-names-using-ls-command

# FILES=$(find $IN -type f -regex ".*/[0-9]*")        ## recursive; numeric filenames only
FILES=$(find $IN -type f -regex ".*/[0-9 ]*")         ## recursive; numeric filenames only (may include spaces)

# echo '$FILES:'                                      ## single-quoted, (literally) prints: $FILES:
# echo "$FILES"                                       ## double-quoted, prints path/, filename (one per line)

# ----------------------------------------------------------------------------
# MAIN LOOP:

for f in $FILES
do

  # Tag top of file with basename of current dir:
  printf "[top] Tag: ${PWD##*/}\n\n" | cat - $f > temp && mv -f temp $f

  # Tag bottom of file with basename of current dir:
  printf "\n[bottom] Tag: ${PWD##*/}\n" >> $f
done

unset IFS; set +f

有一个非常简单的方法:

echo "your header" > headerFile.txt
cat yourFile >> headerFile.txt

只是为了好玩,这里有一个使用ed的解决方案,它没有不能处理空文件的问题。您可以把它放到一个shell脚本中,就像这个问题的任何其他答案一样。

ed Test <<EOF
a

.
0i
<added text>
.
1,+1 j
$ g/^$/d
wq
EOF

上面的脚本将要插入的文本添加到第一行,然后连接第一行和第二行。为了避免ed在无效连接错误时退出,它首先在文件末尾创建一个空行,如果它仍然存在,则稍后删除它。

限制:如果<added text>恰好等于单个句点,此脚本将不起作用。

我的观点是:

sed  -i '1i /path/of/file.sh' filename

即使字符串中包含正斜杠"/"

使用shell:

echo "$(echo -n 'hello'; cat filename)" > filename

不幸的是,命令替换将删除文件末尾的换行符。为了保持它们,人们可以使用:

echo -n "hello" | cat - filename > /tmp/filename.tmp
mv /tmp/filename.tmp filename

既不需要分组,也不需要命令替换。

别名是另一种解决方案。添加到你的init rc/ env文件:

addtail () { find . -type f ! -path "./.git/*" -exec sh -c "echo $@ >> {}" \; }
addhead () { find . -type f ! -path "./.git/*" -exec sh -c  "sed -i '1s/^/$@\n/' {}" \; }

用法:

addtail "string to add at the beginning of file"
addtail "string to add at the end of file"

使用echo方法,如果你像我一样使用macOS/BSD,就不要像其他人建议的那样使用-n开关。我喜欢为文本定义一个变量。

所以它是这样的:

Header="my complex header that may have difficult chars \"like these quotes\" and line breaks \n\n "

{ echo "$Header"; cat "old.txt"; } > "new.txt"
mv new.txt old.txt

我找到的最简单的解决方法是:

echo -n "<text to add>" | cat - myFile.txt | tee myFile.txt

注:

Remove | tee myFile.txt if you don't want to change the file contents. Remove the -n parameter if you want to append a full line. Add &> /dev/null to the end if you don't want to see the output (the generated file). This can be used to append a shebang to the file. Example: # make it executable (use u+x to allow only current user) chmod +x cropImage.ts # append the shebang echo '#''!'/usr/bin/env ts-node | cat - cropImage.ts | tee cropImage.ts &> /dev/null # execute it ./cropImage.ts myImage.png

博士 -

考虑使用ex。由于您想要给定行的前面,那么语法基本上与sed相同,但“就地编辑”选项是内置的。

我无法想象一个你有sed但没有ex/vi的环境,除非它是一个MS Windows盒子,有一些特殊的“sed.exe”,也许。

Sed和grep从ex / vi演变而来,所以说Sed语法与ex语法相同可能更好。

您可以将行号更改为#1以外的内容,或者搜索一行并更改该行。

source=myFile.txt
Front="This goes IN FRONT "
man true > $source
ex -s ${source} <<EOF
1s/^/$Front/ 
wq
EOF
$ head -n 3 $source
This goes IN FRONT TRUE(1)                                                    User Commands                                                    TRUE(1)

NAME

长的版本,我推荐ex(或者ed,如果你是一个很酷的孩子)。

我喜欢ex,因为它是可移植的,非常强大,允许我在不需要GNU(甚至BSD)扩展的情况下进行就地编写和/或备份。

此外,如果你知道ex的方法,那么你就知道如何在vi和vim中做它,如果那是你的专长的话。 注意,当我们使用"i"nsert和使用echo时,EOF没有被引用:

str="+++ TOP +++" && ex -s <<EOF
r!man true
1i
`echo "$str"`
.
"0r!echo "${str}"
wq! true.txt
EOF

0 r !Echo "${str}"也可以用作:0read!或:0 r !你可能在vi模式中使用过(实际上是一样的),但是:在这里是可选的,一些实现不支持“r”ead地址为零。 “r”直接读取到特殊的行#0(或从行1)会自动将所有内容“下”,然后只需:wq保存更改。

$ head -n 3 true.txt | nl -ba   
     1  +++ TOP +++
     2  TRUE(1)                          User Commands                         TRUE(1)
     3  

而且,大多数经典的sed实现都没有ex默认应该有的扩展(如\U&)。

Cat连接多个文件。<()将命令的输出作为文件发送。结合这两者,我们可以在文件的开头和结尾插入行,

cat <(echo "line before the file") file.txt <(echo "line after the file")