Home Man
Search
Today's Posts
Register

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

Tags
perl, sed, shell script, shell scripts

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 08-09-2016
[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.
# 2  
Old 08-09-2016
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)
# 3  
Old 08-09-2016
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
...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)
# 5  
Old 08-09-2016
Thanks to all of you, Balajesuri, Scrutinizer & Stomp !
# 6  
Old 08-09-2016
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)
# 7  
Old 08-09-2016
@rdrtx1: Interesting solution. Took some seconds to get it
👤 Login to reply

« Previous Thread | Next 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 a user input string after matched string in file tomislav91 Shell Programming and Scripting 13 5 Days Ago 03:34 AM
Insert a single quote in front of a line in vi editor pokhraj_d Shell Programming and Scripting 3 09-05-2015 04:15 AM
Insert String every n lines, resetting line counter at desired string Skonectthedots Shell Programming and Scripting 1 03-17-2015 09:49 AM
Insert varying length spaces between words mpacer Shell Programming and Scripting 1 06-24-2011 09:04 PM
Inserting newline in front of multi-character string verge Shell Programming and Scripting 7 04-20-2011 02:57 AM
insert spaces between characters with pure shell fubaya Shell Programming and Scripting 5 03-21-2011 11:51 PM
insert predefine text in front and on a loop masayangbata Shell Programming and Scripting 2 02-12-2011 10:30 AM
Insert a special character $ in place of extra spaces naree Shell Programming and Scripting 12 06-08-2010 12:46 PM
bash - add backslash in front of variables w/ spaces trey85stang Shell Programming and Scripting 3 11-05-2008 04:32 AM
sh, ksh: command to remove front spaces from a string? pseudocoder Shell Programming and Scripting 3 09-22-2007 07:57 AM


All times are GMT -4. The time now is 01:23 AM.

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