Replacing a numeric string with a sequence of numbers
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need to change the contents so that the number between the commas (second column) is in sequence starting from whatever the number is on the first line. As shown below
Try the `sort` command; perhaps something (off top of my head, and assuming that the number on the first line is the LOWEST of the numbers) like `sort -t , -u -k2 $filename > $new_filename` though you'll definitely want to look up the right syntax in the man page.
SVA
Last edited by GrapefruiTgirl; 06-21-2009 at 12:36 PM.
Reason: fixed the field separator; may need to actually SED the commas to spaces, then back
Try the `sort` command; perhaps something (off top of my head, and assuming that the number on the first line is the LOWEST of the numbers) like `sort -t- -u -k2 $filename > $new_filename` though you'll definitely want to look up the right syntax in the man page.
SVA
Wouldnt the sort command just sort the numbers? I dont just want to sort them i want them to be in sequence i.e. 5,3,4,7,9 to change to 5,6,7,8. Just sorting them would result in 3,4,5,7,9.
Wouldnt the sort command just sort the numbers? I dont just want to sort them i want them to be in sequence i.e. 5,3,4,7,9 to change to 5,6,7,8. Just sorting them would result in 3,4,5,7,9.
Thanks
Not sure I understand what you asked here.. I can't see how 5,3,4,7,9 can be sorted to 5,6,7,8 no matter how you go about it.
The sort command sorts records in a file, not items in the record.
Why not try experimenting with what I gave you, provide us some feedback on what it did, and explain how what it did is not what you want.
Yes thats exactly what i needed. Apologies my English is not very top class
So what exactly is happening there?
What you really wanted was to have a sequence
in the second column, starting with the value of
the first row (discarding all values previously
in column two). What the awk script does is to
store the first value [if we're on the first line
(NR==1)], and then just keeps adding one to the
start value for any row we encounter, replacing
the value in column 2 ($2, which we initially read
in the first row) with the value of start, while
leaving column 1 and 3 intact. Really I should have
called the thing sequence rather than start to make
it clearer - but I only just crawled out of bed ;D
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.