Insert 1 space using the command sed | Unix Linux Forums | UNIX for Advanced & Expert Users

  Unix/Linux Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Insert 1 space using the command sed

UNIX for Advanced & Expert Users


Tags
linux, sed

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-10-2012
ic12 ic12 is offline
Registered User
 
Join Date: May 2012
Last Activity: 15 May 2012, 9:41 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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:


Code:
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 without using sed I would also be very thankful.


Moderator's Comments:
Please use code tags, thanks!

Last edited by zaxxon; 05-10-2012 at 04:54 AM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 05-10-2012
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 30 December 2014, 10:04 PM EST
Posts: 3,059
Thanks: 33
Thanked 659 Times in 638 Posts

Code:
awk '{print substr($0,1,10)" "substr($0,11,length($0))}' input.txt
2012-01-10 12:30:55|7323456|65432
2011-02-03 13:11:06|1223|3456

---------- Post updated at 02:28 PM ---------- Previous update was at 02:26 PM ----------


Code:
$ sed 's/\(..........\)\(.*\)/\1 \2/' input.txt
2012-01-10 12:30:55|7323456|65432
2011-02-03 13:11:06|1223|3456

---------- Post updated at 02:29 PM ---------- Previous update was at 02:28 PM ----------


Code:
$ sed 's/\(.\{10\}\)\(.*\)/\1 \2/' input.txt
2012-01-10 12:30:55|7323456|65432
2011-02-03 13:11:06|1223|3456

Sponsored Links
    #3  
Old Unix and Linux 05-10-2012
ic12 ic12 is offline
Registered User
 
Join Date: May 2012
Last Activity: 15 May 2012, 9:41 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you so much. All that worked perfect.
    #4  
Old Unix and Linux 05-10-2012
zaxxon's Unix or Linux Image
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 27 March 2015, 11:45 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,266
Thanks: 128
Thanked 456 Times in 415 Posts
One more Unix or Linux Image


Code:
# sed 's/........../& /' infile

Sponsored Links
    #5  
Old Unix and Linux 05-10-2012
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 30 December 2014, 10:04 PM EST
Posts: 3,059
Thanks: 33
Thanked 659 Times in 638 Posts

Code:
 
$ perl -lane '$_=~s/^(\d{4}-\d{2}-\d{2})(.*)/$1 $2/;print $_' input.txt
2012-01-10 12:30:55|7323456|65432
2011-02-03 13:11:06|1223|3456

---------- Post updated at 02:44 PM ---------- Previous update was at 02:36 PM ----------


Code:
 
$ perl -lane '$_=~s/^(.{10})(.*)/$1 $2/;print $_' input.txt
2012-01-10 12:30:55|7323456|65432
2011-02-03 13:11:06|1223|3456

Sponsored Links
    #6  
Old Unix and Linux 05-10-2012
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 27 March 2015, 5:31 PM EDT
Location: Amsterdam
Posts: 10,060
Thanks: 323
Thanked 2,630 Times in 2,351 Posts
Yet one more:

Code:
sed 's/./& /10'

Sponsored Links
    #7  
Old Unix and Linux 05-18-2012
complex.invoke's Unix or Linux Image
complex.invoke complex.invoke is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 24 July 2014, 5:53 AM EDT
Location: BeiJing China
Posts: 236
Thanks: 6
Thanked 23 Times in 8 Posts

Code:
sed 's/\([0-9]\{2\}:\)/ \1/'

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Insert text space nicolethomson Shell Programming and Scripting 7 02-28-2012 05:32 AM
insert white space between numbers MDeBiasse UNIX for Dummies Questions & Answers 2 02-13-2012 06:02 PM
help - sed - insert space between string of form XxxAxxBcx, without replacing the pattern frozensmilz Shell Programming and Scripting 1 12-21-2010 11:28 PM
Insert space between characters using sed cola Shell Programming and Scripting 10 08-22-2010 11:53 AM
Insert space between two words divroro12 Shell Programming and Scripting 3 01-14-2010 02:51 PM



All times are GMT -4. The time now is 07:03 AM.