user3613649
user3613649

Reputation: 5

Convert string output(bash script)

How do I convert my output vendor_id : GenuineIntel to vendor_id = GenuineIntel using the cut command?

#!/bin/bash

VENDORID=`cat /proc/cpuinfo | grep 'vendor_id'|cut -d`=`-f 5`
vendor_id: GenuineIntel
echo $VENDORID

Upvotes: 0

Views: 43

Answers (3)

Andreas Louv
Andreas Louv

Reputation: 47099

You can use translate:

vendorid=$(grep 'vender_id' /proc/cpuinfo | tr ':' '=')
printf "%s\n" "$vendorid"

I changed backticks to $(..) since they are easier to nest. Also remember to double quote your variable expansions $vendorid -> "$vendorid" or it will undergo word splitting.

tr will in this case change all colons to equal signs, eg:

% echo "a:b:c" | tr ':' '='
a=b=c

Upvotes: 1

Cyrus
Cyrus

Reputation: 88583

VENDORID=$(sed -n '/vendor_id/{s/:/=/p;q}' /proc/cpuinfo)

Upvotes: 1

Walter A
Walter A

Reputation: 19982

Translate the character :into = with

 tr ':' '=' < /proc/cpuinfo

Assign to a variable with

vendorid=$(tr ':' '=' < /proc/cpuinfo)

Upvotes: 0

Related Questions