Insert space in a word using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert space in a word using sed
# 1  
Old 05-12-2017
Insert space in a word using sed

Hi all,

I have a file that looks like this-

-----------------------------
Code:
.
.
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   6.411  1.00106.22      A16S 
ATOM     11  C2    U A   5     135.070 112.783   7.722  1.00106.22      A16S 
ATOM     12  O2    U A   5     135.933 112.100   8.250  1.00106.22      A16S 
.
.

-----------------------------

The problem with the file is the lack of space between columns 10 and 11 in certain rows (underlined) which is making parsing a bit difficult.

I used sed to insert space between these fields using this command-

Code:
sed 's/1.00\([0-9]\).*\.[0-9]\{2\}/1.00 [0-9].*\.[0-9]\{2\}/' file.txt

but the output i got was-

--------------------------------
Code:
ATOM     12  O2    U A   5     135.933 112.100   8.250  1.00 [0-9].*.[0-9]{2}      A16S 
ATOM     12  O2    U A   5     135.933 112.100   8.250  1.00 [0-9].*.[0-9]{2}      A16S 
ATOM     12  O2    U A   5     135.933 112.100   8.250  1.00 [0-9].*.[0-9]{2}      A16S

---------------------------

I am relatively new to sed/awk. Please suggest a way to go about it.

Thanks,

Last edited by Scrutinizer; 05-12-2017 at 08:53 PM.. Reason: code tags
# 2  
Old 05-12-2017
Hi, try this adaptation:
Code:
sed 's/1\.00\([0-9]*\.[0-9]\{2\}\)/1.00 \1/' file


Last edited by Scrutinizer; 05-13-2017 at 02:07 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 05-13-2017
To make sure you don't substitute an arbitrary 1.00 somewhere on the line, and allow for field 10 to assume values other than 1.00, try
Code:
awk 'NF != 12 {T = $(NF-1); sub (/\.[0-9][0-9]/, "& ", T); sub ($(NF-1), T)} 1' file

This User Gave Thanks to RudiC 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

Insert Word After Match

Dear ALL, I have sample file : host.txt fullname: FreeBSD Host: server1 ip: 1.1.1.1 mask: 255.255.255.0 fullname: CentOS Host: server2 mask: 255.255.255.0 fullname: Fedora Host: server3 ip: 1.3.1.2 mask: 255.255.255.0 (2 Replies)
Discussion started by: gnulyn
2 Replies

2. Shell Programming and Scripting

Replacing a particular word with another word in all the xml's under a particular directory with sed

Hi Folks, Could you please advise what will be the SED command to replace a word in all xml's under a particular directory for example let say I rite now at the following below location $ cd /ter/rap/config now under config directory there will be lots of xml file , now my objective is to... (1 Reply)
Discussion started by: punpun66
1 Replies

3. Shell Programming and Scripting

How to insert a word into a text file?

Hi, I have a text file with one line having few words separated by space and I need to insert another word on "n"th column/field so that previous word should shift right (to n+1st column). how can I do that? It seems we can do using awk but unable to figure out. Please advise, thanks! ... (11 Replies)
Discussion started by: magnus29
11 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

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

7. 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

8. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

9. Shell Programming and Scripting

Insert space between characters using sed

Input: Youcaneasilydothisbyhighlightingyourcode. Putting space after three characters. You can eas ily dot his byh igh lig hti ngy our cod e. How can i do this using sed? (10 Replies)
Discussion started by: cola
10 Replies

10. Shell Programming and Scripting

insert word in each line of a file

can someone tell me how can I insert a word in front of each line in a file. i tried with sed but didn't managed yet. Is there another command or this one(sed) works? 10x anyway. (7 Replies)
Discussion started by: atticus
7 Replies
Login or Register to Ask a Question