S
S
samlowry2011-07-08 08:33:56
linux
samlowry, 2011-07-08 08:33:56

How to write the output of a file to the same file in Linux consoles?

For example, some
cat file | sort > file
File is overwritten in this case.

Answer the question

In order to leave comments, you need to log in

8 answer(s)
S
Sergey, 2011-07-08
@bondbig

I
Igor Ivanchenko, 2011-07-08
@evgsd

cat file | sort >> file?

@
@sledopit, 2011-07-08
_

What you want is possible in two ways:
1. cat file | sort > file.tmp; mv file.tmp file
2. Modifying a file via perl/sed/whatever-has-similar-features, launched with the -i option (actually they do the first step, but you don't see it)

A
Anatoly, 2011-07-08
@taliban

This cannot be done because most programs are normal, and do not read the entire file at once, but use buffers for this, read a part, displayed, read a part, displayed.
To do what you want, you just need to find a program (option) that reads the entire file before outputting it.

K
kader, 2011-07-08
@kader

and what prevents to save in a new file and then to make a concatenation?

V
VBart, 2011-07-08
@VBart

f=`cat file`; echo "$f" >> file

V
VBart, 2011-07-08
@VBart

Hurry up =) here's a solution for you:
sort file >> file
The sort command reads the entire file into memory before sorting, which is not surprising.

K
Kindman, 2011-07-08
@Kindman

You can rename the file "file" to "_file", merge what you need into "file" and then delete "_file".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question