Proxx
Proxx

Reputation: 151

Separate variable to $1 $2 $3

sqlite3 pcheck.db "SELECT * FROM printers"

gives me:

 1|10.0.0.x|HP4250INK|public|hp LaserJet 4250|"CNHXXXXXX"|14457|"Sleep Mode"

i want to put the data separated by | into variables $1 to $8

so that is can output it like

echo "$1 $3 $5"
echo "$2 $4 $6"

./test.sh

1 HP4250INK hp Laserjet 4250
10.0.0.x public "CNHXXXXXX"

Upvotes: 1

Views: 1857

Answers (3)

bash-o-logist
bash-o-logist

Reputation: 6911

you can just use bash without external tools

output=$(sqlite3 pcheck.db "SELECT * FROM printers")
IFS="|"
set -- $output
echo $1
echo $2 #and so on

or use arrays

IFS="|"
output=($(sqlite3 pcheck.db "SELECT * FROM printers"))
echo ${output[0]}
echo ${output[1]} #and so on

Upvotes: 9

Wolph
Wolph

Reputation: 80021

Try using awk for this :)

awk 'BEGIN { FS = "|" } ; { print $1 " " $3 " " $5 }'

Or:

awk 'BEGIN { FS = "|" } ; { printf "%s\t%st\%s\n%s\t%s\t%s", $1, $3, $5, $2, $4, $6 }'

Upvotes: 1

Alberto Zaccagni
Alberto Zaccagni

Reputation: 31560

You could use

echo $YOUR_STRING | cut -d'|' -f1,3,5

for the first three, and

echo $YOUR_STRING | cut -d'|' -f2,4,6

for the other three.

Upvotes: 2

Related Questions