Putting white Space at the end of the string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Putting white Space at the end of the string
# 1  
Old 10-16-2013
Putting white Space at the end of the string

Hi Guys,
Hope, you all are doing good out there.
I am writing a shell script and currrint in need of your help.

This is what I need to do;
I have position based plain file. One of the fields is 15 character long. I need to fill that field. The problem is that the value is dynamic, it could be 4, 8, 10, 13 or 15 characters long. It is quite possible that most of the time the value is not 15 character long.

If the value is less than 15, then I need to add the no of exact white spaces to make it up to 15.

For examle, the string is "ERI-STD-W800I". Here, the length of the string is 13, so I need to add 2 white spaces at the end of it. So, the string will be "ERI-STD-W800I ".


Can you please help me in achieving this?

A prompt help will be highly appreciated.

Regards,
Chandan Singh
Moderator's Comments:
Mod Comment Using font changes and bold text do not adequately replace CODE (or ICODE) tags when spacing is important.

Last edited by Don Cragun; 10-16-2013 at 02:08 PM.. Reason: Remove extraneous font changes, change bold characters to ICODE tags; correct spacing.
# 2  
Old 10-16-2013
Assuming you have printf:
Code:
printf "%-15s" $value

This User Gave Thanks to CarloM For This Post:
# 3  
Old 10-16-2013
Quote:
Originally Posted by CarloM
Assuming you have printf:
Code:
printf "%-15s" $value

Although this works fine for the given example, it would be safer to use:
Code:
printf "%-15s" "$value"

in case $value sometimes expands to a string containing whitespace characters.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 4  
Old 10-17-2013
Thanks Don for your help....
Much appreciated.... Smilie
# 5  
Old 10-17-2013
An example with awk, field separator : and field #1 and input file /etc/group
Code:
awk 'BEGIN {FS=OFS=":"} {$field=sprintf("%-*s",size,$field); print}' field=1 size=15 /etc/group

Here the * in the format string requires the size as an additional argument.
Of course could be directly hacked into the format string like this
Code:
awk 'BEGIN {FS=OFS=":"} {$field=sprintf("%-"size"s",$field); print}' field=1 size=15 /etc/group

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add white space

hi guys how can i add spacein file name with sed if strings have no space around dash input 19-20 ( 18-19 ) ABC-EFG output after add white space 19 - 20 (18 - 19 ) ABC - EFG thx in advance (2 Replies)
Discussion started by: mhs
2 Replies

2. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

3. Shell Programming and Scripting

Pad space at the end of string and reformat

I need to read in the string from input file and reform it by cut each segment and check the last segement lenght. If the last segment length is not as expected (see below segment file or table. It is predefined), then pad enough space. Old string FU22222222CA6666666666AKxvbFMddreeadBP999... (11 Replies)
Discussion started by: menglm
11 Replies

4. Shell Programming and Scripting

Reformat a string and pad space at the end

I need to read in the string from input file and reform it by cut each segment and check the last segement lenght. If the last segment length is not as expected (see below segment file or table. It is predefined), then pad enough space. Old string FU22222222CA6666666666AKxvbFMddreeadBP999... (1 Reply)
Discussion started by: menglm
1 Replies

5. Shell Programming and Scripting

How to remove white spaces from the beginning an end of a string in unix?

Suppose, I have a variable var=" name is ". I want to remove the blank spaces from the begining and endonly, not from the entire string. So, that the variable/string looks like following var="name is". Please look after the issue. (3 Replies)
Discussion started by: mady135
3 Replies

6. Shell Programming and Scripting

sed + white space

Hi, What sed command (if sed is the right command) can remove ALL white space from my file. I have a csv, except I want to remove all white space between commas and characters. My idea (without testing) sed 's/ //g' Is there a better way? (18 Replies)
Discussion started by: mcclunyboy
18 Replies

7. Shell Programming and Scripting

Add white space to the end of a line with sed

Im trying to add 5 blank spaces to the end of each line in a file in a sed script. I can figure out who o put the spaces pretty much anywhere else but at the end. thanks Karl (7 Replies)
Discussion started by: karlanderson
7 Replies

8. UNIX for Dummies Questions & Answers

SED with White Space

Dear Members, Suppose i have a variable test which stores a string as below: test='John drives+++++++++a+++++car' now i want to use sed on the above variable and replace + with a white space, so that i get echo $test should give me 'john drives a car' Between... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

9. Programming

How to trim the white space around a string in C program

I am coding a C program to read a plain text file. There are a lot of blank fields or a string with white spaces. I want to know is there such a function called trim() in C to clean the white space around a string? Or some other way can do this efficiently? Thanks. (18 Replies)
Discussion started by: hxm1303
18 Replies

10. Shell Programming and Scripting

stripping white space...

Hi All; Having a problem with a file.. the file contains the following data... (a snapshot) 1331F9E9DB7C2BB80EAEDE3A8F043B94,AL7 1DZ,M,50 186FDF93E1303DBA217279EC3671EA91,NG5 1JU,M,24 3783FFAF602015056A8CD21104B1AAAF,CH42 4NQ,M,17 It has 3 columns sepreated by a , the second column... (7 Replies)
Discussion started by: Zak
7 Replies
Login or Register to Ask a Question