Substringing with bash [closed] - bash

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am trying to substring many strings with bash.However, despite the prefix is sorrectly deleted, the suffix is not.
One of the strings:
lcl|MK087647.1_cds_QHD46953.1_7_[gene=rpl2]_[protein=ribosomal_protein_L2]_[exception=RNA_editing]_[protein_id=QHD46953.1]_[location=complement(71768..73444)]_[gbkey=CDS]
The desired output:
MK087647.1_cds_QHD46953.1_7_[gene=rpl2]_[protein=ribosomal_protein_L2]
The code
for row in $colonna2; do tmp=${row#*lcl|}
colonna2_newname=${tmp%exception=*} echo $colonna2_newname; done
The output
MK087647.1_cds_QHD46953.1_7_[gene=rpl2]_[protein=ribosomal_protein_L2]_[exception=RNA_editing]_[protein_id=QHD46953.1]_[location=complement(71768..73444)]_[gbkey=CDS]
Any guess why the suffix is not deleted? Has my syntax some error?
Thanks in advance

You have the variable substitution mostly right; it seems the main problem with the code is that there is no line break or semicolon after you define the colonna2_newname variable.
You will also want to change the colonna2_newname variable's definition from ${tmp%exception=*} to ${tmp%_[exception=*}.
for row in $colonna2
do
tmp="${row#*lcl|}"
colonna2_newname="${tmp%exception=*}"
echo "$colonna2_newname"
done
# output:
# MK087647.1_cds_QHD46953.1_7_[gene=rpl2]_[protein=ribosomal_protein_L2]
Now about the for loop: If any of the lines in your $colonna2 variable have whitespace in them, for will split the line into separate strings after each space. for loops are better suited for use with arrays and globbed filenames/pathnames. while read loops are better to use with lines of text:
while IFS=$'\n' read -r row
do
tmp="${row#*lcl|}"
colonna2_newname="${tmp%exception=*}"
echo "$colonna2_newname"
done <<< $colonna2

Related

Ruby Regex for string "and/or" as exact match [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I am trying to figure out the Ruby Regex for the exact string "and/or". For example, let's say I have a name variable that is "Elvin and/or Jafarli"
name = "Elvin and/or Jafarli"
and I want to split the name based on the string "and/or". How is that done in Ruby?
This is the final result I am looking for:
name.split(some_regex) results in ["Elvin", "Jafarli"]
** UPDATE **
This is the current regex that exists in the system
names.split(/ (?i)(?:and|or) /)
What I want to do is to update the regex to also split on exactly string match like "Elvin and/or Jafarli".
Add another alternative with |, and escape the delimiter:
names.split(/ (?i)(?:and|or|and\/or) /)
or use the alternative regex literal form:
names.split(%r{ (?i)(?:and|or|and/or) })
I feel like there must be a catch. This seems too easy.
irb(main):001:0> name = "Elvin and/or Jafarli"
=> "Elvin and/or Jafarli"
irb(main):002:0> name.split /\s+and\/or\s+/
=> ["Elvin", "Jafarli"]
Remember to escape the / in the regular expression and account for the surrounding whitespace. \s+ specifies one or more whitespace characters.

Windows Batch - delete last characters from variable [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
I need to delete the last 3 characters from a variable in Windows batch
set string=abcdefghij
I need the following output:
abcdefg
I can delete the first 3 characters from the variable using this:
set newstring=%string:~3,100%
echo %newstring%
Output:
defghij
But how can I delete the last 3 characters from the string - and store the result into a variable?
I can show the string without the last 3 characters by using sed (from GnuWin32) but I also need to store the result into another variable. I need a better solution than storing the result in a file and then reading the file into a variable (set /p newstring=<file.txt)
Later note: This question is very different, it needs removing last characters from a multi-line text, while I need removing last characters from a variable.
Just use negative index
set longtext=1234567890
:: Remove last 3 characters
echo %longtext:~0,-3%
:: show last three characters
echo %longtext:~-3%
Output:
1234567
890

How to match strings, ignoring certain characters [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have to match pairs of strings, ignoring spaces " " and hyphens "-". I want to regard the following pairs as identical.
"2,3 chloro benzene" and "2,3 chlorobenzene"
"4'3',2-dinitrotoluene" and "4'3',2-di nitro toluene"
Due to the spaces, I cannot match them. How can I do that? I am not sure how to do it in Ruby.
Use String#delete to delete unwanted chars and normalize the two strings before comparing them, as shown below:
s1 = "2,3 chloro-benzene"
s2 = "2,3 chlorobenzene"
s1.delete(" -") == s2.delete(" -")
#=> true

Searching text using a keyword, WITHOUT grep, sed, awk etc [closed]

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

Using read command [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I am extremely confused about using the read command. Can someone please explain this to me? For example if I have:
An executable file called script, containing
read first second
echo $first
echo $second
and you call it with:
echo This is a line of input | ./script
What happens and why? I can't get it to work and something is supposed to displayed
Running help read gives this info:
The line is split into fields as with word splitting, and the first
word is assigned to the first NAME, the second word to the second
NAME, and so on, with any leftover words assigned to the last NAME.
So what's happening here is that the first word is assigned to the variable $first, and the rest of the input line assigned to the last variable $second.
If you want to keep the first word in $first and the second word in $second, try adding a $third variable.

Resources