2013-06-03 74 views
3

我想用选项卡分隔列生成日志文件。它应该有制表符分隔输出的格式如下,但所有的注释字段Linux命令以布局选项卡分隔列表很好

time  date  alias comment 
10:09:20 03/06/13 jre  This is a test comment 

我使用csh历史的目的

set time = `perl -MPOSIX -e 'print POSIX::strftime("%T", localtime)'` 
set date = `perl -MPOSIX -e 'print POSIX::strftime("%d/%m/%y", localtime)'` 
set alias = jre 
set comment = "This is a test comment" 

配管我的文字column -t

echo "time\tdate\talias\tcomment" | column -t > somefile 
echo "$time\t$date\t$alias\t$comment" | column -t >> tt 

我几乎得到我想要的。但是,我的注释字段中的空格也会更改为制表符。有没有一种方法可以分隔前三个字段,但在评论字段中保持空间分隔?

回答

2

由于您的评论是在最后一栏,请试着使用paste。例如:

paste <(echo -e "this\tis\ttab\tseparated") <(echo "this is your comment") 

默认情况下,paste也加入到制表符中。

+0

感谢您的解决方案。我在csh中收到错误消息'Missing name for redirect',但没有在bash中。我认为这涉及到无关的空白字符。 csh是非常令人沮丧的 – moadeep

+0

'bash -c paste <(echo -e“this \ tis \ ttab \ tseparated”)<(echo“This is your comment”)'窍门 – moadeep

+0

很酷。很高兴你把事情解决了 :-) – Steve

2

一种选择是做所有的工作与

awk ' 
    BEGIN { 
     OFS = "\t" 
     header = "time date alias comment" 
     gsub(/\s+/, OFS, header) 
     print header 

     out[0] = strftime("%T", systime()) 
     out[1] = strftime("%d/%m/%y", systime()) 
     out[2] = "jre" 
     out[3] = "This is a test comment" 

     l = length(out) 
     for (i = 0; i < l; i++) { 
      printf "%s%s", out[ i ], i == l ? "" : OFS; 
     } 
     printf "\n" 
    } 
' 

它产生:

time date alias comment 
11:45:00 03/06/13 jre This is a test comment 

它不只是在标题下方打印,但这种情况下,它应该是更好地改为使用格式化的打印件(printf)。

2

这可能会为你工作:

printf 'Time\tDate\tAlias\tComment\n' > file 
printf '%s\t%s\t%s\t%s\n' $(date '+%T') $(date '+%d/%m/%y') jre "This is a comment" >> file