Fedora Linux Support Community & Resources Center
  #1  
Old 23rd May 2012, 03:53 PM
Adunaic's Avatar
Adunaic Online
Registered User
 
Join Date: Mar 2009
Location: Lancaster, UK
Posts: 883
linuxfirefox
yet another sed thread - find and replace

I have several nagios config files that are looking quite ugly as the tabbing/spacing is all over the place making it hard to read.

My first thought was to have sed remove any groupings of two or more spaces and replace them with a tab ('\t'). Most lines are then:

<tab> string <tab> string

However the strings are of varying length so one or two tabes are needed. Initially I singled out the tabs one by one but there are too many. My current sed script is:

Code:
s/ \+ /\t/g
s/\t\+/\t/g
s/\tuse/\tuse\t/g
s/\talias/\talias\t/g
s/\taddress/\taddress\t/g
s/\tmembers/\tmembers\t/g
s/\tname/\tname\t/g
s/\tparents/\tparents\t/g
Is there any way in sed to do something like:

if first string is greater than X characters long put one tab after it, if first string is greater than Y characters long put two tabs after it and so on?
Reply With Quote
  #2  
Old 23rd May 2012, 04:00 PM
DBelton's Avatar
DBelton Offline
Administrator
 
Join Date: Aug 2009
Posts: 6,612
linuxfirefox
Re: yet another sed thread - find and replace

have you looked at the unexpand command? That is what it is made for

man unexpand
Reply With Quote
  #3  
Old 23rd May 2012, 04:06 PM
Adunaic's Avatar
Adunaic Online
Registered User
 
Join Date: Mar 2009
Location: Lancaster, UK
Posts: 883
linuxfirefox
Re: yet another sed thread - find and replace

That cannot count a string though to decide how many tabs to insert
Reply With Quote
  #4  
Old 23rd May 2012, 04:22 PM
DBelton's Avatar
DBelton Offline
Administrator
 
Join Date: Aug 2009
Posts: 6,612
linuxfirefox
Re: yet another sed thread - find and replace

actually it does insert multiple tabs based on 8 spaces per tab. You can change that with the --tabs= parameter if you wish.
Reply With Quote
  #5  
Old 23rd May 2012, 04:43 PM
Adunaic's Avatar
Adunaic Online
Registered User
 
Join Date: Mar 2009
Location: Lancaster, UK
Posts: 883
linuxfirefox
Re: yet another sed thread - find and replace

Still not sure that it does what I want (bit confused after reading about sed).

I have however figured out how to get sed to do what I want:

Code:
#Remove all groups of 2 or more spaces and replace with a single tab
s/ \+ /\t/g
#Remove all groups of 2 or more tabs and replace with a single tab
s/\t\+/\t/g
#Find any match to tab string tab where the string is 1 - 7 chars long and add two tabs to the end.
s/\(^\t[[:graph:]]\{1,7\}\t\)/\1\t\t/g
#Find any match to tab string tab where the string is 8 - 17 chars long and add one tab to the end.
s/\(^\t[[:graph:]]\{8,17\}\t\)/\1\t/g
Reply With Quote
Reply

Tags
replace, sed, thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
linux find and replace in multiple files COKEDUDE Using Fedora 3 15th September 2011 04:09 PM
Using find with sed to replace variables in php files? Vector Programming & Packaging 2 5th November 2010 03:25 AM
I want to find and replace across multiple lines and do it recursively. reashlin Linux Chat 14 9th October 2008 02:42 PM
phpmyadmin how do you find > and replace? 105547111 Using Fedora 2 28th June 2007 02:07 PM


Current GMT-time: 00:07 (Wednesday, 22-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat