Spacing between words

Hi

I have a csv file in below format

First Line=1 
Second Line=2 
And the third Line=3 
Now comes the fourth Line=4 

I want to insert spaces so that the output would be

First Line[3 tabs]=1 
Second Line[3 tabs]=2 
And the third Line[2 tabs]=3 
Now comes the fourth Line[1 tabs]=4 

Can anyone help me do this , Basically the number of tabs should be dynamic depending on the longest line so that all = signs appear on the same vertical column in the output ..

Thanks
Siva M

thru sed...

sed 's/=/ = /' inputfile > outfile

This doesn't help , it puts one space before and after "=" . if the number of words before = sign are not of equal length then the spacing would not be proper

This script will pad the first column with spaces:

awk -F= 'BEGIN { 
  ARGV[ARGC++] = ARGV[ARGC-1] 
  }
NR == FNR {
  length($1) > ml && ml = length($1)
  next
  }
{ 
  printf "%-*s=%s\n", ml, $1, $2 
  }' infile

Do you really need tabs?

How much would be the maximum characters in first field? Assuming 50. And also assuming the number is not more than 999(i.e. maximum 3 digits).

awk -F"=" '{ printf ("%50s = %3d", $1, $2)}' inputfile
1 Like

awk -F"=" '{ printf ("%-50s = %3d", $1, $2)}' inputfile did the trick

Thanks a lot

awk -F"=" '{ printf ("%-50s = %-3d", $1, $2)}' inputfile
1 Like
# ./justdoit infile
First Line                =1
Second Line               =2
And the third Line        =3
Now comes the fourth Line =4
 ## justdoit ##
 
#!/bin/bash
rm -f tmpSEDx
x=" "
sed -n "$ s/=/$x=/p" $1>>tmpSEDx
lastl=$(cat tmpSEDx|wc -c)
while  read -r l
  do
    curl=$(echo $l|wc -c)
    let count=$lastl-$curl
    commandx=""
    x=" "
    while [ $(( count -= 1 )) -gt -1 ]
     do
      commandx="$commandx$x";
     done
     echo "$l"|sed "s/=/$commandx=/" >>tmpSEDx
 done<$1
sed '1d' tmpSEDx

I think that Radoulov's solution above is the best one as it calculates the maximum line length before formatting it. I am not sure that I understand the advantage of manipulating the ARGV variable compared to this:

awk -F= 'NR==FNR{len=length($1);len>ml && ml=len;next}{printf "%-*s=%s\n", ml, $1, $2}' infile infile

It's just another way to do the same thing.

After having RTFM I think I understand how it ARGV works. It looks �bergeek for sure! Nice the way how you calculate the maximum line length.