I'm learning bash. when I tried to enter
echo {a,b,c} ,{\ 1," 2",' 3'}
It returns
a b c , 1 , 2 , 3
While I tried to enter
echo {a,b,c}\ ,{\ 1," 2",' 3'}
It returns
a , 1 a , 2 a , 3 b , 1 b , 2 b , 3 c , 1 c , 2 c , 3
I've noticed the only difference is the second command contains \. Can someone explain the mechanism of \ or how two commands work differently? Thanks!
A space act as IFS (Internal Field Seperator). The first example
echo {a,b,c} ,{\ 1," 2",' 3'}
There are two distinct groups {a,b,c} and ,{\ 1," 2",' 3'}. That first one is printed out, then the second one is expanded and printed (comma and then each item in the sequence).
Your second example, the backslash escapes the IFS which tells your shell that it's a literal space character. So
echo {a,b,c}\ ,{\ 1," 2",' 3'}
Expands out each item from the first combining with 1 space and 1 comma and then each item from the second sequence producing:
a , 1 a , 2 a , 3 b , 1 b , 2 b , 3 c , 1 c , 2 c , 3
a , 1 and a , 2 and a , 3 and b , 1 and so on as each combination (which are then separated by a space, again because it is IFS by default)
Related
I got two types of activity selection problem, I want to prove that they are not given an optimal solution every time by just providing a counter example.
part1,
Choose an activity A belongs to S which has the smallest duration time.
Form a sub-problem S-prime from S by removing A and removing any activities that are incompatible with A.
Recursively choose a set of activities X-prime from S-prime.
my counter example:
tasks s/time f/time duration
a , 6 , 7 , 1
b , 0 , 3 , 3
c , 3 , 7 , 4
example output: a
optimal output: b,c
part2,
Choose an activity A belongs to S which overlaps as few other activities
as possible.
Form a sub-problem S-prime from S by removing A and removing any activities that are incompatible with A.
Recursively choose a set of activities X-prime from S-prime.
my counter example:
tasks s/time f/time overlaps
e , 2 , 8 , 0
b , 1 , 1.8 , 1
c , 5 , 7 , 1
d , 0 , 2 , 0
g , 7 , 8 , 1
a , 0 , 1.5 , 2
example output: e
optimal output: a choice of three tasks
I am not sure that my examples are correct, if they are wrong, please provide a correct example so I can know where I was mistaken.
I'm working through this algorithm and the pattern and text are not matching up.
The text is: AADBCCAAA
The pattern is: CCAAA
I've created both the bad-symbol table and good-suffix table.
Bad Symbol:
! A C (! represents letters not in the pattern)
5 1 3
Good-suffix:
k d2
1 2
2 6
3 6
4 6
5 6
As for my searching:
AADBCCAAA
CCAAA Since there is no match, shift 3 because C causes the mismatch
This lines up the right-most A in the pattern with the 2nd to last A in the text. This means
d1 = 3-2 = 1 and d2 = 6. The max of the two is 6 so shift 6.
With Boyer-Moore, does this mean since you can't shift 6, it will just compare the pattern with the end of the text and find the match or am I doing something wrong?
Why do the following expressions resolve the way they do? Brackets should have higher precedence than newlines, shouldn't they?
3 - ( 1 + 1 )
# => 1
3 - ( 1
+ 1 )
# => 2
Omitting the plus also lets the expression evaluate to 2:
3 - ( 1
1 )
# => 2
If I declare as a continuous newline (escaped) or move the plus to the first line, desired behavior is achieved:
3 - ( 1 \
+ 1 )
# => 1
3 - ( 1 +
1 )
# => 1
It is because Ruby recognizes a new line as an end of an expression unless the expression is incomplete. For example,
(1
+ 1)
is the same as
(1;
+1)
which is the same as +1 since the last expression within the parentheses is returned. And that is further the same as 1.
When you have a + at the end of a line, the expression is incomplete, and hence continues to the next line. That makes:
3 - ( 1 +
1 )
to be interpreted as 3 - (1 + 1).
If you have code in the brackets then every line will be threat as separated code line of code if you won't end it with \ or start new one with math operator.
So in your example:
def plus_minus_if_you_understand_this_the_problem_is_solved_i_guess
3 - (1
1 )
end
Means I have number 3 and want to subtract expression in brackets. In the brackets I have line #1 number 1 and in #2 line number 1 again and as it's last line of expression it's retuned by Ruby (like in def last item before end is returned. So:
( 3 # doing nothing
1 ) # returns 1
Also take a look bellow. Again, this part of code returns 2 as it is the last item in brackets:
( puts "hello!"
2 ) => 2
In csh or any other mac friendly script language, I would like to run some form of a loop that reads through 2 lists. Need a bit of help with the syntax.
For example:
variable1 = (1, 2, 3, 4, ...)
variable2 = (one, two, three, four, ...)
Foreach number ($variable1)
echo "{$variable1} equals {$variable2}"
end
When I try, the second variable2 is not recognized within the loop.
#!/bin/bash
variable1=(1 2 3 4)
variable2=(one two three four)
for ((i=0;i<${#variable1[#]};i++)); do
echo "${variable1[$i]} equals ${variable2[$i]}"
done
Output:
1 equals one
2 equals two
3 equals three
4 equals four
Please try this:
#! /bin/csh
set variable1 = (1 2 3 4)
set variable2 = (one two three four)
set i = 1
foreach x ( $variable1 )
echo "$x equals $variable2[$i]"
# i = $i + 1
end
The result is:
1 equals one
2 equals two
3 equals three
4 equals four
I have a text file that contains the following (a b c d etc... contains some random values):
1 a
1 b
2 c
2 d
2 e
2 f
6 g
6 h
6 i
12 j
12 k
Is there a way to separate lines with some characters depending on the content of the first string, knowing that those numbers will always be increasing, but may vary as well. The separation would be when first string is incrementing, going from 1 to 2, then 2 to 6 etc...
The output would be like this (here I would like to use ---------- as a separation):
1 a
1 b
----------
2 c
2 d
2 e
2 f
----------
6 g
6 h
6 i
----------
12 j
12 k
awk 'NR>1 && old != $1 { print "----------" } { print; old = $1 }'
If it isn't the first line and the value in old isn't the same as in $1, print the separator. Then unconditionally print the current line, and record the value of $1 in old so that we remember for next time. Repeat until done.