LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-09-2011, 12:33 PM   #1
mystupidquestion
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Rep: Reputation: Disabled
Why is my awk script producing extra spacing between the first and second column?


I wrote the following awk program:
Code:
cat "$fileToProcess" | awk '!(/LINUX|.inux/ || /\%/ || /.verage/ || /^$/)\
  {
    ("date --date="$1 $2" +%T") | getline correctTime;
      printf "%s\t", correctTime;
      for (i=3; i<NF; i++) printf "%s\t", $i;
      printf "\n"
      close("date --date="$1 $2" +%T")
    }'
to parse a file. The program works as expected, but I'm curious as to why the spacing between column 1 and column 2 is greater than the other columns. Here is a sample of the output:
Code:
20:10:01        all     19.31   0.00    2.90    0.24    0.00    
20:20:01        all     10.30   0.00    1.97    0.22    0.00    
20:30:01        all     11.36   0.00    2.26    0.19    0.00    
20:40:01        all     12.04   0.00    2.37    0.20    0.00
 
Old 10-09-2011, 12:42 PM   #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
It's not an awk problem: it is the way the TAB separator works. By default TAB is 8-spaces length, the first string is 8-characters so that a complete TAB is printed out immediately after that. Look at the spacing of your output:
Code:
-------8-------8-------8-------8-------8-------8-------8-------8
20:10:01        all     19.31   0.00    2.90    0.24    0.00    
20:20:01        all     10.30   0.00    1.97    0.22    0.00    
20:30:01        all     11.36   0.00    2.26    0.19    0.00    
20:40:01        all     12.04   0.00    2.37    0.20    0.00
-------8-------8-------8-------8-------8-------8-------8-------8
Every field is followed by a variable number of spaces to fill the 8-characters TAB separator (included the last one).
 
1 members found this post helpful.
Old 10-09-2011, 12:51 PM   #3
mystupidquestion
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
ic. Thanks!
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Not able to extract last column from awk vinaytp Linux - Newbie 4 05-20-2011 04:27 AM
awk multiple column into single column ilukacevic Programming 49 07-19-2010 07:23 PM
Column statistic by awk ? cs24 Programming 7 01-15-2010 05:41 AM
LXer: Double Spacing In Awk, Perl and Shell on Linux and Unix LXer Syndicated Linux News 0 07-08-2008 06:41 AM
Need to make multiple lines into one with no extra spacing with shell script. sunksullen Programming 10 05-10-2007 05:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:30 AM.

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