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.

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

Shell Programming and Scripting


Tags
perl, sed, shell script

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-09-2016   -   Original Discussion by dae
dae's Unix or Linux Image
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 Unix and Linux 08-09-2016   -   Original Discussion by dae
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 14 April 2018, 10:43 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 Unix and Linux 08-09-2016   -   Original Discussion by dae
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 26 April 2018, 12:34 AM EDT
Location: Amsterdam
Posts: 11,784
Thanks: 542
Thanked 3,431 Times in 3,025 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 Unix and Linux 08-09-2016   -   Original Discussion by dae
stomp's Unix or Linux Image
stomp stomp is offline
Registered User
 
Join Date: May 2016
Last Activity: 24 April 2018, 4:24 PM EDT
Posts: 213
Thanks: 10
Thanked 74 Times in 69 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 Unix and Linux 08-09-2016   -   Original Discussion by dae
dae's Unix or Linux Image
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 Unix and Linux 08-09-2016   -   Original Discussion by dae
rdrtx1's Unix or Linux Image
rdrtx1 rdrtx1 is offline Forum Advisor  
Registered Pusher
 
Join Date: Sep 2012
Last Activity: 25 April 2018, 6:44 PM EDT
Location: Houston, Texas, USA
Posts: 1,115
Thanks: 0
Thanked 405 Times in 383 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 Unix and Linux 08-09-2016   -   Original Discussion by dae
stomp's Unix or Linux Image
stomp stomp is offline
Registered User
 
Join Date: May 2016
Last Activity: 24 April 2018, 4:24 PM EDT
Posts: 213
Thanks: 10
Thanked 74 Times in 69 Posts
@rdrtx1: Interesting solution. Took some seconds to get it Linux
Sponsored Links
Closed

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
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 01:59 AM.