The 9 paths to OpenSim enlightenment:
With sed
and some shell magic:
sed 's/=.*//' OpenSimStats.txt | paste -sd, >out.csv
sed 's/.*=//; s/[^0-9]*$//' OpenSimStats.txt | paste -sd, >>out.csv
With sed
, without shell magic:
sed -n 's/=.*//; 1{ h; b; }; $! H; $ { x; s/\n/,/g; p; }' OpenSimStats.txt >out.csv
sed -n 's/.*=//; 1{ s/[0-9]*$//; h; b; }; s/[^0-9]*$//; $! H; $ { x; s/\n/,/g; p; }' OpenSimStats.txt >>out.csv
With shell magic and a tiny bit of sed
:
paste -sd, <(cut -d= -f1 OpenSimStats.txt) <(cut -d= -f2 OpenSimStats.txt | sed 's/[^0-9]*$//')
With cut
and some shell magic:
cut -d= -f1 OpenSimStats.txt | paste -sd, >out.csv
cut -d= -f2 OpenSimStats.txt | sed 's/[^0-9]*$//' | paste -sd, >>out.csv
With GNU datamash
:
sed 's/=/,/; s/[^0-9]*$//' OpenSimStats.txt | datamash -t, transpose
With perl
:
perl -lnE 's/\D+$//o;
($a, $b) = split /=/;
push @a, $a; push @b, $b;
END { $, = ","; say @a; say @b }' OpenSimStats.txt
With grep
:
grep -o '^[^=]*' OpenSimStats.txt | paste -sd, >out.csv
egrep -o '[0-9.]+' OpenSimStats.txt | paste -sd, >>out.csv
With bash
:
#! /usr/bin/env bash
line1=()
line2=()
while IFS='=' read -r a b; do
line1+=("$a")
[[ $b =~ ^[0-9.]+ ]]
line2+=("$BASH_REMATCH")
done <OpenSimStats.txt
( set "${line1[@]}"; IFS=,; echo "$*" ) >out.csv
( set "${line2[@]}"; IFS=,; echo "$*" ) >>out.csv
With awk
:
awk -F= '
NR==1 { a = $1; sub(/[^0-9]+$/, "", $2); b = $2; next }
{ a = a "," $1; sub(/[^0-9]+$/, "", $2); b = b "," $2 }
END { print a; print b }' OpenSimStats.txt
Bonus 10th path for data nerds, with csvtk
:
csvtk replace -d= -f 2 -p '\D+$' -r '' <OpenSimStats.txt | csvtk transpose
Bonus 11th path with vim
:
:%s/\D*$//
:%s/=/\r/
qaq
:g/^\D/y A | normal dd
:1,$-1 s/\n/,/
"aP
:2,$-2 s/\n/,/
:d 1
:w out.csv
Best Answer
Python
Add to file named
csv2tab
, and make it executabletouch csv2tab && chmod u+x csv2tab
Add to it
Test runs