# Reformat a string and pad space at the end

**URL:** <https://community.unix.com/t/reformat-a-string-and-pad-space-at-the-end/301429>\
**Category:** Shell Programming and Scripting\
**Created:** [December 20, 2011, 10:26am UTC](https://community.unix.com/t/reformat-a-string-and-pad-space-at-the-end/301429 "2011-12-20T10:26:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![menglm](https://community.unix.com/letter_avatar/menglm/32/5_5575768a8748004e209b776fc1b2916d.png) [@menglm](https://community.unix.com/u/menglm)\
**Post date:** [December 20, 2011, 10:26am UTC](https://community.unix.com/t/reformat-a-string-and-pad-space-at-the-end/301429/1 "2011-12-20T10:26:31Z")

</div>

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  
new string  
FU22222222CA6666666666AKxvbFMddreead  
BP999 &&

Segment table  
id length  
-----------------  
FU 10  
CA 12  
AK 5  
FM 9  
BP 20

Thanks

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [December 21, 2011, 3:12am UTC](https://community.unix.com/t/reformat-a-string-and-pad-space-at-the-end/301429/2 "2011-12-21T03:12:08Z")

</div>

Double post of:

> **[Pad space at the end of string and reformat](https://www.unix.com/shell-programming-and-scripting/173550-pad-space-end-string-reformat.html)**
>
> tested the code and it worked perfect. Thanks a lot. One more question regarding this program. If I also need to remove the control characters from the string, can you help to add the additional logic | The UNIX and Linux Forums

Thread closed.
