Fun with character matrices
I'm playing with character matrices and learning how to manipulate them.
Example 1, Horizontal rotation. Take a character matrix and rotate each row right by r where r is the row index minus 1. This transformation might be called a "skew" or "twist." If the input file is ... Code:
abcde Code:
abcde Code:
sed 's/.*/&&/' $InFile \ Example 2, Vertical rotation. Do a similar "twist" on columns. Again, the input file is ... Code:
abcde Code:
aqmie Ideas? Suggestions? Daniel B. Martin |
Code:
sed 's/.*/&&/' $InFile \ Code:
awk -F "" '{print substr($0$0,(NF+2)-NR,NF)}' $InFile Code:
awk -F "" '{ for (i=1; i<=NF;i++) arr[i]=arr[i]$i } \ |
Thank you, millgates, for your constructive response.
Code:
Why the sed part? Why don't you duplicate the line in the awk command? Quote:
I like your transpose code. Code:
awk -F "" '{ for (i=1; i<=NF;i++) arr[i]=arr[i]$i } \ Daniel B. Martin |
[QUOTE=millgates;4632981]
Code:
awk -F "" '{print substr($0$0,(NF+2)-NR,NF)}' $InFile Rotate a matrix about its vertical axis. This input... Code:
abcde Code:
edcba Code:
sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' $InFile Daniel B. Martin |
Quote:
Code:
awk -F "" '{ file[NR] = $0 } END { Quote:
Quote:
Code:
rev $InFile Code:
awk -F "" '{ foo=""; for (i = 1; i <= NF; i++) foo=$i foo; print foo }' $InFile |
Quote:
Code:
rev $InFile Daniel B. Martin |
Definitely cannot do better than rev, but here is a slightly shorter awk:
Code:
awk -F "" '{for(i=NF;i>=1;i--)printf "%s",$i ((i==1)?"\n":"")}' file |
To continue (after a hiatus) the fun with character matrices...
I want to create a matrix of characters such as ... Code:
abcde Code:
Nrows=4 Intuition suggests this could be accomplished with a single awk. I've struggled with code of this nature ... Code:
Nrows=4 awk gurus, please advise. Daniel B. Martin |
Well you will probably need more coding to cater for the situation where row > col, but as a start:
Code:
awk '{for(i=1;i <= Nrows;i++)print gensub("(.*)(.{"i"})$","\\2\\1",1)}' Nrows=$Nrows <<<${AL:0:Ncols} |
Quote:
Code:
echo "Method of LQ Guru grail" Code:
abcde Daniel B. Martin |
You probably have a pre 4+ version of gawk, so add --re-interval.
|
Quote:
Following your suggestion I ran this ... Code:
echo "Method of LQ Guru grail" Code:
eabcd Code:
echo "Method of LQ Guru grail, modified" Code:
abcde Thank you! Daniel B. Martin |
Quote:
I tried to piece it apart and am stumped by "<<<". It's not in my Dougherty & Robbins "sed & awk" book. The search facility of LQ seems to misunderstand <<< as a search field. Google doesn't do any better, so my efforts at self-education are unsuccessful. Therefore I must ask for explanation, elaboration, and pointers to relevant documentation. Daniel B. Martin |
That would be because it is a shell (specifically bash) construct known as a here string, as opposed to a here document:
http://mywiki.wooledge.org/HereString |
Quote:
A side question: Is it possible to search LQ for a string of special characters such as <<< ? Is it possible to tell Google to search on such strings? Daniel B. Martin |
All times are GMT -5. The time now is 05:56 AM. |