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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
06-21-2009, 11:19 AM
|
#1
|
Member
Registered: Aug 2005
Distribution: Debian 5.2
Posts: 56
Rep:
|
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
|
|
|
06-21-2009, 11:22 AM
|
#2
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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
|
|
|
06-21-2009, 11:27 AM
|
#3
|
Member
Registered: Jun 2009
Distribution: slackware
Posts: 123
Rep:
|
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.
|
|
|
06-21-2009, 11:59 AM
|
#4
|
Member
Registered: Aug 2005
Distribution: Debian 5.2
Posts: 56
Original Poster
Rep:
|
Quote:
Originally Posted by GrapefruiTgirl
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
|
|
|
06-21-2009, 12:00 PM
|
#5
|
Member
Registered: Aug 2005
Distribution: Debian 5.2
Posts: 56
Original Poster
Rep:
|
Quote:
Originally Posted by noctilucent
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
|
|
|
06-21-2009, 12:13 PM
|
#6
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
huh?
Quote:
Originally Posted by ziggy25
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
|
|
|
06-21-2009, 01:56 PM
|
#7
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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 01:57 PM.
|
|
|
06-21-2009, 02:39 PM
|
#8
|
Member
Registered: Aug 2005
Distribution: Debian 5.2
Posts: 56
Original Poster
Rep:
|
Quote:
Originally Posted by Tinkster
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
|
|
|
06-21-2009, 02:41 PM
|
#9
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Originally Posted by ziggy25
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
is ugh the input or output file?
Thanks
|
Input.
Cheers,
Tink
Last edited by Tinkster; 06-21-2009 at 02:45 PM.
|
|
|
06-21-2009, 03:42 PM
|
#10
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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!
|
|
|
06-21-2009, 04:18 PM
|
#11
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Originally Posted by GrapefruiTgirl
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
@ myself: learn some Awk to complement my Gawk.
|
Always good to know the basics ... =}
Cheers,
Tink
|
|
|
06-21-2009, 08:43 PM
|
#12
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Code:
awk -F"," 'NR==1{s=$2;}{print $1,s++,$3}' OFS="," file
|
|
|
12-03-2009, 04:17 AM
|
#13
|
Member
Registered: Aug 2005
Distribution: Debian 5.2
Posts: 56
Original Poster
Rep:
|
I forgot to thank you all for the help.
The awk solution worked very well.
Thanks all for your help
|
|
|
12-03-2009, 11:18 AM
|
#14
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
Quote:
Originally Posted by ziggy25
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
|
|
|
All times are GMT -5. The time now is 04:37 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|