Issue
My goal is to convert file with 2 1 0 to file with 1 0 -1 via A H B. For example,
Infile.txt Temp.txt Final.txt
2 2 2 1 1 1 0 0 A A A H H H B B 1 1 1 0 0 0 -1 -1
I was able to convert from numerics(2 1 0) to characters(A H B) using the code as follows:
cut -f2- Infile.txt | sed '1,1d' | sed 's/2/A/g' | sed 's/1/H/g' | sed 's/0/B/g' > Temp.txt
However, I could not convert from (A H B) to (1 0 -1). Hence, I got stuck with Temp.txt
So, I would appreciate any solution to figure it out. Thanks!
Solution
OP has mentioned in a comment the source file is a 20,000 x 500 (row x column) matrix of the digits 2
, 1
and 0
.
Create a 20,000 x 501 (row x column) matrix:
awk '
BEGIN { for (i=1;i<=20000;i++) {
sep=""
for (j=1;j<=167;j++) {
printf "%s2 1 0", sep
sep=" "
}
print ""
}
}
' > matrix.dat
$ head -5 matrix.dat | cut -c1-30
2 1 0 2 1 0 2 1 0 2 1 0 2 1 0
2 1 0 2 1 0 2 1 0 2 1 0 2 1 0
2 1 0 2 1 0 2 1 0 2 1 0 2 1 0
2 1 0 2 1 0 2 1 0 2 1 0 2 1 0
2 1 0 2 1 0 2 1 0 2 1 0 2 1 0
One awk/gsub()
idea:
awk '{ gsub(/1/,9)
gsub(/2/,1)
gsub(/0/,-1);
gsub(/9/,0)
}
1
' matrix.dat > matrix.awk1.out
One awk/loop
idea:
awk '{ for (i=1;i<=NF;i++)
$i=$i-1
}
1
' matrix.dat > matrix.awk2.out
One sed
idea:
sed 's/1/9/g;s/2/1/g;s/0/-1/g;s/9/0/g' matrix.dat > matrix.sed.out
These all generate the same result:
$ diff matrix.awk1.out matrix.awk2.out
$ diff matrix.awk2.out matrix.sed.out
$ head -5 matrix.awk1.out | cut -c1-35
1 0 -1 1 0 -1 1 0 -1 1 0 -1 1 0 -1
1 0 -1 1 0 -1 1 0 -1 1 0 -1 1 0 -1
1 0 -1 1 0 -1 1 0 -1 1 0 -1 1 0 -1
1 0 -1 1 0 -1 1 0 -1 1 0 -1 1 0 -1
1 0 -1 1 0 -1 1 0 -1 1 0 -1 1 0 -1
Run times:
- system:
cygwin
(in a VM),awk 5.1.1
,sed 4.8
- 5.5 secs :
awk/gsub()
- 3.9 secs :
awk/loop
- 5.9 secs :
sed
Answered By - markp-fuso Answer Checked By - Clifford M. (WPSolving Volunteer)