LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash ; changing some data inside of a file (https://www.linuxquestions.org/questions/linux-newbie-8/bash-%3B-changing-some-data-inside-of-a-file-667963/)

sebastian29 09-06-2008 08:59 AM

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

jschiwal 09-06-2008 09:09 AM

You could easily do that with sed.

Code:

sed '1,4s/^/%/;s/,/./g' demo
%-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


rlhartmann 09-06-2008 09:54 AM

Note, to save the changes with sed, redirect into a NEW file:
Quote:

sed '1,4s/^/%/;s/,/./g' originalfile > formattedfile

ghostdog74 09-06-2008 10:23 AM

Code:

exec < file
for i in {0..3} ; do read line; echo "%$line"; done
while read -r line; do echo ${line//,/.}; done


sebastian29 09-07-2008 04:00 AM

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

chrism01 09-07-2008 11:16 PM

Just wrap a loop around the soln above:
Code:

for file in ls
do
    soln-above
done

one way to change filename

new_file=`echo $file |sed 's/,/_/'`

sebastian29 09-08-2008 03:35 PM

thank you very much, that's very nice solution...
regards
sebastian

jschiwal 09-08-2008 09:56 PM

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.