Help with Aligning the content of a txt file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with Aligning the content of a txt file
# 15  
Old 06-13-2012
@sdf
Thanks a lot its working good

But still...I expect the command prompt should come in the next new line...This script prompting in the last line of the output itself...Smilie

---------- Post updated at 07:31 AM ---------- Previous update was at 05:43 AM ----------

@sdf
can you please explain the script
I couldn't understand the role of variables b,e,d,i...here
# 16  
Old 06-13-2012
Quote:
@sdf
can you please explain the script
I couldn't understand the role of variables b,e,d,i...here
See attached script with explanation.

Code:
    # https://www.unix.com/shell-programming-scripting/188853-help-aligning-content-txt-file.html
    { NR>1 && FNR==1 ? l=length($0) && a[i++]= "\n" $0 : a[i++]=$0 }  
    #1 NR>1 && FNR==1                        -> If total number of rows is more than 1 and the row of the current input file is 1 than
    #2 ? l=length($0)                          -> reset the variable l (see help text #9) and assign the length of the current row ($0) to (l)
    #3 && a[i++]= "\n" $0                    -> and (&&) add the current line plus the line break ("\n") to the array (a[i++]) which will be automatically incremented with the plus (++) by 1 (=i++)
    #4 : a[i++]=$0                            -> else (:) if not (NR>1 && FNR==1) than add the current line to the array (a)
    {if(NR>1 && FNR==1) for(e=(i-c);e<=(i-1);e++) b[e]=d ;c=FNR; d=l }
    #5 if(NR>1 && FNR==1)                    -> Check again if the total number of rows of all input files is more than 1 and current line number equal 1
    #6 for(e=(i-c);e<=(i-1);e++) b[e]=d        -> do a loop with the variable (e) to fill a second array (b[e]) with the longest line of the current file. This array (b[e]) has the same amount of elements as the array (a[++i]).
    #7 c=FNR                                -> assign the number of lines of the current file to the variable (c) for later use
    #8 d=l                                    -> assign the current length to the variable (d) for later use
    { if( length($0) > l) l=length($0)+1 } 
    #9 if( length($0) > l) l=length($0)+1    -> check the longest line of the current file and assign the length to the variable (l)
    END{for(e=(i-c+1);e<=i;e++) b[e]=d; for(j=1;j<=i;j++) printf("%-"b[j]"s,",a[j-1] )}
    #10 for(e=(i-c+1);e<=i;e++) b[e]=d         -> do the last loop on the last input file to fill the array (b) to adjust the field to the longest line
    #11 for(j=1;j<=i;j++)                    -> do a loop (j) to print out the array (a[j]) and the lengths of the longest lines (b[j])
    #12 printf("%-"b[j]"s,",a[j-1] )        -> print out the elements of the array (a[j-1]) with the blank filled format according to the longest line from the array (b[j])

# 17  
Old 06-14-2012
See attached script with explanation.
Quote:
Originally Posted by sdf
Code:
    # https://www.unix.com/shell-programming-scripting/188853-help-aligning-content-txt-file.html
    { NR>1 && FNR==1 ? l=length($0) && a[i++]= "\n" $0 : a[i++]=$0 }  
    #1 NR>1 && FNR==1                        -> If total number of rows is more than 1 and the row of the current input file is 1 than
    #2 ? l=length($0)                          -> reset the variable l (see help text #9) and assign the length of the current row ($0) to (l)
    #3 && a[i++]= "\n" $0                    -> and (&&) add the current line plus the line break ("\n") to the array (a[i++]) which will be automatically incremented with the plus (++) by 1 (=i++)
    #4 : a[i++]=$0                            -> else (:) if not (NR>1 && FNR==1) than add the current line to the array (a)
    {if(NR>1 && FNR==1) for(e=(i-c);e<=(i-1);e++) b[e]=d ;c=FNR; d=l }
    #5 if(NR>1 && FNR==1)                    -> Check again if the total number of rows of all input files is more than 1 and current line number equal 1
    #6 for(e=(i-c);e<=(i-1);e++) b[e]=d        -> do a loop with the variable (e) to fill a second array (b[e]) with the longest line of the current file. This array (b[e]) has the same amount of elements as the array (a[++i]).
    #7 c=FNR                                -> assign the number of lines of the current file to the variable (c) for later use
    #8 d=l                                    -> assign the current length to the variable (d) for later use
    { if( length($0) > l) l=length($0)+1 } 
    #9 if( length($0) > l) l=length($0)+1    -> check the longest line of the current file and assign the length to the variable (l)
    END{for(e=(i-c+1);e<=i;e++) b[e]=d; for(j=1;j<=i;j++) printf("%-"b[j]"s,",a[j-1] )}
    #10 for(e=(i-c+1);e<=i;e++) b[e]=d         -> do the last loop on the last input file to fill the array (b) to adjust the field to the longest line
    #11 for(j=1;j<=i;j++)                    -> do a loop (j) to print out the array (a[j]) and the lengths of the longest lines (b[j])
    #12 printf("%-"b[j]"s,",a[j-1] )        -> print out the elements of the array (a[j-1]) with the blank filled format according to the longest line from the array (b[j])

Why do we go for one more array....I mean array b[j]..Can you please clarifySmilie
# 18  
Old 06-14-2012
In the array b[j] the length of the longest line of each file is stored. The array can only be activated after every input-file. In the END statement the printout with the padded blanks in the fields is invoked with
Code:
printf("%-"b[j]"s,",a[j-1] )

# 19  
Old 06-18-2012
Quote:
Originally Posted by rajmohan146
@sdf
Thanks a lot its working good

But still...I expect the command prompt should come in the next new line...This script prompting in the last line of the output itself...Smilie

---------- Post updated at 07:31 AM ---------- Previous update was at 05:43 AM ----------

@sdf
can you please explain the script
I couldn't understand the role of variables b,e,d,i...here
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy the content from txt file and create a html file

I have a txt file with a list of error messages in a xml tag format, and each error message is separated with a identifier(endresult).Need to split that and copy and create a new html file.Error message has some special character. how to escape the special character and insert my data into the... (7 Replies)
Discussion started by: DevAakash
7 Replies

2. Shell Programming and Scripting

Aligning a file

I have a large text file in following format cat input.txt abc qwert qwer afweferf wdfwefwe ==> kjhjkwdd mnmn ==> jkjkjwekj poiu ==> lklklke tytyutut ==> olkjmnsmn I need to align those lines with the characters " ==>" . I dont want to disturb the lines which dont have "==>". The... (6 Replies)
Discussion started by: ctrld
6 Replies

3. Shell Programming and Scripting

Reset different and multiple .txt file content to 0

I need help. I have to create a cron job that will reset the value of different and multiple .txt file to 0. Example: Actual 172_21.txt = 25 192_101.txt = 10 192_168.txt = 5 10_10.txt = 3 After the cron job 172_21.txt = 0 192_101.txt = 0 192_168.txt = 0 10_10.txt = 0 The cron... (2 Replies)
Discussion started by: jasperux
2 Replies

4. Shell Programming and Scripting

Aligning columns in a text file using Perl

Hi All, I am new to perl and was trying to write a simple program which will generate a text file as output.. now the output which i am getting is something like this.. ================================================================================================== Col1 ... (8 Replies)
Discussion started by: smarty86
8 Replies

5. Shell Programming and Scripting

Aligning numbers in the second file based on first file

Hi All, I have two sets of files with names .dat and .txt. The number of files is really large more than 90000. The files have names like 1.dat, 2.dat,3.dat and so on where as txt files have names like 1.txt, 2.txt, 3.txt and so on The DAT and TXT files are equal in number. About 90000 each ... (4 Replies)
Discussion started by: shoaibjameel123
4 Replies

6. UNIX for Dummies Questions & Answers

sed insert content of file.txt to multi files

Ive this sed & find command find /home/www/ -name footer.php -exec sed -i 's/<\/body>/file.txt\n<\/body>/' what I need to place content of file.txt before </body> in all footer.php files file.txt content is google analytic script which is like 7 lines any help to adjust my command to... (2 Replies)
Discussion started by: xmoe
2 Replies

7. Shell Programming and Scripting

how to create file.txt and add current date in file content

Hey guy, how to make bash script to create foo.txt file and add current date into file content and that file always append. example: today the script run and add today date into content foo.txt and tomorrow the script will run and add tomorrow date in content foo.txt without remove today... (3 Replies)
Discussion started by: chenboly
3 Replies

8. Shell Programming and Scripting

Extract content from several txt-files

Hi! Im trying to write a script in ksh that creates a single txt-file from specific content in several other txt-files. From these files I want to extract all text after 'WORD' and before '=', regardless of number of lines and other content. I have tried cat and guess I need... (7 Replies)
Discussion started by: larsu
7 Replies

9. UNIX for Dummies Questions & Answers

Binary txt file received when i use uuencode to send txt file as attachment

Hi, I have already read a lot of posts on sending attachments in unix...but none of them were of help for my problem...so here goes.. i wanna attach a text file and send to a mail id..used the following code : uuencode "$File1" "$File1" ;|mail -s "$Mail_sub" abc@abc.com it works... (2 Replies)
Discussion started by: ash22
2 Replies

10. UNIX for Dummies Questions & Answers

grep/cat/more -- search in a txt file and display content from a specific "keyword"

Hi, I have a .txt file Sample: ===================== NEXT HOST ===================== AEADBAS001 ip access-list extended BLA_Incoming_Filter ip access-list extended BLA_Outgoing_Filter access-list 1 permit xxxxxxxxxxxxxx access-list 2 permit xxxxxxxxxxxxxx =====================... (4 Replies)
Discussion started by: I-1
4 Replies
Login or Register to Ask a Question