LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to make a command actually change a file (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-make-a-command-actually-change-a-file-400229/)

tongar 01-06-2006 08:49 AM

How to make a command actually change a file
 
In the bash shell, I am learning all about redirection and command usage. It is clear that the disjoint committee that designed all these commands had no regard for a standard syntax. One thing they all valued, however, was a desire to make each command and option as short, criptic and non-verbose as possible.

Thats why it puzzles me that I have not encountered a way yet to just have a command actually change the file it reads as stdin. Attempts to have the same file as stdout result in a blanking out of the stdin file. For example, don't do this if you want the file:

nl <somefile.txt >somefile.txt
or
nl <somefile.txt | tee >somefile.txt

It seems a violation of non-verboseness, the everything on one line rule, and a missed oportunity to create a new criptic one character operand for there to be no way to do this other than:

nl <somefile.txt >somefile2.txt
mv -f somefile2.txt somefile.txt

Am I missing something?

thanks,
tongar

pixellany 01-06-2006 09:01 AM

What you refer to as short, cryptic, and non-verbose is part of the essence of *ix systems. One day, you will learn to like it.

You don't "change files" at the command line---that is done in text editors--or hex editors. What you can do with files at the command line:
read
append
write
change ownership, access, and access time

to read or write (or append), you use any command that needs or generates data and you redirect the stdin and stdout to the file, eg:
ls > list creates a new file with the output of the ls command
cat file1 > file2 reads file1 and sends the output to file2
cat file1 >> file2 reads file1 and APPENDS the output to file2
cat file1 | grep word > file2 reads file1 and writes lines containing "word" to file2.
etc.....
touch file either creates file or updates its access time
rm file removes the file

marozsas 01-06-2006 10:10 AM

In fact, may help, if you think about commands more like a filter that change the input in some way and output the modified data. In general, there is no files here, just a input flow and an output flow. Theses flows can come from files and goes to files but its just a special case !

An example:
cat | tr '[a-z]' '{A-Z]' | nl

Here, what you type is the input flow. But it can be ANY program's output, either the normal output or the error output, whatever. There is no file here. And the final result can be used inside a logical test inside a program, without files taking part in this game.

for file in `find $DIR -type f | sed -e 's#/# #g' | rev | cut -d" " -f 1 | rev`; do
echo "do something with file $file"
done

Here, I list all files below $DIR, and extract from the full path (/dir1/dir2/dir3/filename) just the filename part.
Again, there is no need to intermediate files. (Hey guys, I known /usr/bin/basename, this is just a example, ok?)

The point here is theses commands was designed to handle input flow and produce another flow.

Well, about the cryptic names I must agree without. But, I prefer cryptic names than aCommandWithVeryLongName, just like Pascal commands.

shengchieh 01-06-2006 09:18 PM

Take a look at the command, sed. You can use sed to edit your file
without entering a text editor.

man sed and/or
info sed

should gives more info. Googling sed will gives alot of docs.

Sheng-Chieh


All times are GMT -5. The time now is 12:12 PM.