LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-24-2013, 09:08 PM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,084

Rep: Reputation: 287Reputation: 287Reputation: 287
sed - express strings of dots


These are contrived examples ...
Code:
st="abcdefghijklmnopqrstuvwxyz"
sed 's/\(.......\)/\1\n/g' <<< $st 
sed 's/\(..........\)/\1\n/g' <<< $st 
sed 's/\(.............\)/\1\n/g' <<< $st
All three seds produce the expected results.

Question: how may the sed be written to parameterize the number of dots?
Is there a shorthand to express a string of 13 dots as '.'{13} or somesuch?
Is there a shorthand to express a string of n dots as '.'{$n} or somesuch?

Daniel B. Martin
 
Old 03-24-2013, 09:22 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,421

Rep: Reputation: 597Reputation: 597Reputation: 597Reputation: 597Reputation: 597Reputation: 597
Sure. You can either introduce the "[" and "]" characters with "\" to tell sed that they are special
Code:
sed -e 's/\(.\{13\}\)/\1\n/'
or tell sed to use extended regular expressions, which will make both the parentheses and curly brackets special unless escaped by a backslash.
Code:
sed -r -e 's/(.{13})/\1\n/'
All of the features of extended regular expressions are available in standard expressions. It's just that the need to use backslashes to introduce some characters (i.e., mark them as special) in a standard expression reverses to a need to escape them (mark them as literal) in an extended expression.
 
1 members found this post helpful.
Old 03-24-2013, 09:45 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,084

Original Poster
Rep: Reputation: 287Reputation: 287Reputation: 287
Quote:
Originally Posted by rknichols View Post
Sure. You can either introduce the "[" and "]" characters with "\" to tell sed that they are special
Code:
sed -e 's/\(.\{13\}\)/\1\n/'
or tell sed to use extended regular expressions, which will make both the parentheses and curly brackets special unless escaped by a backslash.
Code:
sed -r -e 's/(.{13})/\1\n/'
Thank you, rknichols, this is precisely what is needed.

I generalized your sample code this way:
Code:
echo; echo "Method #1 of LQ Member rknichols"
nc=5
st="abcdefghijklmnopqrstuvwxyz"
sed -e 's/\(.\{'$nc'\}\)/\1\n/g' <<< $st

echo; echo "Method #2 of LQ Member rknichols"
nc=5
st="abcdefghijklmnopqrstuvwxyz"
sed -r -e 's/(.{'$nc'})/\1\n/g' <<< $st
... and it produced the desired results ...
Code:
Method #1 of LQ Member rknichols
abcde
fghij
klmno
pqrst
uvwxy
z

Method #2 of LQ Member rknichols
abcde
fghij
klmno
pqrst
uvwxy
z
SOLVED!

Daniel B. Martin

Last edited by danielbmartin; 03-24-2013 at 10:13 PM. Reason: Improved code
 
  


Reply

Tags
sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] sed help. Search and replace multiple strings with one command. dbrazeau Programming 4 02-13-2013 11:45 AM
[SOLVED] How to use sed with some strings robertjinx Linux - Server 3 12-10-2012 08:29 AM
[Cygwin, sed] Using filenames as both files and search strings within sed lingh Linux - Newbie 5 10-20-2012 10:38 AM
suing sed to remove certain list of strings from a file ??? HuMan-BiEnG Linux - Distributions 11 10-13-2010 09:04 AM
Help with sed - replacing strings thulley Linux - Software 4 08-22-2006 10:07 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration