在 bash 上垂直翻转多个字符串
Flip multiple strings vertically on bash
我正在尝试垂直翻转任何句子的内容。因此,任何字符串的每个字符都将在同一行中垂直打印。例如:
Sample Text: This is an Example
Output expected: T i a E
h s n x
i a
s m
p
l
e
按照以下方向,我正在努力实现这一目标,但目前还做不到。
echo "Input provided by user is $@"
for i in $(seq 1 $#); do
echo ${!i} | sed 's/./ &/g' | xargs |tr ' ' '\n'
done
Current output:
T
h
i
s
i
s
a
n
E
x
a
m
p
l
e
此外,这也无济于事
echo Print text vertically | fold -c -w1
T
h
i
s
i
s
a
n
E
x
a
m
p
l
e
更多无效的替代方案:
#echo "Input provided by user is $@"
for i in $(seq 1 $#); do
content[i]=$(echo ${!i}|fold -c -w1)
#echo ${content[i]}
done
echo ${content[@]}
max
变量保存所有单词中的最大长度。对于您的文本,它将是:length('Example')
,即 7(所有单词的最大长度)
使用 awk
脚本文件:
$ awk -f script.awk <<< "This is an Example"
TiaE
hsnx
i a
s m
p
l
e
这是脚本:
{
max=0
for(i=1;i<=NF;i++)
max=length($i)>max?length($i):max;
for(j=1;j<=max;j++)
{
for(i=1;i<=NF;i++)
{
temp=substr($i, j, 1);
printf temp==""?" ":temp
}
printf "\n"
}
}
#!/bin/bash
function abc(){
maxIteration=0;
for i in $(seq 1 $#); do
j=$(echo ${!i})
if [ $maxIteration -lt ${#j} ]
then
maxIteration=${#j};
fi
done
COUNTER=0;
while [ $COUNTER -lt $maxIteration ]; do
for i in $(seq 1 $#); do
j=$(echo ${!i})
if [ ${#j} -gt $COUNTER ]
then
echo ${j:$COUNTER:1} | tr '\n' ' ';
else
echo " " | tr '\n' ' ';
fi
done
echo -e "\n"
let COUNTER=COUNTER+1
done
}
abc $@| grep .
我以前创建过一些类似的脚本。简短但完整的 POC:
#!/bin/bash
count=0
max=0
#first determine the longest string so we can later pad shorter strings with spaces
for i in $(echo "" | xargs -d: -i echo {})
do
size=$(echo $i | wc -c)
if [[ $size > $max ]]
then
max=$size
fi
done
files=""
#then echo the strings vertically inside the tmp files
for i in $(echo "" | xargs -d: -i echo {})
do
res=$(echo $i | sed 's/./ &/g' | xargs |tr ' ' '\n' > /tmp/$count.out)
#and add spaces at the end
add_space=$((max-$(echo $i | wc -c)))
for space in $(seq 0 $add_space)
do
echo " " >> /tmp/$count.out
done
files=$files" $count.out"
count=$((count+1))
done
#and finally print them side by side
pr -t -J -m -w 70 -S" " $files
我在 /tmp 下创建 tmp 文件,垂直回显字符串,然后使用 pr
打印出来。
% ./s.sh "This is an Example"
T i a E
h s n x
i a
s m
p
l
e
因为 Perl 很有趣:
perl -a script.pl <<< 'This is an Example'
T i a E
h s n x
i a
s m
p
l
e
和脚本:
@F = map { [/./g] } @F;
while (grep @{$_}, @F) {
printf "%s ", shift @{$_} || ' ' for @F;
print "\n"
}
备选脚本:
perl -pe '$r.=$/while/\S/&&s/(\S)(\S*)|\B/$r.=(||$").$";/ge}{$_=$r' \
<<< 'This is an Example'
T i a E
h s n x
i a
s m
p
l
e
我正在尝试垂直翻转任何句子的内容。因此,任何字符串的每个字符都将在同一行中垂直打印。例如:
Sample Text: This is an Example
Output expected: T i a E
h s n x
i a
s m
p
l
e
按照以下方向,我正在努力实现这一目标,但目前还做不到。
echo "Input provided by user is $@"
for i in $(seq 1 $#); do
echo ${!i} | sed 's/./ &/g' | xargs |tr ' ' '\n'
done
Current output:
T
h
i
s
i
s
a
n
E
x
a
m
p
l
e
此外,这也无济于事
echo Print text vertically | fold -c -w1
T
h
i
s
i
s
a
n
E
x
a
m
p
l
e
更多无效的替代方案:
#echo "Input provided by user is $@"
for i in $(seq 1 $#); do
content[i]=$(echo ${!i}|fold -c -w1)
#echo ${content[i]}
done
echo ${content[@]}
max
变量保存所有单词中的最大长度。对于您的文本,它将是:length('Example')
,即 7(所有单词的最大长度)
使用 awk
脚本文件:
$ awk -f script.awk <<< "This is an Example"
TiaE
hsnx
i a
s m
p
l
e
这是脚本:
{
max=0
for(i=1;i<=NF;i++)
max=length($i)>max?length($i):max;
for(j=1;j<=max;j++)
{
for(i=1;i<=NF;i++)
{
temp=substr($i, j, 1);
printf temp==""?" ":temp
}
printf "\n"
}
}
#!/bin/bash
function abc(){
maxIteration=0;
for i in $(seq 1 $#); do
j=$(echo ${!i})
if [ $maxIteration -lt ${#j} ]
then
maxIteration=${#j};
fi
done
COUNTER=0;
while [ $COUNTER -lt $maxIteration ]; do
for i in $(seq 1 $#); do
j=$(echo ${!i})
if [ ${#j} -gt $COUNTER ]
then
echo ${j:$COUNTER:1} | tr '\n' ' ';
else
echo " " | tr '\n' ' ';
fi
done
echo -e "\n"
let COUNTER=COUNTER+1
done
}
abc $@| grep .
我以前创建过一些类似的脚本。简短但完整的 POC:
#!/bin/bash
count=0
max=0
#first determine the longest string so we can later pad shorter strings with spaces
for i in $(echo "" | xargs -d: -i echo {})
do
size=$(echo $i | wc -c)
if [[ $size > $max ]]
then
max=$size
fi
done
files=""
#then echo the strings vertically inside the tmp files
for i in $(echo "" | xargs -d: -i echo {})
do
res=$(echo $i | sed 's/./ &/g' | xargs |tr ' ' '\n' > /tmp/$count.out)
#and add spaces at the end
add_space=$((max-$(echo $i | wc -c)))
for space in $(seq 0 $add_space)
do
echo " " >> /tmp/$count.out
done
files=$files" $count.out"
count=$((count+1))
done
#and finally print them side by side
pr -t -J -m -w 70 -S" " $files
我在 /tmp 下创建 tmp 文件,垂直回显字符串,然后使用 pr
打印出来。
% ./s.sh "This is an Example"
T i a E
h s n x
i a
s m
p
l
e
因为 Perl 很有趣:
perl -a script.pl <<< 'This is an Example'
T i a E
h s n x
i a
s m
p
l
e
和脚本:
@F = map { [/./g] } @F;
while (grep @{$_}, @F) {
printf "%s ", shift @{$_} || ' ' for @F;
print "\n"
}
备选脚本:
perl -pe '$r.=$/while/\S/&&s/(\S)(\S*)|\B/$r.=(||$").$";/ge}{$_=$r' \
<<< 'This is an Example'
T i a E
h s n x
i a
s m
p
l
e