Brayan Pastor
Brayan Pastor

Reputation: 984

how to remove columns except the last one?

I want to move the columns except for the last, I do not know if there was any way to do it with awk or sed or some other line command

 ...
    00000000: 30327c30 30303131 36333132 567c317c  0|2011002136|1|V
    00000010: 44204149 56452045 4d415449 544e4549  IA DE EVITAMIENT
    00000020: 7c007c4f 30327c00 302d3131 37312d33  O|.|.|2011-03-17
    00000030: 3a393020 303a3035 33427c30 38313957   09:50:00|B3W918
    00000040: 327c317c 7c39397c 4f52544f 4f4d7c53  |1|2|99|OTROS|MO
    00000050: 414b4f54 7c007c52 7c007c00 36312d47  TOKAR|.|.|.|G-16
    00000060: 7c54527c 444e4f43 52494355 204e5520  |RT|CONDUCIR UN 
    00000070: 49484556 4f4c5543 524f5020 414e5520  VEHICULO POR UNA
    ...

i tried, but only delete the first column

$ cat file | sed -E $'s/ +/\t/g' | cut -f2-

i want this result. The last column

    ...
    0|2011002136|1|V
    IA DE EVITAMIENT
    O|.|.|2011-03-17
    09:50:00|B3W918
    |1|2|99|OTROS|MO
    TOKAR|.|.|.|G-16
    |RT|CONDUCIR UN 
    VEHICULO POR UNA
    ...

Upvotes: 2

Views: 894

Answers (6)

Alex Harvey
Alex Harvey

Reputation: 15472

The simplest solutions don't seem to have been mentioned.

You really just want the characters beginning in the 52nd position. So use cut:

Input:

▶ cat > FILE <<EOF
    00000000: 30327c30 30303131 36333132 567c317c  0|2011002136|1|V
    00000010: 44204149 56452045 4d415449 544e4549  IA DE EVITAMIENT
    00000020: 7c007c4f 30327c00 302d3131 37312d33  O|.|.|2011-03-17
    00000030: 3a393020 303a3035 33427c30 38313957   09:50:00|B3W918
    00000040: 327c317c 7c39397c 4f52544f 4f4d7c53  |1|2|99|OTROS|MO
    00000050: 414b4f54 7c007c52 7c007c00 36312d47  TOKAR|.|.|.|G-16
    00000060: 7c54527c 444e4f43 52494355 204e5520  |RT|CONDUCIR UN
    00000070: 49484556 4f4c5543 524f5020 414e5520  VEHICULO POR UNA
EOF

Output:

▶ cut -c52- FILE
0|2011002136|1|V
IA DE EVITAMIENT
O|.|.|2011-03-17
 09:50:00|B3W918
|1|2|99|OTROS|MO
TOKAR|.|.|.|G-16
|RT|CONDUCIR UN
VEHICULO POR UNA

Note that cut can be emulated in sed this way, as noted in a comment:

▶ sed -E 's/.{51}//' FILE

Another way is to use AWK to set the field widths, if you have a version of AWK that supports it like GNU AWK, nawk (Mac OS X) etc.

▶ gawk 'BEGIN {FIELDWIDTHS = "15 9 9 9 9 16"} {print $6}' FILE
0|2011002136|1|V
IA DE EVITAMIENT
O|.|.|2011-03-17
 09:50:00|B3W918
|1|2|99|OTROS|MO
TOKAR|.|.|.|G-16
|RT|CONDUCIR UN
VEHICULO POR UNA

The benefit of the AWK solution is, while a little more effort to set up, it communicates more information about your expectations of the data to the reader of your code.

To figure out where all the columns begin, you can use CTRL + g in Vim after positioning the cursor on them.

Upvotes: 1

Claes Wikner
Claes Wikner

Reputation: 1517

awk '{print "    "$NF}' file
...
0|2011002136|1|V
EVITAMIENT
O|.|.|2011-03-17
09:50:00|B3W918
|1|2|99|OTROS|MO
TOKAR|.|.|.|G-16
UN
UNA
... 

Upvotes: 1

oguz ismail
oguz ismail

Reputation: 50750

Seems like you're just trying to get last 16 chars.

$ sed 's/.*\(.\{16\}\)/\1/' file
0|2011002136|1|V
IA DE EVITAMIENT
O|.|.|2011-03-17
 09:50:00|B3W918
|1|2|99|OTROS|MO
TOKAR|.|.|.|G-16
|RT|CONDUCIR UN
VEHICULO POR UNA

I took a closer look at your sample and realized that it is a hex dump generated with xxd -e. If you want to obtain the original file, use following:

$ xxd -r file | xxd -e | xxd -r > original_file
$ cat original_file
0|2011002136|1|VIA DE EVITAMIENTO|||2011-03-17 09:50:00|B3W918|1|2|99|OTROS|MOTOKAR||||G-16|RT|CONDUCIR UN VEHICULO POR UNA

P.S: original_file contains NUL bytes but they're not shown on terminal.

Upvotes: 3

user7712945
user7712945

Reputation:

if your data in d file, tried on gnu awk

awk -F' [a-f0-9]{8}  ' '{print $2}' d

tried on gnu sed

sed -E 's/.*\s[a-f0-9]{8}\s{2,}(.*)/\1/' d

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203532

Just delete the first 5 space-separated fields.

With GNU sed:

$ sed -E 's/(\s+\S+){5}\s+//' file
0|2011002136|1|V
IA DE EVITAMIENT
O|.|.|2011-03-17
09:50:00|B3W918
|1|2|99|OTROS|MO
TOKAR|.|.|.|G-16
|RT|CONDUCIR UN
VEHICULO POR UNA

With any POSIX sed:

$ sed 's/\([[:space:]]*[^[:space:]]*\)\{5\}[[:space:]]*//' file
0|2011002136|1|V
IA DE EVITAMIENT
O|.|.|2011-03-17
09:50:00|B3W918
|1|2|99|OTROS|MO
TOKAR|.|.|.|G-16
|RT|CONDUCIR UN
VEHICULO POR UNA

Upvotes: 1

stevesliva
stevesliva

Reputation: 5655

Alternative to print final 16 chars with grep:

grep -Po '.{16}$' file

or

grep -o '.\{16\}$' file

  • -P for perl regexes
  • -o for only-matching

Upvotes: 2

Related Questions