Visit Our UNIX and Linux User Community


File formating


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File formating
# 1  
Old 03-03-2017
File formating

I need to create a fixed width file based on the column lengths.
lets assume I have six(this may be dynamic) fields each are of different length
  • column1=6 #size of the column
  • column2=3
  • column3=2
  • column4=3
  • column5=4
  • column6=5

I tried below code snippet but it is not working
Code:
 echo 'a|b|c|d|e|f' |awk 'BEGIN {IFS = "|";column_length[0]=6;column_length[1]=3;column_length[2]=2;column_length[3]=3;column_length[4]=4;column_length[5]=5}
{{for(i=1;i<=NF;i++){printf " | ";printf("%-$column_length[$i-1]s",$i);printf "|"; if(i==NF){printf("\n")}}}}'

I want output to be in below format
Code:
| a     | b  | c | d   | e   | f    |


Last edited by rbatte1; 03-03-2017 at 07:13 AM..
# 2  
Old 03-03-2017
To help you get on your way, try:
Code:
echo 'a|b|c|d|e|f' |awk 'BEGIN {FS = "|";column_length[0]=6;column_length[1]=3;column_length[2]=2;column_length[3]=3;column_length[4]=4;column_length[5]=5}
{for(i=1;i<=NF;i++) printf("| %-" column_length[i-1] "s",$i); printf("|\n")}'

Code:
| a     | b  | c | d  | e   | f    |


--
Note: instead of
Code:
printf("| %-" column_length[i-1] "s",$i)

,you can also use:
Code:
printf("| %-*s", column_length[i-1],$i)


Last edited by Scrutinizer; 03-03-2017 at 12:40 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-03-2017
Hi I am getting below error when I formatted my code
Code:
 #!/bin/ksh
set -x
fixed_output_format()
{
 domain=$1
 file_reg=$2
 file_freq=$3
 file_name=$4
 file_counter=$5
 record_cnt=$6
 file_creation_dt=$7
 echo " We are inside Module  "  
  $(echo "$domain | $file_reg | $file_freq | $file_name | $file_counter | $record_cnt | $file_creation_dt " |
  awk 'BEGIN
       {
        FS = "|";
        column_length[0]=6;
        column_length[1]=3;
        column_length[2]=2;
        column_length[3]=3;
        column_length[4]=4;
        column_length[5]=5;
        column_length[6]=6;
       }
       {
        for(i=1;i<=NF;i++)
        printf("| %-" column_length[i-1] "s",$i);
        printf("|\n")
       }' )  >>  OUTPUT_FILE
 }
################
echo " we are the start of script"
fixed_output_format a b c d e f g

I am getting below output
Code:
awk: cmd. line:1: BEGIN blocks must have an action part

# 4  
Old 03-03-2017
awk doesn't ALWAYS allow for breaking lines. Put the opening { on the same line OR use a "line continuation" character: \ .
# 5  
Old 03-03-2017
In addition to what RudiC and Scrutinizer have already said, why would you write a 28 line Korn shell function using a pipeline of echo and awk to get at awk's printf function when ksh's built-in printf utility can do the same thing in 5 lines?

Note that the leading space on the 1st line of your script makes that line just a comment and removes its ability to specify that your script is to be run by /bin/ksh.
Code:
#!/bin/ksh
set -x
fixed_output_format()
{	echo " We are inside Module  "  
	printf '|%-6s|%-3s|%-2s|%-3s|%-4s|%-5s|%-6s|\n' \
	    "$1" "$2" "$3" "$4" "$5" "$6" "$7" >> OUTPUT_FILE
 }
################
echo " we are the start of script"
fixed_output_format a b c d e f g

And, note that calling this function fixed_output_format is a misnomer if the arguments you supply to this function contain strings that are longer than the specified field widths. If you really want fixed width output for each field (truncating long input strings), you would want to change:
Code:
	printf '|%-6s|%-3s|%-2s|%-3s|%-4s|%-5s|%-6s|\n' \

to:
Code:
	printf '|%-6.6s|%-3.3s|%-2.2s|%-3.3s|%-4.4s|%-5.5s|%-6.6s|\n' \

To see what difference this makes, add an additional line to the end of your script:
Code:
fixed_output_format 7777777 4444 333 4444 55555 666666 7777777

and look at the last two lines printed into OUTPUT_FILE by the above printf statement suggestions.
With your current format you'd get:
Code:
|a     |b  |c |d  |e   |f    |g     |
|7777777|4444|333|4444|55555|666666|7777777|

and with the alternative you'd get:
Code:
|a     |b  |c |d  |e   |f    |g     |
|777777|444|33|444|5555|66666|777777|

This User Gave Thanks to Don Cragun For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #547
Difficulty: Medium
unary operators only operate on universal variables.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with ... Formating the file using awk

I have a file like below position1 0 7802 7802 0 client1 - - position1 8 8032 8032 0 client1 ... (7 Replies)
Discussion started by: manas_ranjan
7 Replies

2. UNIX for Beginners Questions & Answers

File formating help

Hi all, I am having the file below I need that as below Thanks, Arun (12 Replies)
Discussion started by: arunkumar_mca
12 Replies

3. UNIX for Beginners Questions & Answers

File formating with lines

Hi All, Need to modify a file basically join the extra line to one I am having a file like below And would like to make it as below . (4 Replies)
Discussion started by: arunkumar_mca
4 Replies

4. Shell Programming and Scripting

Help in formating a file with field separator

hi thanks a lot But pls help me if my input file is the below format: Name priya 2010-09-21 10:43:49 TEXT ID 1 hi TEXT ID 2 how TEXT ID 3 r TEXT ID 4 u Name2 priya2 2010-09-21 10:43:49 TEXT ID 1 hi1 TEXT ID 2 how1 TEXT ID 3 r1 (4 Replies)
Discussion started by: bha148
4 Replies

5. Shell Programming and Scripting

Help in formating a txt file

Pls help in formatting a txt file using shell scripting Input file format: Name priya 2010-09-21 10:43:49 TEXT ID 1 hi TEXT ID 2 how TEXT ID 3 r TEXT ID 4 u Output required: name priya hi how r u (4 Replies)
Discussion started by: bha148
4 Replies

6. Shell Programming and Scripting

File formating (sed command)

I am new ot unix. Is there one or two lines of command (sed may be) to format a file. My source is.. <tag1> <tag2> <datatag1> data1 </datatag1> <datatag2> data2 </datatag2> <datatag3> data3 </datatag3> <datatag4> data1 </datatag3> </tag2> </tag1> (2 Replies)
Discussion started by: reachsam11
2 Replies

7. UNIX for Dummies Questions & Answers

File formating help needed.

I have a file called vm.cfg . content of file. acpi = 1 apic = 1 builder = 'hvm' How would i write a script which will add boot = 'd' at 3rd line. For example. after running the script the file should be like below. acpi = 1 apic = 1 boot = 'd' builder = 'hvm' I dont want to... (3 Replies)
Discussion started by: pinga123
3 Replies

8. Shell Programming and Scripting

file formating in Perl

Hi, I am new to unix , I have a requirement for formating the input file and generate the output file as per the downstream requirement .. My application receiving a text input file having 4 field and my application need to check each field and if some value of a field is blank ..then it need... (1 Reply)
Discussion started by: julirani
1 Replies

9. Shell Programming and Scripting

File formating question.

Hi, I have a file with only data 1 row: AA#?BB#?CC#?DD Assume '#?' is the delimiter, i want the output in another file to be: AA BB CC DD Pls let me know if this is possible by sed ? Thanks in advance. (3 Replies)
Discussion started by: smc3
3 Replies

10. UNIX for Dummies Questions & Answers

Help required on file formating

I have two files file1 and file2 as shown below: file1: name nameabc bcd nameabcdefg file2: age age1111 age2345 age6743 I have pasted one file on the other with the delimiter "|" and the resulttant file is: name|age nameabc|age1111 bcd|age2345 nameabcdefg|age6743 (1 Reply)
Discussion started by: udiptya
1 Replies

Featured Tech Videos