[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

This User Gave Thanks to balajesuri For This 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'

This User Gave Thanks to Scrutinizer For This 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 06:57 AM..
This User Gave Thanks to stomp For This 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}'

These 2 Users Gave Thanks to rdrtx1 For This Post:
dae (08-10-2016), stomp (08-09-2016)
# 7  
Old 08-09-2016
@rdrtx1: Interesting solution. Took some seconds to get it Smilie
Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sed, Inline replacement of string with spaces baris35 Shell Programming and Scripting 6 08-25-2018 02:16 PM
Insert a single quote in front of a line in vi editor pokhraj_d Shell Programming and Scripting 3 09-05-2015 05:15 AM
How to insert file contents after nth occurrence of a string using sed? dimocn Shell Programming and Scripting 5 08-27-2014 09:23 PM
sed delete leading spaces in a .csv if not in a string gary_w Shell Programming and Scripting 4 07-06-2012 02:57 PM
Insert a break page after certain string using SED newbeee UNIX for Dummies Questions & Answers 7 12-06-2011 01:38 PM
insert predefine text in front and on a loop masayangbata Shell Programming and Scripting 2 02-12-2011 11:30 AM
help - sed - insert space between string of form XxxAxxBcx, without replacing the pattern frozensmilz Shell Programming and Scripting 1 12-22-2010 12:28 AM
Help with sed matching <tag1> newline spaces <tag2> and replace the value in the same string format mgharios Shell Programming and Scripting 5 05-07-2010 03:58 PM
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 09:43 AM
sed: replace string with another string (with spaces) zmfcat1 Shell Programming and Scripting 4 07-16-2009 06:37 AM
sed - remove spaces before 1rst occurence of string danmauer Shell Programming and Scripting 6 01-15-2009 07:23 PM
bash - add backslash in front of variables w/ spaces trey85stang Shell Programming and Scripting 3 11-05-2008 05:32 AM
Can I use sed to insert a string which has colon Jenny.palmy UNIX for Dummies Questions & Answers 2 04-28-2008 08:04 PM
sh, ksh: command to remove front spaces from a string? pseudocoder Shell Programming and Scripting 3 09-22-2007 08:57 AM
how to remove spaces in a string using sed. radhika Shell Programming and Scripting 4 06-02-2005 04:00 PM
All times are GMT -4. The time now is 04:47 PM.

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





Not a Forum Member?
Forgot Password?