bash ; changing some data inside of a file
Hello,
I got some data files which I need to change a little bit. The data file looks like this: -0,090 0,000 05BA 0B3A -719,930 -720,000 -719,655 -719,800 -719,340 -719,600 -719,045 -719,200 -718,740 -718,900 -718,435 -718,600 -718,150 -718,300 ... I have to prepare it for octave. This means the first four lines should be as a comment (I think - %) and the rest lines should have no commas but dots. It should look like: %-0,090 0,000 % %05BA 0B3A % -719.930 -720.000 -719.655 -719.800 -719.340 -719.600 -719.045 -719.200 -718.740 -718.900 -718.435 -718.600 -718.150 -718.300 ... I try to write a bash script, but I can not find a function that looks for a character in a file. Can you help me? regards, sebastian |
You could easily do that with sed.
Code:
sed '1,4s/^/%/;s/,/./g' demo |
Note, to save the changes with sed, redirect into a NEW file:
Quote:
|
Code:
exec < file |
Hello,
thank you very much for the solution with sed; it works!! the second task is to do it automatically with all 14000 files in one directory changing the names of the files from 0,00 to 0_00 (0,10 to 0_10) and so on... greetings, sebastian |
Just wrap a loop around the soln above:
Code:
for file in ls new_file=`echo $file |sed 's/,/_/'` |
thank you very much, that's very nice solution...
regards sebastian |
In your for loop, the file will be in a variable. You could run the sed line like
sed '1,4s/^/%/;s/,/./g "$file" >"${file//,/_}" Using variable substition will be quicker and cleaner the using sed to change the filename. |
All times are GMT -5. The time now is 06:14 PM. |