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.

[sed]: syntax to insert N spaces in front of a string


👤 Login to reply

 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-09-2016
dae dae is offline
Registered User
 
Join Date: Aug 2016
Last Activity: 8 March 2017, 7:42 AM EST
Location: Paris
Posts: 28
Thanks: 41
Thanked 0 Times in 0 Posts
[sed]: syntax to insert N spaces in front of a string

Dear all,

I would like to insert N blankspaces in front of a string using sed command

To give an example (N=10), I tried that code:

Code:
[x004191a@xsnl11p317a tmp]$ echo "abcd" | sed 's/^/ \{10,\}&/'

but I failed, by obtaining that result:

Code:
 {10,}abcd

Any help would be greatly appreciated,

Thanks in advance,

Didier.
Sponsored Links
    #2  
Old 08-09-2016
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 21 June 2018, 3:02 AM EDT
Location: India
Posts: 1,984
Thanks: 31
Thanked 575 Times in 548 Posts
Code:
x="ABCD"
n=10
n=$((n + ${#x}))
printf "%${n}s" $x

The Following User Says Thank You to balajesuri For This Useful Post:
dae (08-09-2016)
Sponsored Links
    #3  
Old 08-09-2016
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 19 July 2018, 1:50 AM EDT
Location: Amsterdam
Posts: 11,853
Thanks: 546
Thanked 3,469 Times in 3,057 Posts
The replacement part (right hand side) of sed's substitution command does not use regex, so with pure sed you can only type 10 spaces.
Code:
sed 's/^/          /g'

The Following User Says Thank You to Scrutinizer For This Useful Post:
dae (08-09-2016)
    #4  
Old 08-09-2016
stomp stomp is offline
Registered User
 
Join Date: May 2016
Last Activity: 17 July 2018, 7:35 AM EDT
Posts: 218
Thanks: 10
Thanked 76 Times in 71 Posts
...some glibberish with perl...
Code:
 echo -e "lorem\nipsum\ndolor\nsit\namet" | { N=10; perl -pe "s/^/' 'x$N/e" ; }

Code:
          lorem
          ipsum
          dolor
          sit
          amet


Last edited by stomp; 08-10-2016 at 05:57 AM..
The Following User Says Thank You to stomp For This Useful Post:
dae (08-09-2016)
Sponsored Links
    #5  
Old 08-09-2016
dae dae is offline
Registered User
 
Join Date: Aug 2016
Last Activity: 8 March 2017, 7:42 AM EST
Location: Paris
Posts: 28
Thanks: 41
Thanked 0 Times in 0 Posts
Thanks to all of you, Balajesuri, Scrutinizer & Stomp !
Sponsored Links
    #6  
Old 08-09-2016
rdrtx1 rdrtx1 is offline Forum Advisor  
Registered Pusher
 
Join Date: Sep 2012
Last Activity: 31 May 2018, 9:41 AM EDT
Location: Houston, Texas, USA
Posts: 1,142
Thanks: 0
Thanked 421 Times in 398 Posts
Code:
n=10 ; echo "ABCD" | sed ':lbl; /^ \{'$n'\}/! {s/^/ /;b lbl}'

The Following 2 Users Say Thank You to rdrtx1 For This Useful Post:
dae (08-10-2016), stomp (08-09-2016)
Sponsored Links
    #7  
Old 08-09-2016
stomp stomp is offline
Registered User
 
Join Date: May 2016
Last Activity: 17 July 2018, 7:35 AM EDT
Posts: 218
Thanks: 10
Thanked 76 Times in 71 Posts
@rdrtx1: Interesting solution. Took some seconds to get it
Sponsored Links
👤 Login to reply

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
Using sed to replace a string in file with a string in a variable that contains spaces vivelafete Shell Programming and Scripting 2 11-24-2009 08:43 AM
sed: replace string with another string (with spaces) zmfcat1 Shell Programming and Scripting 4 07-16-2009 05:37 AM
Can I use sed to insert a string which has colon Jenny.palmy UNIX for Dummies Questions & Answers 2 04-28-2008 07:04 PM
sh, ksh: command to remove front spaces from a string? pseudocoder Shell Programming and Scripting 3 09-22-2007 07:57 AM
how to remove spaces in a string using sed. radhika Shell Programming and Scripting 4 06-02-2005 03:00 PM



All times are GMT -4. The time now is 03:23 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?