LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2011, 09:06 AM   #1
wolverene13
Member
 
Registered: May 2010
Location: Matiland, FL
Distribution: Debian Squeeze
Posts: 57

Rep: Reputation: 0
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?

Last edited by wolverene13; 11-23-2011 at 09:25 AM. Reason: Added [code] blocks
 
Old 11-23-2011, 09:18 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 11-23-2011, 10:53 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 11-23-2011, 12:34 PM   #4
wolverene13
Member
 
Registered: May 2010
Location: Matiland, FL
Distribution: Debian Squeeze
Posts: 57

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
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.
Attached Files
File Type: txt inoctets.txt (386 Bytes, 15 views)
File Type: txt nonvariable.txt (2.6 KB, 22 views)
 
Old 11-24-2011, 01:57 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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)
 
  


Reply

Tags
columns, sed, sort



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
Looking for an alternative 'cleaner' solution flea89 Linux - Newbie 3 03-14-2010 02:27 PM
Firefox Cleaner mlpa Linux - Software 2 08-01-2009 05:14 PM
Text Cleaner nlavon Linux - Software 2 06-10-2008 06:32 AM
Good registry cleaner? colinstu General 5 07-24-2007 05:39 PM
MRU cleaner Ageless Linux - Software 0 04-26-2005 12:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration