Awk/sed - add space between dot and letter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk/sed - add space between dot and letter
# 1  
Old 01-22-2013
Awk/sed - add space between dot and letter

I need to change
Code:
[A-Z].[A-Z]

into
Code:
[A-Z]. [A-Z]

so that e.g.
Code:
A.J

becomes
Code:
A. J

I have tried
Code:
sed 's/[A-Z]\.[A-Z]/[A-Z]\.\ [A-Z]/g'

but that didn't work.
# 2  
Old 01-22-2013
Code:
sed 's/\([A-Z]\)\.\([A-Z]\)/\1. \2/g'

\1 is the 1st matching \(...\) etc.
This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 01-23-2013
Hi,

you can use this also.
Code:
sed "s/\./\. /g" $file

Reason: replace the "." with ". "

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 01-23-2013 at 09:30 AM..
# 4  
Old 01-23-2013
But here the task is to replace "." when prefixed and followed by an uppercase character.
Test:
Code:
echo ...A.1.B.C.D.E.FX. | sed 's/\([A-Z]\)\.\([A-Z]\)/\1. \2/g'

...A.1.B. C.D. E.FX.
You further see how the /g modifier works. It repeats the expression on the remainder of the line, after the part that was covered by the previous expression.
If the repetition should include the last character of the previous expression, things become complicated. In this case one can run the whole statement twice:
Code:
echo ...A.1.B.C.D.E.FX. | sed 's/\([A-Z]\)\.\([A-Z]\)/\1. \2/g; s/\([A-Z]\)\.\([A-Z]\)/\1. \2/g'

...A.1.B. C. D. E. FX.
# 5  
Old 01-23-2013
Hey,

you can use this also.

Code:
#iterating the sed command up to end
echo ...A.1.B.C.D.E.FX. | sed -e :a -e 's/\([A-Z]\{1\}\)\.\([A-Z]\{1\}\)/\1\. \2/;ta'
...A.1.B. C. D. E. FX.


Last edited by Scott; 01-24-2013 at 07:33 AM.. Reason: Code tags
This User Gave Thanks to srinivas matta For This Post:
# 6  
Old 01-24-2013
Thanks for the hint!
The explicit loop is certainly more efficient.
Code:
echo ...A.1.B.C.D.E.FX. | sed -e :a -e 's/\([A-Z]\)\.\([A-Z]\)/\1. \2/;ta'

# 7  
Old 01-24-2013
Nice solution indeed. But you don't necessarily need the two expressions (may depend on the sed version used?),
Code:
$ echo ...A.1.B.C.D.E.FX. | sed ':a;s/\([A-Z]\)\.\([A-Z]\)/\1. \2/;ta'
...A.1.B. C. D. E. FX.

will do as well.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace space in column with letter for several rows

I have a pbd file, which has the following format: TITLE Protein X MODEL 1 ATOM 1 N PRO 24 45.220 71.410 43.810 1.00 0.00 ATOM 2 H1 PRO 24 45.800 71.310 42.000 1.00 0.00 TER ENDMDL Column 22 is the chain... (5 Replies)
Discussion started by: Egy
5 Replies

2. Shell Programming and Scripting

awk to add space between text

I am trying to use awk to add a space in a string of text. My awk is close but does not produce the desured output. Thank you :). input washington aveLansing, MI washington streetLansing, MI Desired output washington ave Lansing, MI washington street Lansing, MI awk -F"\t"... (9 Replies)
Discussion started by: cmccabe
9 Replies

3. Shell Programming and Scripting

sed add space X times

Pattern: Mary walks at the park every day with her children sed 's/$/ /' will make it add 1 space at the end (trailing) I want to add X ($VARIABLE) number of spaces (which comes from RANDOM) i.e. VARIABLE='14' then it will do: sed 's/$/ /' = 14 spaces added at the... (10 Replies)
Discussion started by: holyearth
10 Replies

4. UNIX for Dummies Questions & Answers

Try to use awk or sed to get available disk space

Guys this is my 1st post so please excuse if i am asking something already answered I am trying to write a script to send an alert whenever any of my servers runs low on disk space, i am getting the following output 9 Dir(s) 6,611,517,440 bytes free I need to just get 6,611,517,440 from... (2 Replies)
Discussion started by: LinuxNovice83
2 Replies

5. Shell Programming and Scripting

Dot operator and space, Parameter not set

Hi, i have this script setenv.sh if then echo "is empty" fi echo "done" The following is the result when i run the script from command without and with a dot and space operator $ setenv.sh is empty done $ . setenv.sh sh: VAR_1: Parameter not set. $ It's our standard to run... (5 Replies)
Discussion started by: ysrini
5 Replies

6. Shell Programming and Scripting

Remove letter from $1 using awk or sed

I have a file: 575G /local/mis/SYBDUMP I want to remove the G, K, M or T so I can use $1 in awk or sed to do math. I want to end up with a file containing: 575 /local/mis/SYBDUMP It should not matter how small or large the numeric numbers are so if 2, 3, 4, or 5 digits etc I want to see... (9 Replies)
Discussion started by: tamvgi
9 Replies

7. UNIX for Advanced & Expert Users

Sed - add text to start of line if 1st char anything but space

Problem: I have a lot of files, the files first line should always have 4 spaces before any text. Occasionally some of the files will miss the leading spaces and it's a problem. This is only in the first line. So if there are 4 spaces then text, do nothing. If there are not 4 spaces, add 4... (2 Replies)
Discussion started by: Vryali
2 Replies

8. Shell Programming and Scripting

Add white space to the end of a line with sed

Im trying to add 5 blank spaces to the end of each line in a file in a sed script. I can figure out who o put the spaces pretty much anywhere else but at the end. thanks Karl (7 Replies)
Discussion started by: karlanderson
7 Replies

9. Shell Programming and Scripting

using sed to add letter after variable

Hi All, I need to have a sed command insert the letter i at the end of some lines in a file. Problem is that where I want to add the i is after variables that change in each file eg, tsal314384' tsal315386' tsal317392' I need to have it like this ... (2 Replies)
Discussion started by: outthere_3
2 Replies

10. UNIX for Dummies Questions & Answers

How to add a space after an Upper case letter

Hi I have two questions. 1. how to convert "EverythingIsFine" to "Everything Is Fine" in a txt file. 2. how to convert everything to upper case letter and reverse, I hope there is a general purpose script for this. (1 Reply)
Discussion started by: onthetopo
1 Replies
Login or Register to Ask a Question