Spacing between words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Spacing between words
# 1  
Old 12-14-2010
Spacing between words

Hi


I have a csv file in below format
Code:
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

Code:
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

Last edited by radoulov; 12-14-2010 at 04:39 AM.. Reason: Code tags, please!
# 2  
Old 12-14-2010
thru sed...
Code:
sed 's/=/ = /' inputfile > outfile

# 3  
Old 12-14-2010
Quote:
Originally Posted by michaelrozar17
thru sed...
Code:
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
# 4  
Old 12-14-2010
This script will pad the first column with spaces:

Code:
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?
# 5  
Old 12-14-2010
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).

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


Last edited by R0H0N; 12-14-2010 at 05:00 AM..
This User Gave Thanks to For This Post:
R0H0N
# 6  
Old 12-14-2010
Quote:
Originally Posted by R0H0N
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).

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


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

Thanks a lot
# 7  
Old 12-14-2010
Code:
awk -F"=" '{ printf ("%-50s = %-3d", $1, $2)}' inputfile


Last edited by R0H0N; 12-14-2010 at 05:48 AM..
This User Gave Thanks to For This Post:
R0H0N
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep spacing issue

Hi, I'm trying to match html strings using grep commend and I'm not able to eliminate the extra spaces. This is working when there is no space. Can anyone help me what am I missing here? I'm able to match KayDee where there is no space. grep commands tried: grep -P -o -e... (1 Reply)
Discussion started by: Shenbaga.d
1 Replies

2. Shell Programming and Scripting

Uniform Spacing in the message

Hello, I am running a script which sends an output as an email; I am having issues with the spacing being not uniform in the message. Snippet of the code and email message below: if ] then echo "$Hostname\tMISSING\tHMCBackup" >> $BackupMsg else if ] then echo... (12 Replies)
Discussion started by: hasn318
12 Replies

3. UNIX for Dummies Questions & Answers

output spacing and formatting

here is my code and output, i just want to display it clearly to the users. how can I fix the spacing or put some headers like NAME, DEV id, Size, Meta code: symdg show $INS-${SNAP} | egrep "D-" | awk '{print $1,$3,$NF,$5}' output: D-arch 23C2 983040 (M) D-db 0704 245760 (M) D-undo 07DB... (12 Replies)
Discussion started by: prodigy06
12 Replies

4. UNIX for Dummies Questions & Answers

Define spacing of columns

Hi! I need to change the spacing assigned to each number in a text file. I have an input file with 5 columns and 3 rows. Here, all numbers are separated by 1 space. I need to change this in such a way that the number in the first column has 6, the number in the second column has 5 and all other... (2 Replies)
Discussion started by: Alauda
2 Replies

5. UNIX for Dummies Questions & Answers

paste command without spacing?

I'm trying to combine text files without a space. So if i use the paste command paste file1 file2 file3 > file4 the new file created has spacing between the contents of the once individual files. Is there some trick I can do with a delimiter that removes the spaces.. like paste -d'' or... (1 Reply)
Discussion started by: steveinthebox
1 Replies

6. UNIX for Dummies Questions & Answers

Help with the spacing

while IFS="" read r; do printf "XXX\t%s\n" "$r" done < test1.txt > test.txt The issue is, XXX wud be a dummy column/row added to the file..But i want this XXX column to be a separated as a TAB Delimiter it should be something like XXX 1 XXX 2 (3 Replies)
Discussion started by: saggiboy10
3 Replies

7. Shell Programming and Scripting

spacing problem

Hi guys, I have this little code: for directory in / $(echo $path | tr '/' ' ' ) do cd $directory echo "$(ls -ld | cut -c2-10 | sed 's/.\{3\}/& /' | sed 's/.\{7\}/& /' | sed 's/.\{1\}/& /g')" " $directory" done The output of this will be showing the permissions with spaces so it will... (2 Replies)
Discussion started by: darkhider
2 Replies

8. Shell Programming and Scripting

How to adjust spacing

Is there a way to adjust spacing of a line using k shell? e.g I have a file below $ cat file1 AAA BBB CCC A B C AAAA BB CC I want each word to be adjusted with spaces to have 10 character length like below: AAA BBB CCC A B C AAAA BB CC Any... (4 Replies)
Discussion started by: stevefox
4 Replies

9. Shell Programming and Scripting

character spacing issue

I have a script that has a counter in it, the output from the script puts the values in columns, and when the values are greater than 9 it moves the rest of the row over, hence displacing the columns. Is their something I can do to make these values fit in their respective column? I tried typing an... (1 Reply)
Discussion started by: wxornot
1 Replies

10. UNIX for Dummies Questions & Answers

spacing question

hi , i have follwing strings in a text file hello i am cool hey i am cool arrey i am cool in all the above 3 i wanna retrieve cool.......for this i used split(/ /) but it takes into consideration only one space.......is there any regular expression to do the trcik? ... (7 Replies)
Discussion started by: vivekshankar
7 Replies
Login or Register to Ask a Question