Register
It is currently Thu Apr 17, 2014 6:12 pm

make fix-width script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Thu Jun 10, 2010 1:20 pm   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
Hi everyone,

I'm trying to make a script for making fixed-width output.
I have it for CSV files but I'd like to make it for files that are sepparated
by spaces or tabs, how can I do this? This sepparators may also repeat themselves
which in that case, it should just ignore the repetitions and take it as one
Anyone with ideas on how to improve my script?


Code:
#!/usr/bin/env bash
width=20
IFS="," read -r -a array < file.txt
numElems=${#array[*]}

for (( j = 0; j < $numElems-1; j++ )); do
   format=$format"%"$width"s"
done
format=$format"%"$width"s\n"

while IFS="," read -r -a array; do
      
   line=""
   for (( i = 0; i < $numElems; i++ )); do
      line="$line${array[i]}\t"
   done
   printf $format "${array[@]}"
   
done < "file.txt"


Last edited by afflictedd2 on Sun Jun 13, 2010 1:12 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Sun Jun 13, 2010 11:04 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 511
Do you mind editing your post to erase this horrible thing you seem to call script? plz!

however, usual IFS is ‘‘<space><tab><newline>’’ so you don't have to care about repeated spaces or tabs.
see man bash /Shell Variables /IFS


Top
 Profile  
 PostPosted: Sun Jun 13, 2010 1:13 pm   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
Better? you're complaining about the indenting, right?

Anyhow.. I know the <tab><enter> are \t \n respectively, what is space?


Top
 Profile  
 PostPosted: Sun Jun 13, 2010 2:13 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 511
no, I mean it doesn't work.

about IFS, it is <space><tab><newline>, by default. that's all.
see it through
Code:
od -c <<<"$IFS"
0000000      \t  \n  \n
0000004
there's a space before \t

say you've got a file.txt like
Code:
foo    bar      baz             foobar          barbaz bazfoo
foo bar baz     foobar   barbaz          bazfoo
foo bar baz foobar barbaz bazfoo
foo bar baz foobar barbaz bazfoo
foo bar                 baz              foobar    barbaz       bazfoo

Code:
#!/usr/bin/env bash

width=20

#while IFS="," read -r -a array; do
while read -r -a array; do
   printf %${width}s "${array[@]}"$'\n'
done < "file.txt"
I hope this works as you expect.


Top
 Profile  
 PostPosted: Wed Dec 22, 2010 1:37 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Howdy folks!

I know this is an old post, but if the goal is just to have nicely formatted output, why not just do something like:

Code:
column -t file.txt


The file.txt that I used was a copy/paste of randomly spaced foo/bar/baz text in one of Watael so politely worded posts :)

The input and output look something like this:

Code:
$ cat > file.txt
foo    bar      baz             foobar          barbaz bazfoo
foo bar baz     foobar   barbaz          bazfoo
foo bar baz foobar barbaz bazfoo
foo bar baz foobar barbaz bazfoo
foo bar                 baz              foobar    barbaz       bazfoo

$ column -t file.txt
foo  bar  baz  foobar  barbaz  bazfoo
foo  bar  baz  foobar  barbaz  bazfoo
foo  bar  baz  foobar  barbaz  bazfoo
foo  bar  baz  foobar  barbaz  bazfoo
foo  bar  baz  foobar  barbaz  bazfoo


This is probably way too late to help the original poster, but hopefully it'll help somebody!

-J


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP