Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a circular shift of strings in bash?

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
like image 350
Kyle Van Koevering Avatar asked Apr 05 '10 12:04

Kyle Van Koevering


People also ask

What does [- Z $1 mean in Bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What's do `` $() ${} commands do in Bash?

Save this answer. Show activity on this post. $() means: "first evaluate this, and then evaluate the rest of the line". On the other hand ${} expands a variable.

How do you slice a string in Bash?

Using the cut Command Specifying the character index isn't the only way to extract a substring. You can also use the -d and -f flags to extract a string by specifying characters to split on. The -d flag lets you specify the delimiter to split on while -f lets you choose which substring of the split to choose.

How do I shift in Bash?

The shift command is one of the Bourne shell built-ins that comes with Bash. This command takes one argument, a number. The positional parameters are shifted to the left by this number, N. The positional parameters from N+1 to $# are renamed to variable names from $1 to $# - N+1.


2 Answers

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)

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

$* 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.

function tail() {
    shift
    echo $*
}

Fourth, you can capture the output of commands and functions using `...` or $(...)

rest=`tail $*`

Fifth, you can send the output of one command to the input of another using the pipe character (|):

seq 5 | sort
like image 92
outis Avatar answered Sep 21 '22 00:09

outis


To get you started, check out the read builtin:

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

You also need a way to feed your input file into that loop.

like image 31
Arkku Avatar answered Sep 21 '22 00:09

Arkku