LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-06-2008, 08:59 AM   #1
sebastian29
LQ Newbie
 
Registered: Mar 2008
Location: Munich
Distribution: Gentoo
Posts: 10

Rep: Reputation: 0
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
 
Old 09-06-2008, 09:09 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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
 
Old 09-06-2008, 09:54 AM   #3
rlhartmann
Member
 
Registered: Mar 2008
Posts: 73

Rep: Reputation: 16
Note, to save the changes with sed, redirect into a NEW file:
Quote:
sed '1,4s/^/%/;s/,/./g' originalfile > formattedfile

Last edited by rlhartmann; 09-06-2008 at 09:55 AM. Reason: Fixed a typo
 
Old 09-06-2008, 10:23 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
exec < file
for i in {0..3} ; do read line; echo "%$line"; done
while read -r line; do echo ${line//,/.}; done
 
Old 09-07-2008, 04:00 AM   #5
sebastian29
LQ Newbie
 
Registered: Mar 2008
Location: Munich
Distribution: Gentoo
Posts: 10

Original Poster
Rep: Reputation: 0
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
 
Old 09-07-2008, 11:16 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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/,/_/'`
 
Old 09-08-2008, 03:35 PM   #7
sebastian29
LQ Newbie
 
Registered: Mar 2008
Location: Munich
Distribution: Gentoo
Posts: 10

Original Poster
Rep: Reputation: 0
thank you very much, that's very nice solution...
regards
sebastian
 
Old 09-08-2008, 09:56 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.

Last edited by jschiwal; 09-08-2008 at 09:57 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
why is growisofs changing my file names with data dvd? kryptobs2000 Linux - Software 2 01-17-2008 12:57 PM
using su inside a bash file elie nasrani Linux - Newbie 3 08-22-2007 09:10 PM
how will you know if a cd-rw has a data inside? jaepi Programming 4 05-17-2007 08:57 AM
Bash Scripting help, altering data in text file. trey85stang Linux - General 4 05-31-2006 05:18 AM
Setting Bash Variables from outside data file llewis Programming 10 01-12-2005 04:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration