Add one line to file in linux

Shell one liner to prepend to a file

This still uses a temp file, but at least it is on one line:

echo "text" | cat - yourfile > /tmp/out && mv /tmp/out yourfile 

@makboi the — after cat is a convention used in Shell programming to mean that the input of the command should be read from stdin . The output of commands such as echo in the answer is written to the stdout , and written into stdin for the cat command using the pipe | . Think of piping as passing stdout of one command to the stdin of another, hence the name — the left hand command’s output flows into the right hand command’s input

Sorry to be a pedant, but && means this is as much of a «one-liner» as minifiying Javascript files creates a «one-liner», and you might confuse newbies who don’t know this operator separates two commands. It’d be clearer written as the separated lines echo «text» | cat — yourfile > /tmp/out mv /tmp/out yourfile — maybe sed -i ‘1s/^/prepend me\n/’ filename would qualify a little better for a true «one line» prepender

echo '0a your text here . w' | ed some_file 

The hack below was a quick off-the-cuff answer which worked and received lots of upvotes. Then, as the question became more popular and more time passed, people started reporting that it sorta worked but weird things could happen, or it just didn’t work at all. Such fun.

I recommend the ‘sponge’ solution posted by user222 as Sponge is part of ‘moreutils’ and probably on your system by default. (echo ‘foo’ && cat yourfile) | sponge yourfile

The solution below exploits the exact implementation of file descriptors on your system and, because implementation varies significantly between nixes, it’s success is entirely system dependent, definitively non-portable, and should not be relied upon for anything even vaguely important. Sponge uses the /tmp filesystem but condenses the task to a single command.

Now, with all that out of the way the original answer was:

Creating another file descriptor for the file ( exec 3<> yourfile ) thence writing to that ( >&3 ) seems to overcome the read/write on same file dilemma. Works for me on 600K files with awk. However trying the same trick using ‘cat’ fails.

Passing the prependage as a variable to awk ( -v TEXT=»$text» ) overcomes the literal quotes problem which prevents doing this trick with ‘sed’.

#!/bin/bash text="Hello world What's up?" exec 3<> yourfile && awk -v TEXT="$text" 'BEGIN ' yourfile >&3 

Источник

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

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.

Читайте также:  Linux чем упаковать файл

19 Answers 19

sed can operate on an address:

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

Want to add on the first 10 lines?

$ < echo -n ''; cat file; > >file.new $ mv file

-i stands for in-place, you can append a suffix to -i to make a copy rather than overwrite. -i.new would make a new file ending with .new, but just -i would edit the file directly.

Note that the sed won’t work on an empty file — afaict sed can’t be made to do anything at all with 0-length input.

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.

On Mac OS, was getting error with «undefined label». Found that you need to give an extension for a backup file; see mkyong.com/mac/…

Under Mac OS’s sed version you have to supply backup file name with -i option. One can pass just an empty string for no backup like sed -i » ‘1s/^/new test\n/’ filename

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 
echo "$(echo -n 'hello'; cat filename)" > filename 

Unfortunately, command substitution will remove newlines at the end of file. So as to keep them one can use:

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

Neither grouping nor command substitution is needed.

This is the superior and simpler solution to all the other ones using sed , which don’t work for empty files.

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

This would only output the text followed by the file’s content, but it does not modify the file at all.

That’s a good solution, I wonder why it didn’t get any upvotes. Here’s mine my good sir. Also, why printf and not a simple echo ?

I tried directing this into a file by appending > file to the command, it jus spammed my terminal with «some text at the beginning»

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

This will work even is the string containing forward slash «/»

Читайте также:  Акронис тру имидж линукс

To add a line to the top of the file:

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.

I’ve found macOS sed wants a dot to edit in place without creating a backup: sed -i. file

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

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

/mnt/Vancouver/Programming/file1 

tag the top of file1 with Programming .

SOLUTION 1 — non-empty files:

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

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

SOLUTION 2 — empty or non-empty files:

printf "$\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 "$\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 ".*/7*") ## 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: $\n\n" | cat - $f > temp && mv -f temp $f # Tag bottom of file with basename of current dir: printf "\n[bottom] Tag: $\n" >> $f done unset IFS; set +f 

Источник

Append text to file from command line without using io redirection

this sounds like an XY problem (perlmonks.org/index.pl?node_id=542341), why do you need append without redirection?

Читайте также:  All linux commands on android

@Joel : could be true. I asked the question for a colleague, and really don’t know what the exact problem is.

Typically useful when manipulating files with sudo (the IO are under the user’s environment), and you sometimes have a limited set of tools allowed via sudo.

5 Answers 5

If you don’t mind using sed then,

$ cat test this is line 1 $ sed -i '$ a\this is line 2 without redirection' test $ cat test this is line 1 this is line 2 without redirection

As the documentation may be a bit long to go through, some explanations :

  • -i means an inplace transformation, so all changes will occur in the file you specify
  • $ is used to specify the last line
  • a means append a line after
  • \ is simply used as a delimiter

I get sed: -i may not be used with stdin on macOS 13.3 bash shell. Probably cos of stackoverflow.com/a/21243111/259453

If you just want to tack something on by hand, then the sed answer will work for you. If instead the text is in file(s) (say file1.txt and file2.txt):

perl -e ‘open(OUT, «>>», «outfile.txt»); print OUT while (<>);’ file*.txt

N.B. while the >> may look like an indication of redirection, it is just the file open mode, in this case «append».

You can use the —append feature of tee :

cat file01.txt | tee --append bothFiles.txt cat file02.txt | tee --append bothFiles.txt 
cat file01.txt file02.txt | tee --append bothFiles.txt 

I assume the request for no redirection ( >> ) comes from the need to use this in xargs or similar. So if that doesn’t count, you can mute the output with >/dev/null .

Piping will also step out of xargs.. So not much better than >> , the best approach here is using sh -c

@Tofandel, I meant before xargs . You can pipe as much as you want before xargs ; but >> will require you to end the pipe, which then can’t continue to xargs .

You can use Vim in Ex mode:

I like the approach in this answer, but it doesn’t seem to append to the file. A simple example like this: for fidx in $(seq 1 6); do ex -sc «a|Something$» -cx test.txt; done produces text that is not ordered:cat test.txt Something2 Something1 Something3 Something4 Something5 Something6. How do I make it go to the end of the file before appending? Interestingly, it only seems to happen for the first two lines of an empty file. If I run the example multiple times everything is ordered correctly.

Try putting a $ before the a, which references the end of the file: ex -sc «$a|Something$» -cx test.txt

Источник

Оцените статью
Adblock
detector