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
# 8  
Old 12-21-2011
Bug

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

Thanks a lot
# 9  
Old 12-21-2011
To remove control characters (before the field widths are counted) try this:
Code:
awk 'NR== FNR { W[$1]=$2 ; next}
{ start=width=0;
  gsub("[[:cntrl:]]", "")
  for(i=1; substr($0,i,2) in W; i+=W[substr($0,i,2)]) {
    start=i;width=W[substr($0,i,2)]; }
   print i, start, width , length, substr($0,i,2)
   if (start+width>length)
      printf "%s\n%*s&&\n", substr($0,1,start-1), -width, substr($0,start)
   else
      print substr($0,start+width,2) ":Undefined!"
}' seg_table infile

This User Gave Thanks to Chubler_XL For This Post:
# 10  
Old 12-22-2011
MySQL

It works perfect. Do you know how to insert control character with VI editor? Also how can I send the output to a file? I tried and could not make it work
# 11  
Old 12-23-2011
you may try below perl
Code:
my $str='FU22222222CA6666666666AKxvbFMddreeadBP99';

while(<DATA>){
    my @tmp = split;
    $hash{$tmp[0]}=$tmp[1]-length($tmp[0]);
    $reg2=$reg2.'|'.$tmp[0];
}
$reg2=~s/^\|//;
my @tmp =split(/(?=(?:$reg2))/,$str);
map { s/($reg2)(.*)/$1.$2.' 'x($hash{$1}-length($2))/e} @tmp;
print join "", @tmp;
print "<---\n";
__DATA__
FU 10
CA 12
AK 10
FM 9
BP 10

# 12  
Old 12-23-2011
Thanks Summer Cherry for providing the perl script. As I do not have any perl experience, would you mind send me the complete script. So I can execute. Also can you please replace the string with file. the actual data I need to process is in a file.
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