如何在 bash 中循环移位字符串?
我有一个家庭作业,我需要从文件中获取输入并连续删除一行中的第一个单词并将其附加到行尾,直到完成所有组合。
我真的不知道从哪里开始,非常感谢任何类型的指导。
让我感到困惑的部分是,这应该是在不使用数组的情况下执行的。我不仅仅是在寻找某人来为我解决问题,我只是在寻找方向。
样本输入:
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
I have a homework assignment where I need to take input from a file and continuously remove the first word in a line and append it to the end of the line until all combinations have been done.
I really don't know where to begin and would be thankful for any sort of direction.
The part that has me confused is that this is suppose to be performed without the use of arrays. I'm not just fishing for someone to solve the problem for me, I'm just looking for some direction.
SAMPlE INPUT:
Pipes and Filters
Java Swing
Software Requirements Analysis
SAMPLE OUTPUT:
Analysis Software Requirements
Filters Pipes and
Java Swing
Pipes and Filters
Requirements Analysis Software
Software Requirements Analysis
Swing Java
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
一些应该有所帮助的花絮:当您使用字符串调用函数时,该字符串被分成多个参数(位置参数,名为
$n
,其中n是整数从 1) 开始,变量 < code>$IFS (默认为空格、制表符和换行符)$*
和$@
按顺序为您提供所有位置参数。二、特殊变量
$ #
保存函数的参数数量。第三,
shift< /code>
丢弃第一个位置参数并将所有其他位置参数上移一个。
第四,您可以使用 `...` 或
$(...)
第五,您可以使用 管道符 (
|
):A few tidbits that should help: when you call a function with a string, the string is split into multiple arguments (the positional parameters, named
$n
, where n are integers starting at 1) on the characters in the variable$IFS
(which defaults to spaces, tabs and newlines)$*
and$@
give you all the positional parameters, in order.Second, the special variable
$#
holds the number of arguments to a function.Third,
shift
discards the first positional parameter and moves up all the others by one.Fourth, you can capture the output of commands and functions using `...` or
$(...)
Fifth, you can send the output of one command to the input of another using the pipe character (
|
):首先,请查看内置的
read
:您还需要一种方法将输入文件输入到该循环中。
To get you started, check out the
read
builtin:You also need a way to feed your input file into that loop.
让我向您展示一个基于 @outis 答案 的快速工作示例:
或者如果您感兴趣,也可以使用其他方式按顺序遍历:
Let me allow to show you a quick working example based on @outis answer:
Or other way-round if you're interested in traversing by sequence:
这是简短的演示。遍历单词,得到最后一个单词并将其放在字符串前面。直到最终的字符串与原来的字符串相同。
您应该能够使用文件完成其余的工作。
this is short demo. Go through the words, get the last word and put in front of the string. until the final string is the same as the original.
you should be able to do the rest using a file.
Bash 允许您使用子字符串表示法来寻址位置参数。
然后,处理多行输入就像
while
循环一样简单:或者如果您喜欢冒险,甚至:
我没有看到任何关于示例输出应该如何进入的解释该顺序,因此该解决方案忽略了您问题的这方面。
请注意,这并不处理所有排列/组合,仅处理您在问题中指定的排列/组合。
Bash lets you address positional parameters using substring notation.
Then, handling multiple lines of input is as simple as a
while
loop:Or if you're feeling adventurous, even:
I don't see any explanation as to how your sample output is supposed to get in that order, so this solution ignores that aspect of your question.
Note that this doesn't handle all permutations/combinations, only the ones you specified in your question.
以下是如何循环左移字符串中的字符以及如何循环左移一行上的单词。
输出
Here is how you can circular left shift the chars in a string and how to circular left shift words on a line.
output
参数扩展可以让您删除变量的一部分。
Parameter expansion will let you carve off parts of a variable.