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

  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 Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 05-10-2012
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 19 March 2014, 7:13 AM EDT
Posts: 3,039
Thanks: 33
Thanked 652 Times in 631 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 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 05-10-2012
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 17 April 2014, 2:52 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,160
Thanks: 108
Thanked 428 Times in 398 Posts
One more


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

Sponsored Links
    #5  
Old 05-10-2012
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 19 March 2014, 7:13 AM EDT
Posts: 3,039
Thanks: 33
Thanked 652 Times in 631 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 05-10-2012
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 17 April 2014, 8:40 AM EDT
Location: Amsterdam
Posts: 8,874
Thanks: 230
Thanked 2,141 Times in 1,922 Posts
Yet one more:

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

Sponsored Links
    #7  
Old 05-18-2012
complex.invoke's Avatar
complex.invoke complex.invoke is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 11 April 2014, 6:29 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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 11:27 AM.