LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-26-2012, 08:39 AM   #1
charlyb21
LQ Newbie
 
Registered: Apr 2012
Posts: 1

Rep: Reputation: Disabled
how do i remove the first 5 numbers from my data string ??


Hi,

I am trying to process twelve .csv files that contain 9 columns of 20,000 data points each.
I have got a piece of Fortran code to perform an FFT on this data and it runs.
I also have a Bash command file to run the FFT on all my data sets one after the other and output the results to a .dat file.



BUT i have a problem, the data does not open in excel so im trying to use gnuplot to produce graphs of the .dat files but the first column of my data is time and due to the logger used to take the data the time values have 12 digits. Of these 12 digits the first 5 are all the same so i wish to remove the first 5 digits from the string so that gnuplot is able to plot it as at the moment it is cutting off the last 2 digits which are the key ones.

Any help with this would be greatly appreciated!!
Please let me know if anything is not clear
 
Old 04-26-2012, 09:30 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by charlyb21 View Post
... i wish to remove the first 5 digits from the string ...
Try this ...
Code:
sed 's/,/\n/g' $InFile \
|cut -c6-              \
|paste -s -d,          \
> $OutFile
Explanation:
If the input file looks like this ...
Code:
123456789,123456789,123456789,123456789,123456789,123456789
... the sed changes it to this ...
Code:
123456789
123456789
123456789
123456789
123456789
123456789
... then cut changes it to this ...
Code:
6789
6789
6789
6789
6789
6789
... then paste changes it to this ...
Code:
6789,6789,6789,6789,6789,6789
Daniel B. Martin

Last edited by danielbmartin; 04-26-2012 at 09:59 AM. Reason: Improved code example, added explanation
 
Old 04-26-2012, 10:42 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
hmmm ... my understanding is that it is the first 5 from each line and not from each field (maybe OP can clear that up). Also you do not indicate if the numbers start at the
beginning of the line or if there is possibly white space prior? (I will assume there is none)

So borrowing Daniel's example:
Code:
echo '123456789,123456789,123456789,123456789,123456789,123456789' | sed -r 's/^[0-9]{5}//'
If running on a file:
Code:
sed -ri 's/^[0-9]{5}//' file
This will edit the file in place.

You also do not say why Excel had issues?
 
1 members found this post helpful.
Old 04-26-2012, 11:03 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
... my understanding is that it is the first 5 from each line and not from each field ...
If that is the case, then maybe we need nothing fancier than ...
Code:
cut -c6- $InFile > $OutFile
Quote:
This will edit the file in place.
Not to quarrel with the guru, I always avoid editing an input file in place. I want to be able to run and re-run a program as my code evolves, always starting with the same input file. In other words, it is dangerous to tamper with evidence!

Daniel B. Martin
 
Old 04-26-2012, 12:01 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Not to quarrel with the guru, I always avoid editing an input file in place. I want to be able to run and re-run a program as my code evolves, always starting with the same input file. In other words, it is dangerous to tamper with evidence!
No probs, supply -i an extension:
Code:
sed -ri.bak 's/^[0-9]{5}//' file
Now you have the edited file called "file" and the old file stored in "file.bak"

And definitely no quarrel ... I learn something different every time I look at questions
 
  


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
Replacing a numeric string with a sequence of numbers ziggy25 Linux - Newbie 13 12-03-2009 10:18 AM
Remove everything up to the last numbers of a string w/ sed or awk OutThere Linux - General 4 04-23-2009 07:01 PM
how to convert a string of numbers to integer? cigarstub Programming 8 04-09-2007 07:49 PM
Subtracting numbers in a string using C# mrobertson Programming 6 01-11-2006 02:30 PM
C making an int from a string of numbers ocularbob Programming 11 02-25-2004 02:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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