This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Matching brackets in a string
Is there a way around using [[ and ]] for Part in Mathematica?
OK, today looks like the day for asking typesetting questions!
This is something I always wondered if there is a solution for. Many times, I write Part notation just the normal way like this x[[3]] but what I prefer is the solid notation, which is
The reason I do not enter it as above the first time, is that it is faster to type x[[3]] because I do not have to type x ESC [[ ESC 3 ESC ]] ESC each time, and also many times I simply forget.
Then what I do, is later on, I go over the whole code looking for each normal [[ ]] and then change it to the solid [[ ]] . But this is boring.
I tried to use the editor to do a replace every [[ with the solid [[ and then replace all every ]] with the solid ]] but that did not work. It broke the code.
So my question is: Is there a way to automatically do this change? It would require lexical scanning and analysis of the code, as one can just change any [[ seen with a solid [[.
Thanks
Related
This question already has answers here:
Why do shell script comparisons often use x$VAR = xyes?
(7 answers)
Closed 9 months ago.
I was looking at the source code of NodeSource's installer script for Node.js.
Everywhere in the code, when they want to compare two strings, they prepend X to both sides before comparing:
if [[ "X${NODENAME}" == "XNode.js 10.x" ]]; then
I was pointed to some questions on the network:
What's the purpose of adding a prefix on both sides of a shell variable comparison to a string literal?
Why do shell script comparisons often use x$VAR = xyes?
Why append an extra character in `test`/`[` string comparison in POSIX sh?
They all explain why this is necessary when comparing with [. But in this case, the comparison is with [[.
Is this technique really necessary for [[?
Credits to #danielhoherd, in a comment
No, it's not necessary.
In fact, this technique is outdated for [ as well, as explained in the SC2268 shellcheck rule:
Avoid x-prefix in comparisons as it no longer serves a purpose.
[...]
Some older shells would get confused if the first argument started with a dash, or consisted of ! or (. As a workaround, people would prefix variables and values to be compared with x to ensure the left-hand side always started with an alphanumeric character.
POSIX ensures this is not necessary, and all modern shells now follow suit.
[...]
I am trying to get information from an XML document and then taking that information and compare it to the information I am getting from a text document. But whenever I compare the number I receive from the XML to the number from the text document, the script always tells me they are not equal, even when they should be.
I am using xmllint to get the information from the XML document and read to get the number from the text document. Then I'm trying to compare them and if they're the same do something. But this is the point that I'm stuck at.
input_3="/Users/unix/Desktop/text.txt"
VAR_4= xmllint --xpath "string(//number)" /Users/unix/Desktop/01/testxml.xml
while IFS= read -r line
do
if [[ "$VAR_4" == "$line" ]]
then echo "YEAH"
else echo "Why"
fi
done < "$input_3"
With this code, I always go into the else part of the statement even though the numbers should be the same. I have been working with echo to check the numbers to make sure they're the same and the only thing I could think of is that is has something to do with new lines or spaces. That either xmllint or read with IFS is putting a new line behind the number and that's the reason the script doesn't consider them the same. For example in my text document the number is 2, but I have to put a newline behind it, so read gets the number, and in the XML, the number is 2 too. I am hoping someone can maybe give me a clue about how I can change the format of the outputs I am getting or how to get the outputs on the same level.
I have a script wherein you have to input a string with a length greater then or equal to 1 and less then 26.
If that's not the case I want to return an error. But that's the part I have figured out
lengthAlphabetInput=${#1}
if [ $lengthAlphabetInput -lt 1 ] || [ $lengthAlphabetInput -gt 26 ]
then
echo "error: key needs to be between 1 and 26 characters"
exit 1
fi
Other than that I would like to check if the input the user gave is a permutation of (a part of) the alphabet.
For example if the user inputs "abc" I want to return an error "abc is
not a permutation of the alphabet"
if the user inputs "xxxgsdnoip" I again want to return the same error
because I don't want the user to use the same letter more than once.
But the input "xyz" or "jhcwslaedmviotrgzxkbynpuqf" would be correct
because these are permutations of the alphabet. (x instead of a, y
instead of b and z instead of c).
Can anyone help me transform this idea into code?
I realized that this is a question raised by a student, so I did not write down a detailed answer, since the experience of reading manual and figuring it out yourself will really help you learn how to use bash (actually the GNU/BSD core utilities), as said by #binaryzebra. What you should do is:
Learn to read manual in bash, with command man, such as man sort for the manual of sort utility. Hit Up/Down arrow key or PageUp/PageDown key to scroll; hit q to exit. Reading manual is your first step into Unix world. Sure you can skip this and find all the information from Google, but learning to read manual will do you more good in the long run.
Read the manual of sed and learn substitution with regular expression. The manual is a little too long for a newcomer, but luckily you do not need to read it all; just scan the manual and find the part about substitution; read the examples as well, if there is any. Practice with some test file. Now you know how to check whether input contains only letters (instead of whitespace, symbols, etc.), as well as how to split each character in its own line.
Read the manual of uniq. It has a much shorter manual; reading the whole manual won't take long.
Now learn the pipeline feature in bash. I cannot find a short and focused manual entry, so you may as well just read the online manual from GNU. With the help of pipeline, you can combine sed and uniq to detect duplicated characters.
By "permutation", it seems that you do not want the characters in their original order. If so, read the manual of the sort utility and think how it can help you.
You do not seem to care about whether all 26 letters are there. If this is the case, you probably do not need the wc (word count) utility, unless you require the subset of letters be continuous (such as "cdefg" instead of "cdhjk").
That's all the hints; good luck with your homework.
#!/usr/bin/perl
$_=shift;
print "not ok:repeated: $1\n" if/(.).*\1/;
my $i=0;
my #s= ( map { ord($_)-97 != $i++ ? ():($_)} split(''));
print "not ok:samePlace: #s\n" if #s;
usage:
$ perl ex.pl rty
$ perl ex.pl abc
not ok:samePlace: a b c
$ perl ex.pl ddss
not ok:repeated: d
This question already has answers here:
How to add a progress bar to a shell script?
(41 answers)
Closed 7 years ago.
I am thinking of writing a simple shell program that will show an output in the console about the percentage work completed.
something like thiss:-
2%==>
which will increase the arrow with respect to the work done.
I donot want to print the loader every time in a new line.
what should be my approach?'
*I often see this thing is used in wget and similar commands
TIA
To update the line the cursor is on, send a CR ("carriage return", \r) to send ("return") the cursor to the beginning of the existing line, from which you can print new contents. Contrast this to the newline (\n), which moves the cursor to a new line.
To see this in action, try running the following:
printf '%s\r' "Existing contents being written here"
sleep 1
printf '%s\r' "New contents being written here "
sleep 1
printf '%s\n' "Writing final contents and moving to a new line"
printf '%s\n' "This is written to a second line."
Note how the second line has some extra whitespace on the end; this padding is there to make sure that the end of the original line's contents are overwritten.
That said, if you just want a status bar already built for you, there are numerous solutions for that already:
pv (Pipe Viewer) will do all the math for you, including with large streams, binary streams, etc.
The dialog program contains a "gauge" widget, with similar functionality. See this example of its usage on Unix StackExchange.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am working on a programming assignment where I have to search for an entry in a text file, and print out text corresponding to the entry. As an example, let's say I have an entry as follows,
JOHN DOE
34 RIGHT WAY
HALIFAX
465-0394
, and the user enters HALIFAX as the keyword, I then would want to find the line that Halifax is located on, and then print out all associated text with this entry. The tricky part is doing this all without grep, sed, or awk, as the assignment is not accepted if these commands are used. I thought about using regular expressions, but these text manipulations can only be done on a single line, and I must do it for the entire file. As of now I am stumped and any help would be appreciated!
Alex
You should read in the whole file in your bash script line by line and then check if the line contains your search term
cat $FILENAME | while read LINE
do
if [[ $LINE =~ *HALIFAX* ]] then
echo "I found HALIFAX"
fi
done
From here on it should be easy enough for you to print out the rest.
I'm assuming this is bash scripting from the tag. My suggestion would be to read the text file line by line into an array, and then loop through the array, searching for the keyword in each string. This can be done using wild cards. Here's a link: String contains in Bash. Could you clarify "print out all associated text with this entry"?
You can do this by using while loop reading the file line by line.
#set the delimiter to newline
IFS_backup=$IFS
IFS=$'\n'
#variable to calculate line number
lineNum=0
while read a1
do
let "lineNum++" #increment variable for each line
#variable a1 store the value of each line
#do comparision with $a1 by user input string
#if string match then value of lineNum equal to the line number of file
# containing user input string
done<filename