Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Insert space in a word using sed

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-12-2017
asmi_g asmi_g is offline
Registered User
 
Join Date: May 2017
Last Activity: 16 May 2017, 8:10 PM EDT
Posts: 1
Thanks: 2
Thanked 0 Times in 0 Posts
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 07:53 PM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 05-12-2017
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 17 August 2017, 4:58 PM EDT
Location: Amsterdam
Posts: 11,511
Thanks: 497
Thanked 3,327 Times in 2,935 Posts
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 01:07 AM..
The Following User Says Thank You to Scrutinizer For This Useful Post:
asmi_g (05-16-2017)
Sponsored Links
    #3  
Old Unix and Linux 05-13-2017
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 18 August 2017, 10:29 AM EDT
Location: Aachen, Germany
Posts: 11,171
Thanks: 287
Thanked 3,442 Times in 3,171 Posts
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

The Following User Says Thank You to RudiC For This Useful Post:
asmi_g (05-16-2017)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to insert a word into a text file? magnus29 Shell Programming and Scripting 11 11-16-2013 03:43 AM
Insert text space nicolethomson Shell Programming and Scripting 7 02-28-2012 05:32 AM
Insert space between two words divroro12 Shell Programming and Scripting 3 01-14-2010 02:51 PM
to insert some word somewhere in the line with shell (or perl) tip78 Shell Programming and Scripting 12 09-20-2009 04:31 AM
insert word in each line of a file atticus Shell Programming and Scripting 7 03-23-2006 01:15 AM



All times are GMT -4. The time now is 10:33 AM.