Pad space at the end of string and reformat


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pad space at the end of string and reformat
# 1  
Old 12-20-2011
Question 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
Code:
FU22222222CA6666666666AKxvbFMddreeadBP999

new string
Code:
FU22222222CA6666666666AKxvbFMddreead
BP999               &&


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


Last edited by radoulov; 12-20-2011 at 12:15 PM.. Reason: Code tags!
# 2  
Old 12-20-2011
Quote:
Originally Posted by menglm
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.
1. Reform it on the basis of what?
2. When you say "cut each segment and check the last segment length", what do you mean? How much of the string do you want to cut?
# 3  
Old 12-20-2011
if you see the sample old and new strings I incluced, basically go through the entire string segment by segment (segment id and length are defined at the table at the end of post). Only if the last segment length is not as expected, then pad spaces, else the old string and new string should be the same,except that new string gets padded with && at the end of string.

In plain program language:
cut the first two byte from the input string as segment id, and lookup segment id table with segment id to get segment length. The cut the inputstring with that length and save the new cutted string. Repeat the same logic till the end of inputstring. If the length of last piece of string is the lenghth from segment lookup, then append space.

See the example, last segment BP is only 5 bytes long. But segment length for BP is 20 (see segment table), so the new string length for the BP will be 5+15 spaces.

Let me know if this is clear
# 4  
Old 12-20-2011
Put your segment sizes in seg_table file:
Code:
FU 10
CA 12
AK 5
FM 9
BP 20

Then try this:
Code:
awk 'NR==FNR {W[$1]=$2;next}
{  start=width=0
   for(i=1; substr($0,i,2) in W; i+=W[substr($0,i,2)]) {
    start=i;width=W[substr($0,i,2)]; }
   if (start+width>length)
      printf "%s\n%*s&&\n", substr($0,1,start-1), -width, substr($0,start)
   else
       print $0"&&"
}' seg_table infile


Last edited by Chubler_XL; 12-20-2011 at 07:40 PM..
# 5  
Old 12-20-2011
Hi, Chubler XL,
this script works for one line, if file contains more than one line, it print out wrong result. for example: file like:
Code:
FU22222222CA6666666666AKxvbFMddreeadBP999
AK22222222CA6666666666CAxvx
CA12334555FU33333FM000

seg code
Code:
 FU 10 
CA 12
AK 5 
FM 9 
BP 20

expected result:
Code:
FU22222222CA6666666666AKxvbFMddreead
BP999               &&
AK22222222CA6666666666
CAxvx     &&
CA12334555FU33333
FM000  &&

current script print out as following:
Code:
FU22222222CA6666666666AKxvbFMddreead
BP999               &&
AK22222222CA6666666666CAxvx&&
CA12334555FU33333FM000&&

Would you please take a look.
ken002
# 6  
Old 12-20-2011
Thats how it is supposed to work, if format is wrong it just puts && on end of line:
Code:
   on 2nd line "AK" is 5 wide and "22" is undefined
   on 3rd line "CA" is 12 wide and "33" is undefined

This input file will work:
Code:
FU22222222CA6666666666AKxvbFMddreeadBP999
AK222CA6666666666CAxvx
CA124FU33311FM000

This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 12-20-2011
Yes, you are right,
Thanks
ken002
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gawk --- produce the output in pattern space instead of END space

hi, I'm trying to calculate IP addresses and their respective calls to our apache Server. The standard format of the input is HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST... (2 Replies)
Discussion started by: busyboy
2 Replies

2. Shell Programming and Scripting

How to pad zeroes based on input string?

Hello everyone, I am comparing two floating point numbers by storing them in seperate files and then using difference command to verify,it is working fine. But I want to compare two values which will come at 4 precision places. ex: file1 Date,Count,Checksum... (7 Replies)
Discussion started by: karthik adiga
7 Replies

3. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: singh.chandan18
4 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Pad Zeros at the end

I have number/strings like below input =23412133 output = 234121330000 (depends on the number give at runtime) i need to padd zeros based on runtime input . i tried below printf ' %d%04d\n', "23412133"; But the precision 4 is static here how can i pass this as runtime input. i am... (11 Replies)
Discussion started by: greenworld123
11 Replies

8. UNIX for Dummies Questions & Answers

removal of space from the end

HI, I need the help from the experts like I have created one file with text like: Code: a b c de f g hi j k l So my question is that i have to write the script in which like in the first sentence it will take only one space after d and remove all the extra space in the end.I dont... (0 Replies)
Discussion started by: bhanudhingra
0 Replies

9. Shell Programming and Scripting

Appending string, variable to file at the start and string at end

Hi , I have below file with 13 columns. I need 2-13 columns seperated by comma and I want to append each row with a string "INSERT INTO xxx" in the begining as 1st column and then a variable "$node" and then $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13 and at the end another string " ; COMMIT;" ... (4 Replies)
Discussion started by: Vaddadi
4 Replies

10. UNIX for Advanced & Expert Users

how can I read the space in the end of line

cat file1|while read i do echo "$i"|wc done with this command the space in the end of the line not considered how can solve that for example: read h "hgyr " echo "$h"|wc 4 (2 Replies)
Discussion started by: Ehab
2 Replies
Login or Register to Ask a Question