LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-15-2010, 01:33 PM   #1
Karl Godt
Member
 
Registered: Mar 2010
Location: Kiel , Germany
Distribution: once:SuSE6.2,Debian3.1, aurox9.2+3,Mandrake?,DSL? then:W7st,WVHB, #!8.10.02,PUPPY4.3.1 now:Macpup
Posts: 314

Rep: Reputation: 45
How to Insert characters into a string in shellscripts


My problem is to insert one sign like space or ':' into a line or string.

The goal is to get a demiliter for `cut` .

For example : STR=ABCD
output should be echo $STR A B C D

I tried to do it with `tr A-Z "'A '-'Z '" and such but without success

I've read also about `sed` and `grep` :
http://unixhelp.ed.ac.uk/CGI/man-cgi?sed
http://www.linuxinfor.com/german/man1/sed.html
http://www.informatikserver.at/LHB/n...00000000000000

I guess a while read loop could do it but I don't know howto use it with ` expr length $STR`
 
Old 11-15-2010, 01:44 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
It should be simple using sed

Did you try sed 's/(.)/ \1/g;s/^ //'?
 
1 members found this post helpful.
Old 11-15-2010, 02:07 PM   #3
Karl Godt
Member
 
Registered: Mar 2010
Location: Kiel , Germany
Distribution: once:SuSE6.2,Debian3.1, aurox9.2+3,Mandrake?,DSL? then:W7st,WVHB, #!8.10.02,PUPPY4.3.1 now:Macpup
Posts: 314

Original Poster
Rep: Reputation: 45
Code:
# echo $STR | sed 's/(.)/ \1/g;s/^ //' 
sed: -e expression #1, char 12: invalid reference \1 on `s' command's RHS
sed syntax is very confusing to me

I have an onboard .html doc for regular expressions that isn't found in the net anymore.
Code:
sed s : stream editor substitute
/(.)  : all characters
/     : the needed space
\1    : escaped NUMBER ?
/     : closing the s command
g     : global

/^    : beginning
//    : nothing and closing the s command
I've put it in code tags for better reading
 
Old 11-15-2010, 02:12 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The parentheses must be escaped unless you use option -r (extended regular expressions):
Code:
sed -r 's/(.)/ \1/g;s/^ //'
sed 's/\(.\)/ \1/g;s/^ //'
Karl, maybe a good sed tutorial is worth reading: http://www.grymoire.com/Unix/Sed.html (see in particular the paragraph "Using \1 to keep part of the pattern")

Last edited by colucix; 11-15-2010 at 02:14 PM.
 
1 members found this post helpful.
Old 11-15-2010, 02:34 PM   #5
Karl Godt
Member
 
Registered: Mar 2010
Location: Kiel , Germany
Distribution: once:SuSE6.2,Debian3.1, aurox9.2+3,Mandrake?,DSL? then:W7st,WVHB, #!8.10.02,PUPPY4.3.1 now:Macpup
Posts: 314

Original Poster
Rep: Reputation: 45
# echo $STR | sed 's/(.)/ \0/g;s/^ //'
ABCD
# echo $STR | sed 's/(.)/ \ 1/g'
ABCD
# echo $STR | sed 's/(.)/ 1/g'
ABCD
# echo $STR | sed 's/./ 1/g'
1 1 1 1

ooops : a new reply

Code:
# echo $STR | sed -r 's/(.)/ \1/g'
 A B C D
#
my sed must be compilied with different default options

Thank you both !!
The sed -r was exactly what I needed !
 
  


Reply



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
How to insert characters by ascii code? J_Szucs Linux - General 14 06-17-2017 12:33 PM
How to insert a space between all characters espiesior Linux - General 2 04-12-2009 09:28 PM
Any tool to insert a newline each X characters in a file? neoAKiRAz Programming 5 05-09-2007 07:15 PM
using sed to insert lines with special characters disorderly Linux - Software 26 04-20-2006 05:30 PM
how to insert special characters set2004 Linux - Newbie 2 08-05-2004 10:50 AM

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

All times are GMT -5. The time now is 09:28 PM.

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