How to insert a text at the beginning of a file?

匿名 (未验证) 提交于 2019-12-03 02:12:02

问题:

So far I've been able to find how to add a line at the beginning of a file but that's not exactly what I want. I'll show it on a example

File content

some text at the beginning 

Result

 some text at the beginning 

It's similar but I don't want to create any new line with it...

I would like to do this with sed if possible.

回答1:

sed can operate on an address:

$ sed -i '1s/^/ /' file 

What is this magical 1s you see on every answer here? Line addressing!.

Want to add on the first 10 lines?

$ sed -i '1,10s/^/ /' file 

Or you can use Command Grouping:

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


回答2:

If the file is only one line, you can use:

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

If it's more than one line. one of:

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

I've included the latter so that you know how to do ranges of lines. Both of these "replace" the start line marker on their affected lines with the text you want to insert. You can also (assuming your sed is modern enough) use:

sed -i 'whatever command you choose' filename 

to do in-place editing.



回答3:

If you want to add a line at the beginning of a file, you need to add \n at the end of the string in the best solution above.

The best solution will add the string, but with the string, it will not add a line at the end of a file.

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


回答4:

You can use cat -

printf '%s' "some text at the beginning" | cat - filename 


回答5:

To insert just a newline:

sed '1i\\' 


回答6:

Hi with carriage return:

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


回答7:

Note that on OS X, sed -i file, fails. However, if you provide a backup extension, sed -i old file, then file is modified in place while file.old is created. You can then delete file.old in your script.



回答8:

echo -n "text to insert " ;tac filename.txt| tac > newfilename.txt 

The first tac pipes the file backwards (last line first) so the "text to insert" appears last. The 2nd tac wraps it once again so the inserted line is at the beginning and the original file is in its original order.



回答9:

PROBLEM: tag a file, at the top of the file, with the base name of the parent directory.

I.e., for

/mnt/Vancouver/Programming/file1 

tag the top of file1 with Programming.

SOLUTION 1 -- non-empty files:

bn=${PWD##*/}    ## bn: basename  sed -i '1s/^/'"$bn"'\n/' 

1s places the text at line 1 of the file.

SOLUTION 2 -- empty or non-empty files:

The sed command, above, fails on empty files. Here is a solution, based on https://superuser.com/questions/246837/how-do-i-add-text-to-the-beginning-of-a-file-in-bash/246841#246841

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

Note that the - in the cat command is required (reads standard input: see man cat for more information). Here, I believe, it's needed to take the output of the printf statement (to STDIN), and cat that and the file to temp ... See also the explanation at the bottom of http://www.linfo.org/cat.html.

I also added -f to the mv command, to avoid being asked for confirmations when overwriting files.

To recurse over a directory:

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

Note also that this will break over paths with spaces; there are solutions, elsewhere (e.g. file globbing, or find . -type f ... -type solutions) for those.

ADDENDUM: Re: my last comment, this script will allow you to recurse over directories with spaces in the paths:

#!/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 


回答10:

There is a very easy way:

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


回答11:

To add a line to the top of the file:

sed -i '1iText to add\' 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!