How can I format a text file into uniform columns?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I format a text file into uniform columns?
# 1  
Old 04-25-2012
How can I format a text file into uniform columns?

I have a file : e.g.
Code:
Charles Dixon  Age 23       Hometown Darlington Postcode DL1 2DC
Fred Bixton  Age 34  Hometown Leeds     Postcode LS1 5XS
Jim Davis            Age 48      Hometown Cardiff  CF2 8YY

Is it possible to format this file into uniform columns using, say, the spaces as delimiters?

Any help most appreciated.

Last edited by Franklin52; 04-25-2012 at 07:10 AM.. Reason: Please use code tags
# 2  
Old 04-25-2012
How does the desired output look like?
# 3  
Old 04-25-2012
pls paste output format you are expecting.
# 4  
Old 04-25-2012
Code:
Charles Dixon      Age 23     Hometown Darlington      Postcode DL1 2DC
Fred Bixton        Age 34     Hometown Leeds           Postcode LS1 5XS
Jim Davis          Age 48     Hometown  Cardiff        Postcode CF2 8YY

i.e. formatted into straight uniform columns instead of staggered as in the example

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 04-25-2012 at 08:04 AM.. Reason: Please use code tags
# 5  
Old 04-25-2012
Hi.

Here is how the free perl code align automatically formats fields (noting that the token postcode was omitted in the last line):
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate preparing and aligning text fields, sed, align.
# Source: http://freecode.com/projects/align

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C sed align

FILE=${1-data1}
pl " Input file $FILE:"
cat -A $FILE

pl " Results of align:"
sed 's/   */\t/g' $FILE |
align -st+ -j_ |
tee f1

pl " Displaying special characters (if any) in output:"
cat -A f1

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
sed GNU sed version 4.1.5
align 1.7.0

-----
 Input file data1:
Charles Dixon  Age 23       Hometown Darlington  Postcode DL1 2DC$
Fred Bixton  Age 34  Hometown Leeds     Postcode LS1 5XS$
Jim Davis            Age 48      Hometown Cardiff  CF2 8YY$

-----
 Results of align:
Charles Dixon Age 23 Hometown Darlington Postcode DL1 2DC
Fred Bixton   Age 34 Hometown Leeds      Postcode LS1 5XS
Jim Davis     Age 48 Hometown Cardiff    CF2 8YY

-----
 Displaying special characters (if any) in output:
Charles Dixon Age 23 Hometown Darlington Postcode DL1 2DC$
Fred Bixton   Age 34 Hometown Leeds      Postcode LS1 5XS$
Jim Davis     Age 48 Hometown Cardiff    CF2 8YY$

The sed transforms 2 or more spaces into TABs. The align options direct fields to be recognized by 1 or more TABs, and the fields are re-joined only with spaces. The distance between fields, the gutter, can be specified if desired.

The perl code is available at freecode as noted in the comments of the script.

Best wishes ... cheers, drl
# 6  
Old 04-25-2012
if you have the util package then you can try as simplest way Smilie
Code:
# column -t infille
Charles  Dixon   Age  23  Hometown  Darlington  Postcode  DL1  2DC
Fred     Bixton  Age  34  Hometown  Leeds       Postcode  LS1  5XS
Jim      Davis   Age  48  Hometown  Cardiff     Postcode  CF2  8YY

# 7  
Old 04-26-2012
Thanks for taking the time to look at this guys. Unfortunately I don't have the "util" package ygemici and I don't have any perl skills drl ! I tried running your perl script but got errors and don't have the knowledge to interpret them. I was hoping for a standard unix scripting solution but again - I do appreciate your times ! Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Format text file to html

Hi Experts, Anybody out there figure out on how to achieve in shell scripts or tools. I have done googling to find solutions but no luck. I have thousands of .txt files to batch process, please see the below sample text content after -------- start here --------. What I want to achieve is to... (10 Replies)
Discussion started by: lxdorney
10 Replies

2. Shell Programming and Scripting

How to concatenate 2-columns by 2 -columns for a text file?

Hello, I want to concatenate 2-columns by 2-columns separated by colon. How can I do so? For example, I have a text file containing 6 columns separated by tab. I want to concatenate column 1 and 2; column 3 and 4; column 5 and 6, respectively, and put a colon in between. input file: 1 0 0 1... (10 Replies)
Discussion started by: huiyee1
10 Replies

3. UNIX for Dummies Questions & Answers

Removing columns from a text file that do not have any values in second and third columns

I have a text file that has three columns. But at the end of the text file, there are trailing lines that have missing second and third columns: 4 0.04972604 KLHL28 4 0.0497332 CSTB 4 0.04979822 AIF1 4 0.04983331 DECR2 4 0.04990344 KATNB1 4 4 4 4 How can I remove the trailing... (3 Replies)
Discussion started by: evelibertine
3 Replies

4. Shell Programming and Scripting

Conversion of spaces Text file into CSV format file

Input file (each line is separaed by spaces )given below: Name Domain Contact Phone Email Location ----------------------- ------------------------------------------------ ------- -----... (18 Replies)
Discussion started by: sreenath1037
18 Replies

5. UNIX for Dummies Questions & Answers

How to convert text to columns in tab delimited text file

Hello Gurus, I have a text file containing nearly 12,000 tab delimited characters with 4000 rows. If the file size is small, excel can convert the text into coloumns. However, the file that I have is very big. Can some body help me in solving this problem? The input file example, ... (6 Replies)
Discussion started by: Unilearn
6 Replies

6. Shell Programming and Scripting

format text file

i have a text file in this format: name1^A1^B1^ name2^A2^B2^ ... namex^Ax^Bx^ name1^AA1^ name2^AA2^ ... namex^AAx^ name1^AAA1^BBB1^ name2^AAA1^BBB2^ ... namex^AAAx^BBBx^ name1^AAAA1^ name2^AAAA2^ ... namex^AAAAx^ i want to generate a file: (2 Replies)
Discussion started by: busystock
2 Replies

7. Shell Programming and Scripting

Uniform length for all the lines in file

Hi, I have a file with different width for each line. like first line with 45characters and second line of 30 characters. But I want to make all the lines to 45 characters in file. Appreciate your inputs Thanks Arun: (1 Reply)
Discussion started by: arund_01
1 Replies

8. Shell Programming and Scripting

Changing the text file format

Hi, I have a shell script to unload all the empname who have salary >50000 from the emp table into a text file(empname.txt) . m_db unload "$dbc_file" -column_delimiter ',' -select "SELECT empname FROM emp where salary > 50000" >> empname.txt Now my text file have data in the following format ... (3 Replies)
Discussion started by: kavithakuttyk
3 Replies

9. Shell Programming and Scripting

Format a text file

I have a file that gets created by pasting 3 files together to get the one file. The output of the 3 files follows. sft.rtf c3_critappdb_u5 start 04:22:20 end 08:03:41 c3_critappdb_u6 start 18:01:31 end 20:21:19 c3_critappdb_u7 start 00:02:50 end 08:30:17 c3_critappdb_u8 start 17:00:42 end... (2 Replies)
Discussion started by: jhardy
2 Replies

10. Shell Programming and Scripting

use of format file to extract columns from a source file

hi experts lets say my format file is B B ========= column no,name,type,length 1,ee,N,12 3,hj,N.4 4,kl,N,5 source file ======== d e f g h i 5 8 9 7 6 5 1 3 4 5 6 6 (2 Replies)
Discussion started by: subhendu81
2 Replies
Login or Register to Ask a Question