LinuxQuestions.org
Register a domain and help support LQ
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 06-21-2009, 10:19 AM   #1
ziggy25
Member
 
Registered: Aug 2005
Distribution: Debian 5.2
Posts: 56

Rep: Reputation: 15
Replacing a numeric string with a sequence of numbers


I have a text file with content that look like this..

Code:
1,3927,"AIS"
1,6928,"AIS"
1,3929"AIS"
1,5932,"AIS"
1,3931,"AIS"
1,4932,"AIS"
1,3933,"AIS"
1,9936,"AIS"
1,4935,"AIS"
1,5936,"AIS"
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

Code:
1,3927,"AIS"
1,3928,"AIS"
1,3929,"AIS"
1,3930,"AIS"
1,3931,"AIS"
1,3932,"AIS"
1,3933,"AIS"
1,3934,"AIS"
1,3935,"AIS"
1,3936,"AIS"
I could probably hack a Java program to do this but is there a command or a regular expression in vi that could do this quickly?

I know in vi you could do things like :s%/3987/3988/g but that will replace everything with 3988. Any ideas?


Thanks
 
Old 06-21-2009, 10:22 AM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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 11:36 AM. Reason: fixed the field separator; may need to actually SED the commas to spaces, then back
 
Old 06-21-2009, 10:27 AM   #3
noctilucent
Member
 
Registered: Jun 2009
Distribution: slackware
Posts: 123

Rep: Reputation: 25
For example, you could:

1. split contents into 3 files [field1.out, field2.out, field3.out]
2. sort field2.out
3. merge field1.out, field2.out, field3.out

fairly quickly.
 
Old 06-21-2009, 10:59 AM   #4
ziggy25
Member
 
Registered: Aug 2005
Distribution: Debian 5.2
Posts: 56

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by GrapefruiTgirl View Post
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.

Thanks
 
Old 06-21-2009, 11:00 AM   #5
ziggy25
Member
 
Registered: Aug 2005
Distribution: Debian 5.2
Posts: 56

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by noctilucent View Post
For example, you could:

1. split contents into 3 files [field1.out, field2.out, field3.out]
2. sort field2.out
3. merge field1.out, field2.out, field3.out

fairly quickly.
Yes but how do i do it? Im not really proficient in shell scripting so an example would be helpfull
 
Old 06-21-2009, 11:13 AM   #6
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
huh?

Quote:
Originally Posted by ziggy25 View Post
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.

Thanks,

Sasha
 
Old 06-21-2009, 12:56 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
He's not very good at describing his request, I'm afraid,but
I believe what he WANTS is this:
Code:
awk -F, '{if(NR==1){start=$2}else{start++};print $1","start","$3}' ugh 
1,3927,"AIS"
1,3928,"AIS"
1,3929,"AIS"
1,3930,"AIS"
1,3931,"AIS"
1,3932,"AIS"
1,3933,"AIS"
1,3934,"AIS"
1,3935,"AIS"
1,3936,"AIS"
with ugh being the file.


Cheers,
Tink

Last edited by Tinkster; 06-21-2009 at 12:57 PM.
 
Old 06-21-2009, 01:39 PM   #8
ziggy25
Member
 
Registered: Aug 2005
Distribution: Debian 5.2
Posts: 56

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Tinkster View Post
He's not very good at describing his request, I'm afraid,but
I believe what he WANTS is this:
Code:
awk -F, '{if(NR==1){start=$2}else{start++};print $1","start","$3}' ugh 
1,3927,"AIS"
1,3928,"AIS"
1,3929,"AIS"
1,3930,"AIS"
1,3931,"AIS"
1,3932,"AIS"
1,3933,"AIS"
1,3934,"AIS"
1,3935,"AIS"
1,3936,"AIS"
with ugh being the file.


Cheers,
Tink
Yes thats exactly what i needed. Apologies my English is not very top class

So what exactly is happening there? is ugh the input or output file?

Thanks
 
Old 06-21-2009, 01:41 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Quote:
Originally Posted by ziggy25 View Post
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


Quote:
Originally Posted by ziggy25 View Post
is ugh the input or output file?

Thanks

Input.



Cheers,
Tink

Last edited by Tinkster; 06-21-2009 at 01:45 PM.
 
Old 06-21-2009, 02:42 PM   #10
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
OOOOOhhhhhh! Ok! Now I see.

@ Tink: not bad for a chap who just got out of bed; thanks for decoding the topic/ desired output. I missed that one.

@ myself: learn some Awk to complement my Gawk.

@ the OP: there you go!
 
Old 06-21-2009, 03:18 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Quote:
Originally Posted by GrapefruiTgirl View Post
OOOOOhhhhhh! :) Ok! Now I see.

@ Tink: not bad for a chap who just got out of bed; thanks for decoding the topic/ desired output. I missed that one.
Heh. Thanks ;)

Quote:
Originally Posted by GrapefruiTgirl View Post
@ myself: learn some Awk to complement my Gawk.
Always good to know the basics ... =}


Cheers,
Tink
 
Old 06-21-2009, 07:43 PM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
awk -F"," 'NR==1{s=$2;}{print $1,s++,$3}' OFS="," file
 
Old 12-03-2009, 03:17 AM   #13
ziggy25
Member
 
Registered: Aug 2005
Distribution: Debian 5.2
Posts: 56

Original Poster
Rep: Reputation: 15
I forgot to thank you all for the help.
The awk solution worked very well.

Thanks all for your help
 
Old 12-03-2009, 10:18 AM   #14
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by ziggy25 View Post
I forgot to thank you all for the help.
The awk solution worked very well.

Thanks all for your help
Good stuff!

If this solves the issue, you can mark this thread [solved] if you haven't yet, by using "Thread Tools" near the top of the post.

Sasha
 
  


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
[SOLVED] regex with numbers in sequence schneidz Programming 6 04-28-2009 06:52 PM
Replacing selected columns by Serial numbers incremently raghu123 Programming 8 08-25-2008 02:27 AM
problem in comparing numeric with string naren_0101bits Programming 1 01-28-2008 08:10 AM
sequence of numbers, how to extract which numbers are missing jonlake Programming 13 06-26-2006 03:28 AM
left numeric keyboard is typing numbers (problem with NumLock on/off) zstingx Linux - General 0 11-10-2003 09:59 AM


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

Main Menu
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