LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   sed - cleaner way to do this? (http://www.linuxquestions.org/questions/programming-9/sed-cleaner-way-to-do-this-915114/)

wolverene13 11-23-2011 10:06 AM

sed - cleaner way to do this?
 
Hi,

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:

Code:


sed 's/1 =/ 1=/;s/2 =/ 2=/;s/3 =/ 3=/;s/4 =/ 4=/;s/5 =/ 5=/;s/6 =/ =6/;s/7 =/ =7/;s/8 =/ 8=/;s/9 =/ 9=/g'

It works correctly, but it's a really sloppy way to do this. Does anyone know a "cleaner" way to accomplish what I am trying to do?

jschiwal 11-23-2011 10:18 AM

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.

grail 11-23-2011 11:53 AM

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

wolverene13 11-23-2011 01:34 PM

2 Attachment(s)
Quote:

Originally Posted by grail (Post 4531845)
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:

Code:


paste -d'\n' nonvariable.txt inoctets.txt | sort -n | awk '/([0-9]= port)/{print; printf "\n"};/(ATM | "oc | "gig)/{print x; printf "\n"};{x=$0}' | grep -v SON | grep -v AAL

And here's the string that creates exactly what I need, but it is incredibly sloppy and <i>way</i> too long:

Code:


paste -d'\n' nonvariable.txt inoctets.txt | sed 's/1 =/ 1=/;s/2 =/ 2=/;s/3 =/ 3=/;s/4 =/ 4=/;s/5 =/ 5=/;s/6 =/ =6/;s/7 =/ =7/;s/8 =/ 8=/;s/9 =/ 9=/g' | sort -n | awk '/([0-9]= port)/{print; printf "\n"};/(ATM | "oc | "gig)/{print x; printf "\n"};{x=$0}' | grep -v SON | grep -v AAL | sort -n | sort -r -k1,1n

I have attached both nonvariable.txt and inoctets.txt files to this post if you need them for whatever reason.

grail 11-24-2011 02:57 AM

How about:
Code:

paste -d'\n' nonvariable.txt inoctets.txt | sort -n | awk '!/SON|AAL/ && /[0-9]= port/{print $0 (/ATM | "oc | "gig/?"\n"x:"")}{x=gensub(/([1-9]) =/," \\1=","1")}'
I would mention that the pattern ' "oc ' never appears anywhere in your file(s)


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