Harinder
Harinder

Reputation: 21

I need to cut particular text from file to display only particular column using BASH script

I have a bash script that call another bash script and stores output of that in a text file. Now I would like to delete column from that file. For example following file, I need to display only COUNT, CKA CLASS AND CKA LABEL column and delete rest of the section. If I cut the column using awk or sed it will start cutting from PKCS11 provider line

PKCS11 provider found specified slot label: admin (slot: 76147, index: 14)
+-------+-----------------+------------------------------------------------------------------+------------------------------------------------------------------+--------------+-------------+-------------+------------+
| COUNT |    CKA CLASS    |                            CKA LABEL                             |                              CKA ID                              | CKA KEY TYPE | CKA KEY LEN | CKA SUBJECT | CKA ISSUER |
+-------+-----------------+------------------------------------------------------------------+------------------------------------------------------------------+--------------+-------------+-------------+------------+
|   001 | PRIVATE_KEY     | d4a12e598081d863ff371904d42ba6b0d1b19b1f4990477d60sdadafdfdfdffd | c3a12e508081d868c3633c79aa3ff371904d42ba6b0d1b19b1f4040477d6070a | CKK_ECDSA    |           0 |             |            |
|   002 | PUBLIC_KEY      | e5a12g7025l1d868c36331904d42baa6b0d1b19b1ggf4990477dsfdsdfs6070a | d4a12e59891cd868c3633c79aa3ff371904d42b77d6070asfewerww345a122rs | CKK_ECDSA    |           0 |             |            |
+-------+-----------------+------------------------------------------------------------------+------------------------------------------------------------------+--------------+-------------+-------------+------------+

I would like to get the output like this:

PKCS11 provider found specified slot label: admin (slot: 76147, index: 14)
| COUNT |  CKA CLASS  |                            CKA LABEL                             |     
+-------+-----------------+------------------------------------------------------------------+
|   001 | PRIVATE_KEY | ed53bc0ef5cbe3aa0d8994d27dsfdsdfsdfdsf9273f2810defc159e310743d92 |
|   002 | PUBLIC_KEY  | ed53bc0ef5cbe3aa0d8994d27b922ce03dsdfsdd73f2810defc159e310743d92 |  

Upvotes: 0

Views: 73

Answers (2)

builder-7000
builder-7000

Reputation: 7627

You could use cut twice:

cut -d"|" -f-4 inputfile | cut -d"+" -f-4

output:

PKCS11 provider found specified slot label: admin (slot: 76147, index: 14)
+-------+-----------------+------------------------------------------------------------------
| COUNT |    CKA CLASS    |                            CKA LABEL                             
+-------+-----------------+------------------------------------------------------------------
|   001 | PRIVATE_KEY | d4a12e598081d863ff371904d42ba6b0d1b19b1f4990477d60sdad 
|   002 | PUBLIC_KEY  | e5a12g7025l1d868c36331904d42ba6b0d1b19b1f4990477d6070a 
+-------+-----------------+------------------------------------------------------------------

Upvotes: 1

David C. Rankin
David C. Rankin

Reputation: 84541

A simple way to approach it is to get rid of the lines you don't want with sed and then just print out the first 94 characters of the lines that remain, e.g.

$ while read line; do echo "${line:0:94}"; done < <(sed '2d;$d' file)
PKCS11 provider found specified slot label: admin (slot: 76147, index: 14)
| COUNT |    CKA CLASS    |                            CKA LABEL                             |
+-------+-----------------+------------------------------------------------------------------+
|   001 | PRIVATE_KEY     | d4a12e598081d863ff371904d42ba6b0d1b19b1f4990477d60sdadafdfdfdffd |
|   002 | PUBLIC_KEY      | e5a12g7025l1d868c36331904d42baa6b0d1b19b1ggf4990477dsfdsdfs6070a |

(note: if you are not using bash where process substitution is available to feed the while loop, you can just call the sed expression first and pipe the result to the while loop.)


Edit Based on Comment

If the width of the first 3 fields of the format isn't known before hand, you can add a command substitution to determine the total width, for example:

$ width=$(awk -F'|' '{print length($2)+length($3)+length($4)+4}' < <(sed -n 3p file))
  while read line; do
      echo "${line:0:$width}"
  done < <(sed '2d;$d' file)
PKCS11 provider found specified slot label: admin (slot: 76147, index: 14)
| COUNT |    CKA CLASS    |                            CKA LABEL                             |
+-------+-----------------+------------------------------------------------------------------+
|   001 | PRIVATE_KEY     | d4a12e598081d863ff371904d42ba6b0d1b19b1f4990477d60sdadafdfdfdffd |
|   002 | PUBLIC_KEY      | e5a12g7025l1d868c36331904d42baa6b0d1b19b1ggf4990477dsfdsdfs6070a |

Another slightly shorter way that eliminates the while loop and uses cut to trim to the combined width of the first 3 fields with delimiters could be:

$ width=$(awk -F'|' '{print length($2)+length($3)+length($4)+4}' < <(sed -n 3p file))
sed '2d;$d' file | cut -c -$width
PKCS11 provider found specified slot label: admin (slot: 76147, index: 14)
| COUNT |    CKA CLASS    |                            CKA LABEL                             |
+-------+-----------------+------------------------------------------------------------------+
|   001 | PRIVATE_KEY     | d4a12e598081d863ff371904d42ba6b0d1b19b1f4990477d60sdadafdfdfdffd |
|   002 | PUBLIC_KEY      | e5a12g7025l1d868c36331904d42baa6b0d1b19b1ggf4990477dsfdsdfs6070a |

Upvotes: 1

Related Questions