I want to compare particular line of two text file and update one of the file if they are not same.
Asked
Active
Viewed 54 times
-4
-
1"Append"? Or "update"? What do the files look like? What have you tried? – Benjamin W. Feb 01 '16 at 17:05
-
Please see this for information on how to better ask a question http://stackoverflow.com/help/mcve – Adam B Feb 01 '16 at 23:43
-
I want to compare the root device details(specifically not the whole file ) of one vfstab file with other and update the later if the details are not same – Shell_Magic Feb 02 '16 at 10:26
1 Answers
0
Updating a line in a text file is technically not possible (unless the replacing line is of exactly the same length). You have to create a new file, which you can, in the end, move to the old one.
From your tags, I assume that you are looking for a shell solution, which is maybe not a good idea. It's probably more convenient to do it in, for instance, Perl or Ruby or Python.
One possibility is to use the commands head
and tail
, which allow you to dissect a file into parts. You can split your file into three parts: The part before the line in question, the line itself, and the lines which come after.
Another possibility is to use a loop and the read
command of the shell to process a file line by line, like this:
while read line
do
... # Decide here, whether to write $line or the replacement line
done <your_file

user1934428
- 19,864
- 7
- 42
- 87
-
Hi thanks for quick response !! How to replace the line if they are of same length – Shell_Magic Feb 02 '16 at 11:00
-
If you use zsh as a shell, have a look at the commands `sysopen`,`sysread` `sysseek` and `syswrite` in the *zshall* man-page. However, in this case a **real** programming language would be easier to use. – user1934428 Feb 02 '16 at 12:23