File/string formatter script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File/string formatter script?
# 1  
Old 09-14-2012
File/string formatter script?

Hi all,

I have a config file that contains some text as below:

Code:
server01        :/u01/app/oracle/admin/db01/adump              :*.dmp,5        :compress         :/bin/gzip
server01        :/u01/app/oracle/admin/db04/adump             :*.aud,5        :remove         :/bin/rm
server01        :/u01/app/oracle/admin/db06/adump              :*.log,5        :remove         :logrotate

Can anyone advise how is the best way to be able to format the text so that they lined up neatly?

At the moment, am doing it manually. Thinking I should use awk and printf using the max string of each column as the basis for the max length of the column for each field. Using space seems to be better than using tab as well.

Any feedback much appreciated. Thanks in advance.
# 2  
Old 09-14-2012
Replace all the spaces with single space...It will lined up your file...

Code:
sed 's/[ \t]\+/ /g' file

# 3  
Old 09-15-2012
Using awk while specifying max size of each field:

Code:
 
awk -F":" 'BEGIN {widthlist = "16 46 15 17 9"; split (widthlist,widths," ") } {for (i=1; i<=NF; i++) printf "%-*s", widths[i],$i ;printf "\n" }' file.txt
server01        /u01/app/oracle/admin/db01/adump              *.dmp,5        compress         /bin/gzip
server01        /u01/app/oracle/admin/db04/adump              *.aud,5        remove           /bin/rm  
server01        /u01/app/oracle/admin/db06/adump              *.log,5        remove           logrotate


Last edited by mjf; 09-15-2012 at 06:35 AM..
# 4  
Old 09-15-2012
Hi newbie_01,
Your request isn't clear as to whether the field separator in your input is supposed to be the colon character or sequences of space characters. The suggestions you have from pamu and mjf made different assumptions. The sed script proposed by pamu will line up the 1st, 2nd, 3rd, and 4th fields in the output; but not the 5th field. The awk script provided by mjf will line up all of the columns, but still requires that someone manually determine the maximum width of each field and enter those values into the widthlist string in the script.

The following awk script will calculate the width of each field without manual intervention.
Code:
#!/bin/ksh
awk -F: ' {     fc[NR] = NF
        for(j = 1; j <= NF; j++) {
                f[NR,j] = $j
                if(mw[j] < length($j)) mw[j] = length($j)
        }
}
END {   for(i = 1; i <= NR; i++) {
                for(j = 1; j < fc[i]; j++)
                        printf("%-*.*s", mw[j] + 1, mw[j], f[i,j])
                printf("%s\n", f[i,j])
        }
}' in

When the file in contains your input data, and this script is run, the output produced is:
Code:
server01         /u01/app/oracle/admin/db01/adump               *.dmp,5         compress          /bin/gzip
server01         /u01/app/oracle/admin/db04/adump               *.aud,5         remove            /bin/rm
server01         /u01/app/oracle/admin/db06/adump               *.log,5         remove            logrotate

As written the script uses ":" as the field separator. If you change the 2nd line in the file from:
Code:
awk -F: ' {     fc[NR] = NF

to:
Code:
awk ' { fc[NR] = NF

and rerun the script (using the default field separators), the output produced is:
Code:
server01 :/u01/app/oracle/admin/db01/adump :*.dmp,5 :compress :/bin/gzip
server01 :/u01/app/oracle/admin/db04/adump :*.aud,5 :remove   :/bin/rm
server01 :/u01/app/oracle/admin/db06/adump :*.log,5 :remove   :logrotate

Hopefully, one of these will meet your needs.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 01-10-2013
Hi Don,

Oops, sorry forgot to mention the most important piece of information. The field separator is the colon (Smilie ... I'll try your suggestions and hopefully all goes well.

Thanks again.

Happy New Year
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Search a string in a file in shell script?

I have a text file which is generated when the batch job is run. This batch may take few mins to run. When completed, the last line of the text file would be process completed. I need a shell script which will wait for this file till the process completed is printed in it, once found, it would move... (2 Replies)
Discussion started by: Lalat
2 Replies

2. Shell Programming and Scripting

Formatter

Hi, I have been searching for a tool or online URL to format (beautify) UNIX shell script without any luck. Could you please assist any online link or tool to format UNIX shell script. I've couple of legacy scripts needs to be formatted. Thank you in advance. Regards, Pointers (4 Replies)
Discussion started by: pointers1234
4 Replies

3. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

4. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

5. Shell Programming and Scripting

Copying a string from a file using shell script

Hello everyone I am completely new to shell scripting in linux. I wan to write a script to search for a certain string from a .txt file and copy the string which apears just after tat searched string. Eg: in a file- try.txt , we have a line saying: "roses are red, so what do i do" I... (4 Replies)
Discussion started by: Kishore920
4 Replies

6. Shell Programming and Scripting

Shell Script to extract string from file

Hello, I have a file whose contents look as follows <ID="24" name="Kobe Bryant" Position="Shooting Guard"> <name="Artest" Position="Forward" ID="37"> I need to get the output which contains everything in the quotes for Position (Shooting Guard and Forward). I tried this but it doesn't work... (3 Replies)
Discussion started by: grajp002
3 Replies

7. Shell Programming and Scripting

code formatter for shell script

hello, do anybody know a program to format a shell script code ? i tried "editrocket.com" but this product doesn't format a shell script code. i searched for programs but can't find a shell script code formatter. i have to change a shell script and the style of code is ..... regards (5 Replies)
Discussion started by: bora99
5 Replies

8. Shell Programming and Scripting

Unix Formatter

HI all, Is their any UNIX shell script code formatter that work on windows ? If so where can I get it ? I want to format the shell scripts in with proper indentation etc... Thanks...In Advance (0 Replies)
Discussion started by: dhananjaysk
0 Replies
Login or Register to Ask a Question