Replace blank spaces by single tab, and right alignment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace blank spaces by single tab, and right alignment
# 1  
Old 08-08-2007
Replace blank spaces by single tab, and right alignment

Folks, I am wondering if anyone solve this problem.
What I want to know is,
1. Delete all white spaces including leading blank space in each line (e.g. line 2), and replace such spaces by single tab except leading blank space

2. Then, align all columns to the right. But, output white space between columns should be identified by single tab, not white space, not multiple tab.

<input>
1990 8 9 2.31 3.12 4.343 112.113 0.123
1992 9 10 1.11 3.33 2.12 1.23 0.11
2011 10 11 2.56 7.23 3.11 2.33 0.1


Thanks,
# 2  
Old 08-08-2007
The first one is simple...
Code:
$ awk -v OFS="\t" '$1=$1' file1
1990    8       9       2.31    3.12    4.343   112.113 0.123
1992    9       10      1.11    3.33    2.12    1.23    0.11
2011    10      11      2.56    7.23    3.11    2.33    0.1

But the second one is impossible. If you want to right-align, then you have to pad with something, e.g. spaces...
Code:
$ awk -v OFS="\t" '{for(i=1;i<=NF;i++)$i=sprintf("%7s",$i);print}' file1
   1990       8       9    2.31    3.12   4.343 112.113   0.123
   1992       9      10    1.11    3.33    2.12    1.23    0.11
   2011      10      11    2.56    7.23    3.11    2.33     0.1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert mutiple spaces file to single tab

I have the following file I wanted to convert mutiple spaces to tab: I tried cat filename | tr ' ' '\t' or sed 's/ */ /' FILE but it looses the format 5557263102 5557263102 5552074858 5726310211 5557263102 5557263102 5557263103 5557263103 2142406768 ... (2 Replies)
Discussion started by: amir07
2 Replies

2. Shell Programming and Scripting

Add Blank Spaces in text, to perform beter alignment of the string

Hi Guru, I need some advice on how to add blank spaces to the code, rather than me just adding <space-bar spaces> which does not work. Current output of the code File System Backed Up - ALL_LOCAL_DRIVES Daily - Incremental Backup Schedule - 1 Month Retention • 7pm - PRD... (2 Replies)
Discussion started by: Junes
2 Replies

3. Shell Programming and Scripting

How to replace blank tab with zero in a file?

hi, i need to replace a blank tab output in a file to zero. input file: 2015/08/04 00:00:00 171 730579 27088 <blank> 3823 30273 1621778 ... (6 Replies)
Discussion started by: amyt1234
6 Replies

4. AIX

Replace all TAB characters with white spaces

Dear Gurus Can you please advise me on how to Replace all TAB characters with white spaces in a text file in AIX? Either using vi or any utilities (2 Replies)
Discussion started by: tenderfoot
2 Replies

5. UNIX for Dummies Questions & Answers

Replace tab or any spaces with "

my content: samaccountname employeeid useraccountcontrol description i want it to look like this: "samaccountname","employeeid","useraccountcontrol","description" (2 Replies)
Discussion started by: tjmannonline
2 Replies

6. Shell Programming and Scripting

Replace blank spaces with semicolon - text file

Hi all, Been trying to find a solution to this, I'm sure its a sed 1 liner, but I don't know sed well enough to work it out... I have a text file in the following format: 431 666 1332 2665 0.24395 432 670 ... (3 Replies)
Discussion started by: mpcengineering
3 Replies

7. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

8. Shell Programming and Scripting

Replace spaces between strings in a line with tab

Hi All I am having problem in substitution of any number of spaces, or a combination of space and tab in between strings in the lines of text file. Is there any way out in Perl? Please help me. e.g., Say the input is in the following format:- XX yyy zzz... (1 Reply)
Discussion started by: my_Perl
1 Replies

9. Shell Programming and Scripting

Converting 2 or more spaces to a single tab

I'm new to bash and want to know a simple sed, awk, or grep script that will find all instances of 2 or more spaces and convert them to a single tab. Thanks for the help in advance. (1 Reply)
Discussion started by: jkandel
1 Replies

10. Shell Programming and Scripting

replace space or spaces in a line of a file with a single :

I am searching while I await a response to this so if it has been asked already I apologize. I have a file with lines in it that look like: bob johnson email@email.org I need it to look like: bob:johnson:email@email.org I am trying to use sed like this: sed -e 's/ /:/g' file >... (5 Replies)
Discussion started by: NewSolarisAdmin
5 Replies
Login or Register to Ask a Question