LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 07-07-2010, 12:54 PM   #1
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 112

Rep: Reputation: 15
Question Shell script to convert values on successive lines into consecutive ranges?


Hi all,

I have a series of input files formatted like this:
Code:
RTREVF, KOG3266 = 111
RTREVF, KOG3294 = 130
RTREVF, KOG3295 = 177
WAGF, KOG3307 = 107
JTTF, KOG3320 = 174
Each line represents a portion of a data matrix. I want to convert the numbers after the "=" to the range of that partition in the matrix such that the output file looks like this:
Code:
RTREVF, KOG3266 = 1-111
RTREVF, KOG3294 = 112-241
RTREVF, KOG3295 = 242-418
WAGF, KOG3307 = 419-525
JTTF, KOG3320 = 526-699
Can anyone think of an easy way to do this?

Thanks,
Kevin
 
Old 07-07-2010, 01:21 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,541

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
Code:
awk -vn=0 -F= '{printf("%s= %d-%d\n", $1, n+1, n+=$2);}' < input-file > output-file
 
1 members found this post helpful.
Old 07-07-2010, 09:39 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,689

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
You can just use the print in this case too:
Code:
awk -F= '{print $1"+ "(n+1)"-"(n+=$2)}' infile
Much of a muchness I guess
 
1 members found this post helpful.
Old 07-08-2010, 07:11 PM   #4
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 112

Original Poster
Rep: Reputation: 15
Wow! Thanks guys! Both work great!
 
Old 07-08-2010, 07:38 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,483

Rep: Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077
No concerns if the first record is "1" (or worse, "0") ?.
Corner cases are always a potential problem.
 
1 members found this post helpful.
Old 07-09-2010, 11:59 AM   #6
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 112

Original Poster
Rep: Reputation: 15
That's a good thought that I hadn't considered but a previous step in the pipeline throws out any sequences that are less than 25 characters long.

Thanks!
Kevin
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
sed/awk: Three consecutive blank lines in a file, how to delete two of them? recomboDNA Programming 8 06-17-2010 10:50 AM
bash script stdin accept values separated with new lines, commas, spaces m4rtin Programming 6 12-30-2009 07:22 AM
How to Substract two values in shell script amriteshsmsc Linux - General 9 06-26-2009 10:21 AM
Shell script that assign's values to fields NsearchOf Programming 16 06-01-2009 11:00 AM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 11:48 PM


All times are GMT -5. The time now is 06:16 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration