LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   pipe output to append to a text file (https://www.linuxquestions.org/questions/linux-newbie-8/pipe-output-to-append-to-a-text-file-86174/)

davee 08-27-2003 03:51 AM

pipe output to append to a text file
 
I would like to run a command that appends it's output to an existing text file. If I try:

command > file.text

it will overwrite file.text - how do I get it to append this file rather than overwrite?

Dave

MasterC 08-27-2003 03:55 AM

>>

command >> file.txt

Cool

davee 08-27-2003 04:53 AM

Thanks again...!

Dave

MasterC 08-27-2003 05:03 AM

You're welcome ;)

Cool

Lunar 03-13-2016 07:24 PM

the command(s) I like this to work for, doesn't
 
Quote:

Originally Posted by MasterC (Post 446175)
>>

command >> file.txt

I wanted to pipe or redirect output of 'head' and 'tail' run on the /var/log/messages file, but >> overwrites the 'head' portion of the output file.

Code:

# head /var/log/messages > messages-01.txt
# tail /var/log/messages >> messages-01.txt
# cat messages-01.txt

file 'messages-01.txt contains Only the output from 'tail'.
output from 'head' is being overwritten. It is not being appended.

any ideas why?

alternatively, is there a number of lines for cat (or another display util) to output, like tail has?
Code:

# tail -n 100 /var/log/messages > text.txt
so i could just use cat and say 'cat output first 5 lines >> cat output last 50 lines to file text.txt?

Thanks, Landis.

Tim Abracadabra 03-22-2016 07:44 PM

Quote:

Originally Posted by Lunar (Post 5514932)
I wanted to pipe or redirect output of 'head' and 'tail' run on the /var/log/messages file, but >> overwrites the 'head' portion of the output file.

Code:

# head /var/log/messages > messages-01.txt
# tail /var/log/messages >> messages-01.txt
# cat messages-01.txt

file 'messages-01.txt contains Only the output from 'tail'.
output from 'head' is being overwritten. It is not being appended.

any ideas why?

Using your exact commands on Debian 7.9 using the bash shell works fine. The tail with the >> does not
overwrite the content added previously with the head > command.

Unsure why that would not work for you. What distro are you using and what shell?
Code:

echo $SHELL
That might give a clue that may click with someone :)

Maybe try switching to another shell will help but I can't say I've ever see >> not append.

Quote:

alternatively, is there a number of lines for cat (or another display util) to output, like tail has?
Code:

# tail -n 100 /var/log/messages > text.txt
so i could just use cat and say 'cat output first 5 lines >> cat output last 50 lines to file text.txt?

Thanks, Landis.
cat does not have an option for outputting specific lines. See:
Code:

man cat
I know you can use sed for printing a range of line numbers but getting the last 50 lines is ...
quite ugly and I suspect less efficient than tail. In any case you would still be using >> to append, correct?

These commands meet your requirements when I tested them (Again using the bash shell)

Code:

head -n 5 /var/log/messages > text.txt
tail -n 50 /var/log/messages >> text.txt

Also, are you sure you didn't just use the up arrow to repeat the previous command, then edit it
to use tail instead of head and then maybe forget to add the second > character??

Easy to do ;)

Hope that helps,
Tim


All times are GMT -5. The time now is 06:01 AM.