简体   繁体   English

使用awk格式化文本

[英]formatting text using awk

Hi I have the following text and I need to use awk or sed to print 3 separate columns 嗨,我有以下文字,我需要使用awk或sed来打印3个单独的列

11/13/14    101 HUDSON AUBONPAINJERSEY CITY NJ      $4.15
11/22/14    MTAMVM*110TH ST/CATNEW YORK NY          $19.05
11/22/14    DUANE READE #14226 0NEW YORK NY         $1.26

So I like to produce a file containing all the dates. 所以我想产生一个包含所有日期的文件。 Another file containing all the description and third file containing all the numbers 包含所有描述的另一个文件和包含所有数字的第三个文件

I can use an awk to print the first column printy $1 and then use -F [$] option to print last column but I'm not able to just print the middle column as there are spaces etc. Can I ignore the spaces? 我可以使用awk打印第一列printy $ 1,然后使用-F [$]选项打印最后一列,但由于有空格等,所以我不能仅打印中间列。我可以忽略空格吗? or is there a better way of doing this? 还是有更好的方法呢?

Thaking you in advance 提前解冻

Try doing this : 尝试这样做:

 $ awk '
     {
         print $1 > "dates"; $1=""
         print $NF > "prices"; $NF=""
         print $0 > "desc"
     }
' file

or : 要么 :

awk -F'  +' '
    {
        print $1 > "dates"
        print $2 > "desc"
        print $3 > "prices"
    }
' file 

Then : 然后 :

$ cat dates
$ cat desc
$ cat prices

Wasn't fast enough to be the first to give an awk solution, so here's one with grep and sed ... 还不够快,无法成为第一个提供awk解决方案的人,所以这里有grepsed ...

grep -o '^.*/.*/1.' file   #first col
sed 's/^.*\/.*\/1.//;s/\$.*//' file   #middle col
grep -o '\$.*$' file    #last col

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM