Search code examples
shellcut

How to cut the file into two fields in shell?


Please unzip the file which download from :
http://s.yunio.com/MT3r2x
The data format is :

   ‘x + y’                         x plus y                                  
   ‘x - y’                         x minus y                                 
   ‘x*y’                           juxtapose x and y                         
   ‘x/y’                           x forwardslash y                          
   ‘x %+-% y’                      x plus or minus y              

For

cut -c 1-43   maths.txt  

The output is :

   ‘x + y’                         
   ‘x - y’                         
   ‘x*y’                           
   ‘x/y’                           
   ‘x %+-% y’                      

For

cut -c 44-    maths.txt  

The output is :

x plus y                                
x minus y                               
juxtapose x and y                       
x forwardslash y                        
x plus or minus y 

The two fields are what i want to get.

Why i can't use the following command to cut it into two fields?
cut -d' ' -f2 maths.txt or cut -d' ' -f2 maths.txt(ctrl+v+tab) can do neither.


Solution

  • Your cut -d' ' -f2 maths.txt or cut -d' ' -f2 maths.txt(ctrl+v+tab) are working. The problem is that they print the text in each line after first space and before second one. As you have a lot of spaces, it just shows spaces.

    What you can do is to cat the original one and pipe it using col like this:

    cat maths.txt | col | cut -d$'\t' -f1 # first col 
    cat maths.txt | col | cut -d$'\t' -f2 # second col