Insert space between characters using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert space between characters using sed
# 1  
Old 08-21-2010
Insert space between characters using sed

Input:
Code:
Youcaneasilydothisbyhighlightingyourcode.

Putting space after three characters.
Code:
You can eas ily dot his byh igh lig hti ngy our cod e.

How can i do this using sed?
# 2  
Old 08-21-2010
Code:
echo "Youcaneasilydothisbyhighlightingyourcode." | sed 's/\(.\{3\}\)/\1 /g'

This User Gave Thanks to kevintse For This Post:
# 3  
Old 08-22-2010
Or you can just use &:
Code:
sed 's/.\{3\}/& /g'

This User Gave Thanks to konsolebox For This Post:
# 4  
Old 08-22-2010
Thank you all.
# 5  
Old 08-22-2010
To prevent space from being added at the end:
Code:
#!/usr/bin/env gawk

{
	do {
		a = $0
		$0 = gensub(/([^[:blank:]]{3})([^[:blank:]])/, "\\1 \\2", "", a)
	} while (a != $0)

	print
}

... | gawk --re-interval -f script.awk

Or one-liner:
Code:
... | gawk --re-interval '{ do { a = $0; $0 = gensub(/([^[:blank:]]{3})([^[:blank:]])/, "\\1 \\2", "", a); } while (a != $0); } 1'

It's still consistent even if blank characters exist along or at the end of the string.

Last edited by konsolebox; 08-22-2010 at 02:10 AM..
# 6  
Old 08-22-2010
Or even:
Code:
sed 's/.../& /g'

Ow konsolebox already almost gave the same solution..

Last edited by Scrutinizer; 08-22-2010 at 02:09 AM..
# 7  
Old 08-22-2010
Code:
echo "Youcaneasilydothisbyhighlightingyourcode." |awk 'BEGIN{FS=OFS=""}{for (i=1;i<=NF;i++) {if (i%3==0) $i=$i " "}}1'

This User Gave Thanks to rdcwayx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Outputting characters after a given string and reporting the characters in the row below --sed

I have this fastq file: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGGGGGGGGGGGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCA +test-1 GGGGGGGGGGGGGGGGGCCGGGGGFF,EDFFGEDFG,@DGGCGGEGGG7DCGGGF68CGFFFGGGG@CGDGFFDFEFEFF:30CGAFFDFEFF8CAF;;8... (10 Replies)
Discussion started by: Xterra
10 Replies

2. Shell Programming and Scripting

Insert space in a word using sed

Hi all, I have a file that looks like this- ----------------------------- . . ATOM 8 O2' U A 5 135.452 109.687 7.148 1.00 48.99 A16S ATOM 9 C1' U A 5 135.282 111.512 5.641 1.00 48.99 A16S ATOM 10 N1 U A 5 134.647 112.595 ... (2 Replies)
Discussion started by: asmi_g
2 Replies

3. Shell Programming and Scripting

Help awk/sed: putting a space after numbers:to separate number and characters.

Hi Experts, How to sepearate the list digit with letters : with a space from where the letters begins, or other words from where the digits ended. file 52087mo(enbatl) 52049mo(enbatl) 52085mo(enbatl) 25051mo(enbatl) The output should be looks like: 52087 mo(enbatl) 52049... (10 Replies)
Discussion started by: rveri
10 Replies

4. Shell Programming and Scripting

SED - insert space at the beginning of line and multi replace command

hi I am trying to use SED to replace the line matching a pattern using the command sed 'pattern c\ new line ' <file1 >file 2 I got two questions 1. how do I insert a blank space at the beginning of new line? 2. how do I use this command to execute multiple command using the -e... (5 Replies)
Discussion started by: piynik
5 Replies

5. UNIX for Advanced & Expert Users

Insert 1 space using the command sed

Hi I want to use sed to insert one space after the 10'th character in every line. The lines are on this format: 2012-01-1012:30:55|7323456|65432 2011-02-0313:11:06|1223|3456 ...... ...... Does anyone know sed well enough to acomplish this? If there is any other way around this... (7 Replies)
Discussion started by: ic12
7 Replies

6. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

7. Shell Programming and Scripting

Insert text space

I have the following texts in a .txt file, 22/02 23/02 24/02 25/02 26/02 Bike speed (kmph) 004 004 004 004 004 22/02 23/02 24/02 25/02 26/02 Bike speed (kmph) 004 007 007 007 011 I am trying to export it to .csv by doing cat nic.txt > nic.csv 22/02 23/02 are date and this has to... (7 Replies)
Discussion started by: nicolethomson
7 Replies

8. Shell Programming and Scripting

help - sed - insert space between string of form XxxAxxBcx, without replacing the pattern

If the string is of the pattern XxxXyzAbc... The expected out put from sed has to be Xxx Xyz Abc ... eg: if the string is QcfEfQfs, then the expected output is Qcf Ef Efs. If i try to substitute the pattern with space then the sed will replace the character or pattern with space,... (1 Reply)
Discussion started by: frozensmilz
1 Replies

9. Shell Programming and Scripting

Insert space between two words

Hi, I need to insert space between words on my output in UNIX other than the single space given by the space bar on my keyboard, e.g when are you going. (There should be 4 spaces between each of these words) rather than when are you going Can anyone help me with... (3 Replies)
Discussion started by: divroro12
3 Replies

10. Shell Programming and Scripting

How to insert greek characters in to vi editor

Hi, I want to test a unix file by inserting greek characters in to vi editor. Can anyone please suggest how to insert greek characters in to vi editor. (2 Replies)
Discussion started by: DSDexter
2 Replies
Login or Register to Ask a Question