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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [sed]: syntax to insert N spaces in front of a string
# 8  
Old 08-09-2016
Code:
n=10 ; echo "ABCD" | sed 's/^/'$(printf "%${n}s" "")'/'


Last edited by Scrutinizer; 08-10-2016 at 02:51 PM..
# 9  
Old 08-09-2016
Quote:
Originally Posted by dae
Thanks to all of you, Balajesuri, Scrutinizer & Stomp !
Hello dae,

Could you please try following too.
Code:
echo "abcd" | awk '{while(i<10){Q=Q? Q OFS:OFS;i++};print Q $0}'

Output will be as follows.
Code:
          abcd

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 10  
Old 08-10-2016
Rdrtx1's proposal is exactly what I was looking for ... but I am absolutely unable to understand it ! Smilie

if one of you has an idea ?

Thanks again,
# 11  
Old 08-10-2016
This...

Code:
sed 's/^/          /g'

and that...

Code:
n=10
sed 's/^/'$(printf "%${n}s")'/'

are basically the same. The only difference is that the second one creates the 10 spaces in a (bash) subshell. The command printf "%10s" creates exactly 10 spaces. I omitted the arg "". Syntactically not correct, but it works too.

Last edited by stomp; 08-10-2016 at 07:00 AM..
This User Gave Thanks to stomp For This Post:
# 12  
Old 08-10-2016
Quote:
Originally Posted by stomp
This...
Code:
sed 's/^/          /g'

and that...
Code:
n=10
sed 's/^/'$(printf "%${n}s" "")'/'

are basically the same. The only difference is that the second one creates the 10 spaces in a (bash) subshell. The command printf "%10s" "" creates exactly 10 spaces.
Thanks stomp for explanation. Just wanted to add a bit here. It creates 10 spaces in subshell by (printf) and substituted the starting of line by doing sed 's/^/' with those 10 spaces.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 13  
Old 08-10-2016
When I try the command:
Code:
n=10 ; echo "ABCD" | sed 's/^/'$(printf "%${n}s" "")'/'

on OS X El Capitan Version 10.11.6 using ksh version 93u+ 2012-08-01 and with bash version 3.2.57(1)-release (x86_64-apple-darwin15) I get a syntax error similar to:
Code:
sed: 1: "s/^/": unterminated substitute in regular expression

which I believe is what is required by the standards since the output of the command substitution is not quoted.

And, while it is true that some shells will manufacture an empty string argument or a zero numeric argument to match printf format specifiers, that behavior is not required by the standards and will result in syntax errors in some shells with a built-in printf utility and in some stand-alone implementations of the printf utility.

Either of the following should work with any standards conforming shell, sed, and printf utilities:
Code:
n=10 ; echo "ABCD" | sed 's/^/'"$(printf "%${n}s" "")"'/'

or:
Code:
n=10 ; echo "ABCD" | sed 's/^/'"$(printf '%$*s' "$n" "")"'/'

or, if the string you want to print is in a variable instead of being read from standard input:
Code:
n=10; string="ABCD"; printf "%$ns%s" "" "$string"

or:
Code:
n=10; string="ABCD"; printf '%*s%s' "$n" "" "$string"

In all of these cases, as already explained, the commands:
Code:
printf '%Ns" "string"
printf '%*s" N "string"

where N is a non-negative integer will print the string specified by the last operand right justified in a field that is N characters wide with leading space fill. If the string specified by the last operand is more than N characters, it will not be truncated. If you wanted a field of exactly 6 characters no matter how long the given string is, you can specify both a minimum and a maximum field width. And, you can also specify left justified text instead of right justified text. The following code:
Code:
printf 'X%6.6sX\n' "ABCD"
printf 'X%-6.6sX\n' "ABCD"
printf 'X%6.6sX\n' "abcdefgh"

producing the output:
Code:
X  ABCDX
XABCD  X
XabcdefX

This User Gave Thanks to Don Cragun For This Post:
# 14  
Old 08-10-2016
What a "big jump" in my knowledge thanks to all your contributions ... Don, I got the same error for myself but I decided to do not bother all of you more with my problem !

Thanks again for your help and the time spent !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed, Inline replacement of string with spaces

Hello, Just surfed on the web for probable answers but could not get them working. I wish to replace the string containing spaces by another phrase but below answers did not work. My string is: PAIN & GAIN I wish to convert it to: P&G I just need it working with sed with function -i ... (6 Replies)
Discussion started by: baris35
6 Replies

2. Shell Programming and Scripting

Insert a single quote in front of a line in vi editor

Hello Gurus, I wanted to put a single quote in every where starting with /oradata, and at the end with .dbf. For example I have one line as below: alter database rename datafile /oradata/test.dbf to /oradata_new/test.dbf I wanted as below alter database rename datafile '/oradata/test.dbf' to... (3 Replies)
Discussion started by: pokhraj_d
3 Replies

3. Shell Programming and Scripting

sed delete leading spaces in a .csv if not in a string

Solaris, ksh I have a .csv file I am trying to clean up before loading into the database. The file contains comma separated columns that have leading spaces which I need to remove. The trouble is, some columns that should not be touched are strings which happen to have the same pattern in them. ... (4 Replies)
Discussion started by: gary_w
4 Replies

4. UNIX for Dummies Questions & Answers

Insert a break page after certain string using SED

Hi: I have 2 files: teststring.txt and a tempfile.txt teststring file contains: s/Primary Ins./\n1/g I'm trying to search for "Primary Ins." string in tempfile. For every "Primary Ins." string that is found, a new line is inserted and put in number 1. Then, write out the newfile... (7 Replies)
Discussion started by: newbeee
7 Replies

5. Shell Programming and Scripting

insert predefine text in front and on a loop

Hi Experts, I wish to insert predefined text in front of every line and this needs to be in a loop because it is always expanding. Before : 11111111 22222222 33333333 44444444 55555555 77777777 88888888 00000000 To be Inserted : a= b= (2 Replies)
Discussion started by: masayangbata
2 Replies

6. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

7. Shell Programming and Scripting

sed: replace string with another string (with spaces)

Hi I have an XML file with strings XABCD, XEFGHX and XIJKLX. I would like to replace XABCDX with "This is the first string", XEFGHX with "This is the second string" and XIJKLX with "This is the third string". What is the best way to implement this? Should I have a file with the data that is... (4 Replies)
Discussion started by: zmfcat1
4 Replies

8. UNIX for Dummies Questions & Answers

Can I use sed to insert a string which has colon

Hi, all, I wonder if I can use sed to insert a string which has a colon. I have a txt file a.txt like the following TRAIN/DR1/FCJF0/SI1027.MFC TRAIN/DR1/FCJF0/SI1657.MFC I want to insert a string C:/TIMIT/TIMIT at the begining of each line. I use the commond: TIM=C\:/TIMIT/TIMIT... (2 Replies)
Discussion started by: Jenny.palmy
2 Replies

9. Shell Programming and Scripting

sh, ksh: command to remove front spaces from a string?

dear pro-coders, is there any command out there that takes out the front spaces from a string? sample strings: 4 members 5 members 3 members but it has to be like so: 4 members 5 members 3 members (3 Replies)
Discussion started by: pseudocoder
3 Replies

10. Shell Programming and Scripting

how to remove spaces in a string using sed.

Hello, I have the following to remove spaces from beginning and end of a string. infile=`echo "$infilename" | sed 's/^ *//;s/ *$//` How do I modify the above code to remove spaces from beginning, end and in the middle of the string also. ex: ... (4 Replies)
Discussion started by: radhika
4 Replies
Login or Register to Ask a Question