问题描述:

I have a file with multiple KV pairs.

Input:

$ cat input.txt

k1:v1 k2:v2 k3:v3

...

I am only interested in the values. The keys (name) are just to remember what each value meant. Essentially I am looking to cut the keys out so that I can plot the value columns.

Output:

$ ...

v1 v2 v3

Is their a single-liner bash command that can help me achieve this?

UPDATE


This is how I am currently doing it (looks ugly)

>> cat input.txt | python -c "import sys; \

lines = sys.stdin.readlines(); \

values = [[i.split(':')[1] for i in item] for item in \

[line.split() for line in lines]]; \

import os; [os.system('echo %s'%v) for v in \

['\t'.join(value) for value in values]]" > output.txt

网友答案:

is this ok for you?

sed -r 's/\w+://g' yourfile

test:

kent$  echo "k1:v1 k2:v2 k3:v3"|sed -r 's/\w+://g'                                    
v1 v2 v3

update well, if your key contains "-" etc: see below

kent$  echo "k1#-$%-^=:v1 k2:v2 k3:v3"|sed -r 's/[^ ]+://g'  
v1 v2 v3
网友答案:
awk -v FS=':' -v RS=' ' -v ORS=' ' '{print $2}' foo.txt 

http://www.gnu.org/software/gawk/manual/gawk.html#Field-Separators

网友答案:

I see sed, awk and python, so here's plain bash:

while IFS=' ' read -a kv ; do printf '%s ' "${kv[@]#*:}" ; done < input.txt

Just for good measure, here's a perl version:

perl -n -e 'print(join(" ",values%{{@{[split(/[:\s]/,$_)]}}})," ")' < input.txt

The order of the values changes, though, so it's probably not going to be what you want.

网友答案:

Solution with awk:

awk '{split($0,p," "); for(kv in p) {split(p[kv],a,":"); printf "%s ",a[2];} print ""}' foo.txt
网友答案:

Try this

Input.txt

k1:v1 k2:v2 k3:v3

Code

 awk -F " " '{for( i =1 ; i<=NF ;i+=1) print $i}' Input.txt | cut -d ":" -f 2 | tr '\n' ' '

Output

v1 v2 v3

相关阅读:
Top