简体   繁体   中英

formatting text using awk

Hi I have the following text and I need to use awk or sed to print 3 separate columns

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? 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 ...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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