ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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 am trying to line up numbers in a document so that they will sort correctly. The first column consists of single and double digit numbers, followed by an equals sign. An example of the original data (after using the "sort -n" command) in the text file looks like this:
Code:
7= port ethernet 3/2 "gig NF,Egress,95.LUXZ.655712..CEVA,CRVACHVL02,g8/7,1Gb
6= 31.9271
8= port ethernet 3/3 "gig NF,Ingress,95.LUXZ.754327..CEVA,A7CHVLVAXA28W,4/1/14,1Gb
7= -77.4814
9= port ethernet 3/4 "gig NF,Ingress,95.LUXZ.754332..CEVA,A7CHVLVAXA29W,4/1/14,1Gb
8= 3.10811
10= port ethernet 4/1 "gig NF,Egress,95.LUXZ.754333..CEVA,CRVACHVL01,g8/12,1Gb
10= 45.642
11= port ethernet 4/2 "gig NF,Egress,95.LUXZ.754330..CEVA,CRVACHVL02,g8/12,1Gb
11= -77.1873
I need it to look like this:
Code:
7= port ethernet 3/2 "gig NF,Egress,95.LUXZ.655712..CEVA,CRVACHVL02,g8/7,1Gb
6 = 31.9271
8= port ethernet 3/3 "gig NF,Ingress,95.LUXZ.754327..CEVA,A7CHVLVAXA28W,4/1/14,1Gb
7= -77.4814
9= port ethernet 3/4 "gig NF,Ingress,95.LUXZ.754332..CEVA,A7CHVLVAXA29W,4/1/14,1Gb
8= 3.10811
10= port ethernet 4/1 "gig NF,Egress,95.LUXZ.754333..CEVA,CRVACHVL01,g8/12,1Gb
10= 45.642
11= port ethernet 4/2 "gig NF,Egress,95.LUXZ.754330..CEVA,CRVACHVL02,g8/12,1Gb
11= -77.1873
...so that when I sort it, it will be in numerical order. Currently it won't do it because the sort command does not consider the single digits to be in the same column or something to that effect.
This:
Code:
sed 's/[0-9] =/[0-9]=/g'
...doesn't work because it replaces the numbers with "[0-9]". The best solution I've found so far is this:
Please edit your post so the examples are in [ code ] blocks. Outside the blocks, the text is reformatted.
Look at "man 7 regex". You can use character classes in your regular expressions. For example:
s/^\([[:digit:]]*\)=/\1 =/
or
s/^\([0-9]*\)=/\1 =/
Also read the man page for sort. Using the numeric sort option should work. Also consider using = as the field separator to just sort on the first column.
I am not sure I understand the output you want it to look like?? Are you sorting by the first number or not? (you seem to have 7s and 8s out of order)
Why can't you just use:
Code:
sort -k1,1n file
I am just using "sort -n" so I would think it would work, but it looks like it's sorting the columns separately or something. Here's my whole string without the kludge that creates the desired output:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.