6

我有一个家庭作业,我需要从文件中获取输入并不断删除一行中的第一个单词并将其附加到行尾,直到完成所有组合。

我真的不知道从哪里开始,并会感谢任何形式的方向。

让我感到困惑的部分是,这应该是在不使用数组的情况下执行的。我不只是在寻找某人为我解决问题,我只是在寻找一些方向。

样品输入:

Pipes and Filters
Java Swing
Software Requirements Analysis

样品输出:

Analysis Software Requirements
Filters Pipes and
Java Swing
Pipes and Filters
Requirements Analysis Software
Software Requirements Analysis
Swing Java
4

7 回答 7

6

一些应该有所帮助的花絮:当您使用字符串调用函数时,字符串在变量中的字符(默认为空格,制表符和换行符)$n$IFS

function first() {
    echo $1
}
first one two three
# outputs: "one"

$*并按$@顺序为您提供所有位置参数。

其次,特殊变量$#保存函数参数的数量。

第三,shift丢弃第一个位置参数并将所有其他位置参数上移一个。

function tail() {
    shift
    echo $*
}

第四,您可以使用`...`来捕获命令和函数的输出,或者$(...)

rest=`tail $*`

第五,您可以使用管道字符( |)将一个命令的输出发送到另一个命令的输入:

seq 5 | sort
于 2010-04-05T12:34:54.867 回答
3

为了让您开始,请查看read内置:

while read first_word rest_of_the_line; do
    ... your code here ...
done

您还需要一种将输入文件输入该循环的方法。

于 2010-04-05T12:27:35.180 回答
3

让我向您展示一个基于@outis answer的快速工作示例:

strings="string1 string2 string3"
next() { echo $1; }
rest() { shift; echo $*; }
for i in $strings; do
  echo $(next $strings)
  strings=$(rest $strings)
done

如果您有兴趣按顺序遍历,或者其他方式:

strings="string1 string2 string3"
next() { echo $1; }
rest() { shift; echo $*; }
totalWords=$(c() { echo $#; }; c $strings)
for i in `seq -w 1 $totalWords`; do
  echo $i: $(next $strings)
  strings=$(rest $strings)
done
于 2015-05-08T18:05:29.330 回答
0

这是一个简短的演示。遍历单词,得到最后一个单词并放在字符串的前面。直到最终的字符串与原始字符串相同。

    #!/bin/bash    
    s="Software Requirements Analysis"
    org="$s"
    while true
    do
       last=${s##* } #Analysis
       front=${s% *} #Software Requirements
       s="$last $front"
       echo $s
       case "$s" in
         "$org") break;;
       esac
    done

您应该能够使用文件完成其余的工作。

于 2010-04-05T13:03:39.550 回答
0

Bash 允许您使用子字符串表示法来处理位置参数。

$ foo() { for ((i=1; i<=$#; i++)) { printf '%s ' "${@:$i}" "${@:1:$((i-1))}"; echo; }; }
$ foo one two three
one two three
two three one
three one two

然后,处理多行输入就像while循环一样简单:

$ while read -a a; do foo "${a[@]}"; done < input.txt
Pipes and Filters
and Filters Pipes
Filters Pipes and
Java Swing
Swing Java
Software Requirements Analysis
Requirements Analysis Software
Analysis Software Requirements

或者,如果您喜欢冒险,甚至:

$ while read; do foo $REPLY; done < input.txt

我没有看到关于您的示例输出应该如何按该顺序获得的任何解释,因此该解决方案忽略了您问题的这一方面。

请注意,这并不处理所有排列/组合,只处理您在问题中指定的排列/组合。

于 2018-12-25T06:22:00.033 回答
0

以下是如何循环左移字符串中的字符以及如何循环左移一行中的单词。

#!/bin/bash


# To left circular shift a string by chars
#
buf='12345'
bufsize=5

echo buf="$buf"
echo bufsize=$bufsize

for i in $(seq 10) c d e f; do
    buf=${buf:1:$bufsize}${buf:0:1}
    echo $buf
done

echo '--------------------------------------'
# to left circular shift a string by words
buf='one two three four "big bug"'
function lcs()
{
    s=$1
    shift
    echo $@ $s
}
for i in $(seq 10) c d e f; do
    buf=$(lcs $buf)
    echo $buf
done

输出

$ ./test
buf=12345
bufsize=5
23451
34512
45123
51234
12345
23451
34512
45123
51234
12345
23451
34512
45123
51234
--------------------------------------
two three four "big bug" one
three four "big bug" one two
four "big bug" one two three
"big bug" one two three four
bug" one two three four "big
one two three four "big bug"
two three four "big bug" one
three four "big bug" one two
four "big bug" one two three
"big bug" one two three four
bug" one two three four "big
one two three four "big bug"
two three four "big bug" one
three four "big bug" one two
于 2020-12-11T19:33:03.100 回答
-1

参数扩展将让您分割变量的一部分。

于 2010-04-05T12:33:27.353 回答