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: 3 October 2014, 8:50 AM EDT
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 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: 24 October 2014, 10:06 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,237
Thanks: 121
Thanked 453 Times in 412 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: 3 October 2014, 8:50 AM EDT
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 05-10-2012
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 24 October 2014, 12:06 PM EDT
Location: Amsterdam
Posts: 9,546
Thanks: 285
Thanked 2,426 Times in 2,174 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: 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 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 12:07 PM.