LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-29-2012, 09:06 AM   #1
leniviy
Member
 
Registered: Jul 2009
Distribution: Archlinux
Posts: 69

Rep: Reputation: 4
shell convert tab chars to n spaces where n=8 - ($col % 8)


Try this script:
Code:
printf "a\tb \tc  \td   \te\n"; printf "a\tb\tc\td\te\n"
the output in terminal is:
Code:
a       b       c       d       e
a       b       c       d       e
but the number of characters is different.
How to convert the tab characters in the piped input to spaces, so the result looks the same?
I'm thinking of awk:
Code:
( printf "a\tb \tc  \td   \te\n"; printf "a\tb\tc\td\te\n" ) | awk '{
  s="";
  col=0;
  for (i=1; i<=length;i++) {
    c=substr($0,i,1);
    #print "col:", col, "\"" c "\""
    if (c == "\t") {
      j = 8 - (col % 8);
      #print "remainder:", j;
      s = s substr("        ", 1, j );
      col += j;
    } else {
      col++;
      s = s c;
    }
  }
  print s;
}'

Last edited by leniviy; 11-29-2012 at 10:03 AM.
 
Old 11-29-2012, 10:01 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can try the expand command:
Code:
$ (printf "a\tb \tc  \td   \te\n"; printf "a\tb\tc\td\te\n") | expand -
a       b       c       d       e
a       b       c       d       e
The output has no tabs, but equally spaced fields.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need bash script to remove spaces and non alpha chars from folders/ files ne0shell Programming 6 06-22-2012 11:10 AM
[SOLVED] Plugin for Gedit to convert TAB to spaces Mr. Alex Linux - Software 2 01-16-2012 02:17 AM
shell question: pad end of each line with spaces to = 80 chars ?? di11rod Programming 19 04-21-2011 07:03 PM
[SOLVED] How can I know how many spaces equals a tab in a linux terminator. e3399 Linux - Newbie 5 12-09-2010 03:16 PM
How to set TAB to 4 spaces in vim? kornerr Linux - General 2 01-05-2006 01:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration