[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
# 15  
Old 08-10-2016
Quote:
Originally Posted by dae
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 !
If someone suggests some code for you and it doesn't work, please post details about what operating system and shell you're using, show us the exact command that you used, and show us the exact diagnostic messages you got from running that code. You are not bothering us when you point out that something suggested doesn't work for you. (Unless the suggested code explicitly stated that it would only work in certain environments or the thread requested code that would work for a specific environment different from yours.) We might learn that there was a typo in the suggested code. We might find that the suggestion wasn't tested and won't work anywhere. Or, we might find that the suggestion works on some operating systems or with some shells, but won't work on others. (This is why it is ALWAYS a good idea to tell us in the first post in every thread what operating environment (the operating system [including the release] and the shell [including the version] you're using.)

We all learn when we find out that code that works on one system or shell doesn't work on others. I would be very interested to learn what operating system and shell were being used where the command:
Code:
n=10 ; echo "ABCD" | sed 's/^/'$(printf "%${n}s" "")'/'

worked successfully and did not produce a diagnostic message. I know from recent discussions in the group that maintains the POSIX standards that some versions of bash (and maybe other shells as well) quoted the results from a command substitution due to a misunderstanding of the text in the standard; but I think the latest versions of bash would now produce a diagnostic similar to the one I mentioned in post #13. (And the text describing shell quote processing will be cleaned up in the next revision of the standard to remove the confusion that resulted in that bash behavior.)

And, when I see that people here are confused by the wording in the man pages (at least the parts of some of the man pages that have been copied from the standards), sometimes a change magically appears in the next technical corrigenda or next revision of the standard to clarify the text. (In addition to volunteering as a moderator in this forum, helping to maintain the POSIX standards is another one of my unpaid jobs.)
These 2 Users Gave Thanks to Don Cragun For This Post:
# 16  
Old 09-01-2016
Dear all,

My aim: I want to add "spaces" in front of each line of a "variable content", V, using awk !

Example:

Content of "V":

Code:
[x004191a@xsnl11p317a tmp]$ echo "$v"
HOST_CDH_ID
STD_STDOID_CD
FUNCENV_CDH_ID
METDVERS_OID_ID
METDVERS_NAME_LB
VERSFLDR_MATROID_CD
VERSFLDR_PRMRFORMOID_CD
VERSFLDR_STDEVNTOID_CD
VERSFLDR_MAND_FL
VERSFLDR_STDEVNTDEFNAME_LB
VERSFLDR_STDEVNTDEFREPT_LB
VERSFLDR_STDEVNTDEFTYPE_LB
VERSFLDR_ORDRNUMB_NO
VERSFLDR_ATTRVERSFOLD_JS
VERSFLDR_CDH_ID
[x004191a@xsnl11p317a tmp]$


I want to add 10 spaces:

Code:
[x004191a@xsnl11p317a tmp]$ var=$(echo -e "$v"|awk -v val1="10" '{printf("%10s%s\n", " ", $0) }')
[x004191a@xsnl11p317a tmp]$ echo "$var"
          HOST_CDH_ID
          STD_STDOID_CD
          FUNCENV_CDH_ID
          METDVERS_OID_ID
          METDVERS_NAME_LB
          VERSFLDR_MATROID_CD
          VERSFLDR_PRMRFORMOID_CD
          VERSFLDR_STDEVNTOID_CD
          VERSFLDR_MAND_FL
          VERSFLDR_STDEVNTDEFNAME_LB
          VERSFLDR_STDEVNTDEFREPT_LB
          VERSFLDR_STDEVNTDEFTYPE_LB
          VERSFLDR_ORDRNUMB_NO
          VERSFLDR_ATTRVERSFOLD_JS
          VERSFLDR_CDH_ID
[x004191a@xsnl11p317a tmp]$


Now, the number of spaces I want to add is a variable:

Code:
[x004191a@xsnl11p317a tmp]$ var=$(echo -e "$v"|awk -v val1="10" '{printf("%val1%s\n", " ", $0) }')
[x004191a@xsnl11p317a tmp]$ echo "$var"
%val1
%val1
%val1
%val1
%val1
%val1
%val1
%val1
%val1
%val1
%val1
%val1
%val1
%val1
%val1
[x004191a@xsnl11p317a tmp]$

Thanks a lot for your attention and your help ...

Didier.
# 17  
Old 09-01-2016
Code:
#!/bin/ksh

typeset -i padN=10
v='HOST_CDH_ID
STD_STDOID_CD
FUNCENV_CDH_ID
METDVERS_OID_ID'

pad=$(printf "%0${padN}d" |tr "0" " ")
var=$(echo -e "$v"|awk -v pad="${pad}"  '{printf("%s%s\n", pad, $0) }')

echo "${var}"

This User Gave Thanks to vgersh99 For This Post:
# 18  
Old 09-01-2016
Quote:
Originally Posted by rdrtx1
Code:
n=10 ; echo "ABCD" | sed ':lbl; /^ \{'$n'\}/! {s/^/ /;b lbl}'

A Unix sed needs a line end after a lable and after { } braces. And a line break or semicolon before a }.
A multi-line sed script can be
Code:
sed 'line1
line2
line3'

or packed into one line
Code:
sed -e 'line1' -e 'line2' -e 'line3'

A packed variant for Unix sed looks like this
Code:
n=10 ; echo "ABCD" | sed -e ':lbl' -e '/^ \{'$n'\}/b' -e 's/^/ /' -e 'b lbl'

The b command (branch without a lable) goes to the next cycle, like the next command in awk. The n command is similar but does not increase the line number, as you can see with the = command and some more input lines:
Code:
n=10 ; </etc/group sed -e '=;:lbl' -e '/^ \{'$n'\}/b' -e 's/^/ /' -e 'b lbl'

Code:
n=10 ; </etc/group sed -e '=;:lbl' -e '/^ \{'$n'\}/n' -e 's/^/ /' -e 'b lbl'

Last but not least, this does not always insert 10 spaces. Instead it ensures there are 10 spaces; if necessary it inserts some.

Last edited by MadeInGermany; 09-01-2016 at 04:48 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 19  
Old 09-01-2016
You could also try one of these three portable ways to do it. (Note that the behavior of echo -e is not defined by the standards and varies from shell to shell and operating system to operating system when the 1st argument has a minus sign as its first character (as in -e) and whenever any of its arguments contains a backslash character.
Code:
#!/bin/ksh
v='HOST_CDH_ID
STD_STDOID_CD
FUNCENV_CDH_ID
METDVERS_OID_ID
METDVERS_NAME_LB
VERSFLDR_MATROID_CD
VERSFLDR_PRMRFORMOID_CD
VERSFLDR_STDEVNTOID_CD
VERSFLDR_MAND_FL
VERSFLDR_STDEVNTDEFNAME_LB
VERSFLDR_STDEVNTDEFREPT_LB
VERSFLDR_STDEVNTDEFTYPE_LB
VERSFLDR_ORDRNUMB_NO
VERSFLDR_ATTRVERSFOLD_JS
VERSFLDR_CDH_ID'

val1=10
var=$(printf '%s\n' "$v" | awk -v val1="$val1" '{printf("%*s%s\n", val1, "", $0)}')
printf 'var has been set to:\n%s\n' "$var"

var2=$(printf '%s\n' "$v" | while IFS= read -r line
	do	printf '%*s%s\n' "$val1" '' "$line"
	done
)

if [ "$var" != "$var2" ]
then	printf 'var2 has been set to:\n%s\n' "$var2"
else	printf 'var2 is identical to var\n'
fi

var3=$(awk -v val1="$val1" '{printf("%*s%s\n", val1, "", $0)}' <<EOF
$v
EOF
)

if [ "$var" != "$var3" ]
then	printf 'var3 has been set to:\n%s\n' "$var3"
else	printf 'var3 is identical to var\n'
fi

This is shown using a Korn shell, but will work with any shell that conforms to the POSIX standards.

If you want to try this on a Solaris/SunOS system, change the calls to awk to use /usr/xpg4/bin/awk or nawk.

The output from the above script is:
Code:
var has been set to:
          HOST_CDH_ID
          STD_STDOID_CD
          FUNCENV_CDH_ID
          METDVERS_OID_ID
          METDVERS_NAME_LB
          VERSFLDR_MATROID_CD
          VERSFLDR_PRMRFORMOID_CD
          VERSFLDR_STDEVNTOID_CD
          VERSFLDR_MAND_FL
          VERSFLDR_STDEVNTDEFNAME_LB
          VERSFLDR_STDEVNTDEFREPT_LB
          VERSFLDR_STDEVNTDEFTYPE_LB
          VERSFLDR_ORDRNUMB_NO
          VERSFLDR_ATTRVERSFOLD_JS
          VERSFLDR_CDH_ID
var2 is identical to var
var3 is identical to var

Note that unless $v expands to a HUGE string, using shell built-ins (as in the way var2 is assigned in the above script) will be faster than invoking awk (or sed or any other external utility).
This User Gave Thanks to Don Cragun For This Post:
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