Dynamic Columns
【Question】
I have the following sample tab-delimited file:
CvR Col_1 Col_2 Col_3 Col_4 Col_5
S1 1 0 1 0 1
S2 1 1 1 0 1
S3 1 1 1 1 1
S4 1 0 1 1 1
S5 1 0 1 1 1
I am trying to come up with a simple way to print the first column and all the other columns with just “1” values in them.
My desired output file should look like this:
CvR Col_1 Col_3 Col_5
S1 1 1 1
S2 1 1 1
S3 1 1 1
S4 1 1 1
S5 1 1 1
My actual input file will be much bigger. I would like to do this in UNIX where possible. Can anybody help? Thanks.
#!/bin/bash
clear
value='\\(\[01\]\\)'
cp file file2
for i in 1 2 3 4 5 6; do
sed -i "s/ ${value}/ val${i}_\\1/" file2
done
rowcount=$(wc -l <file2)
for i in 1 2 3 4 5 6; do
if \[$(grep -c val${i}_1 file2)-eq ${rowcount} \]; then
sed -i "s/val${i}_./1/" file2
else
sed -i "s/Col_${i}//" file2
sed -i "s/val${i}_.//" file2
fi
done
cat file2
【Answer】
It’s simple to generate columns dynamically in SPL:
A |
|
1 |
=file("/user/data.txt").import@t() |
2 |
=A1.fname().to(2,).select(A1.field(~).count(~==1)==A1.count()) |
3 |
=A1.new(${"\'.CvR\',"+A2.concat@c()}) |
esProc supports windows\unix command line and integration with JAVA. For more information, see Text Files
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL