开发者

Separate variable to $1 $2 $3

sqlite3 pcheck.db "SELECT * FROM printers"

gives me:

 1|10.0.0.x|HP4250INK|public|hp Las开发者_运维技巧erJet 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"


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


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.


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 }'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜