Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have many variables that will be called in a loop. I wanted, for example - To make the variable, when exceeding 30 characters, it would be cut to 30 characters and add 3 dots at the end.
Example:
set text = Hello world
Becomes:
"Hello ..."
That would help me a lot, however - If anyone can do something more advanced, for example when cutting some parts of characters, add the dots to the middle of the text.
Example:
set text = Hello wooooooooooooooooooooooooooooooooorld "
Becomes:
"Hello wo...orld"
#echo off
call :convert Hello
call :convert Hello beautiful World
call :convert Hello wooooooooooooooooooooooooooooooooorld
goto :eof
:convert
set "x=%*"
REM if the string is shorter than 10 chars, just print it and return:
if "%x:~10%" == "" echo %1 & goto :eof
REM else print first 7 chars, thee dots and the last three chars:
echo %x:~0,7%...%x:~-3%
Adapt the following numbers to your needs (you mentioned 30 chars, but none of your examples do match that number):
10 for "first ten characters" (due to zero-based counting it checks, if there is anything in the eleventh position)
7 for the number of characters before the three dots
3 for the number of last characters
The answer to your issue can be solved very easily using %variable:~num_chars_to_skip%
Using the SET command we can edit your variable and have it remove everything over 30 characters in length. From there we can take the new variable we made and manipulate it from there.
This code bellow will remove all characters exceeding 30 and display them in the text... format.
#ECHO OFF
::Edit string one with 30 char limit. String has 40 Chars.
SET String=0123456789012345678901234567890123456789
SET Result=%String:~0,30%
ECHO %Result%...
::Edit string one with 30 char limit. String has 90 Chars.
SET String=012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
SET Result=%String:~0,30%
ECHO %Result%...
::Edit string one with 30 char limit. String has 6 Chars.
SET String=0123456
SET Result=%String:~0,30%
ECHO %Result%...
PAUSE
GOTO :EOF
Learn more about syntax-substring here: https://ss64.com/nt/syntax-substring.html
Related
This question already has answers here:
bash: how does float arithmetic work?
(2 answers)
Closed 5 months ago.
I am working on bash script: need to evaluate an input and then output the result as a float *.xxx:
I do:
read var1
var2=$((var1))
#echo $var2
echo $((var1))
input: 5+50*3/20 + (19*2)/7
my output is 17 but it should be 17.929
How to evaluate it as a float?
Bash only supports integer arithmetic.
In your case, you can use bc(1).
read var1
var2="$(bc <<<"scale=2;$var1")"
Use the scale variable in bc(1) to set the number of significant digits (default with is 0).
bc(1) is subject to truncation errors (truncation happens at every step).
Another option is to use calc(1) (if it is available on your system):
var2=$(calc -d "_=config(\"display\", 3);$var1")
This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Closed 3 years ago.
I need to get user input for a number and then write a name row by row in linux terminal that much amount of times that user inputed. Example if I lets say chose a number 2 the program will write Name 2 times row by row. I wrote some code but I cant figure where is the mistake. I think its the loop where the mistake is.
echo "Please enter a number "
read $number
for value in {$number}
do
echo "Name"
done
To read input and save it into a variable named number, do:
read number
To get the value of number, do $number or ${number}. Remove the { } in the {$number} or shift $ with {.
Just do:
echo "Please enter a number "
read number
if ! test "$number" -gt 0 2> /dev/null; then
echo "You must enter an integer greater than 0" >&2
exit 1
fi
yes Name | sed ${number}q
But don't prompt for the number. Take it as a command line argument, and just do
yes Name | sed "${1}q"
Let sed generate the error message if the parameter is invalid.
The trouble with your loop is that for value in $number takes the string $number and breaks it on whitespace (depends on IFS, actually, but let's not get bogged down by details) and iterates over each value. That is, if $number is the string 1 3 dog 5, then the loop will iterate 4 times with $value taking the values 1, 3, dog, and 5. If $number is 7, then the loop iterates exactly once. You could do for((i=0; i < $number; i++)); do ..., but that does not generate any useful error message if $number is not an integer.
In substitution or deletion of sub string from original string, usually uses this form in windows CMD.
set result=%original:<strings_to_be_removed>=<strngs_to_be_newly_substituted>%
So it works for many situation as follows ..
set "original=Questions that may already have your answer"
set "you=%original:that=you%"
set you
you=Questions you may already have your answer
set challenge=%original:Questions=Challenges%
set challenge
challenge=Challenges that may already have your answer
set answer=%original:*your=%
set answer
answer= answer
But I don't know how to substitute or remove sub-string which starts from specific character(or word) to the end of the original string.
For example, suppose I would like to remove sub-string which starts from "that" to the end of the original string. Then I use command as follows and expect result string to be "Questions "
set result=%original:that*=%
But, result string has no difference from original string. No effect occures. Substitution intention fails..
set result
result=Questions that may already have your answer
I used escape character '^', '\' for this case, but no effect..
How to fix this to substitute or remove substring like this type?
How can you substitute or remove substring which starts from specific character(or word) to the end of the original string? Thank you:-)
you can trick the command line parser to do that:
set "original=Questions that may already have your answer"
set result=%original: may =&REM %
set result
sadly, set "result=%original:may=&REM %" doesn't work, so the string should be free from poison characters.
How it works:
replace the word with &REM, which makes your string:
Questions that & REM already have your answer
and the command:
set result=Questions that & REM already have your answer
& is used as a delimiter for commands (try echo hello&echo world, which executes both echo commands). So what's really executed, is two commands:
set result=Questions that
and
REM already have your answer
It also doesn't work with delayed expansion. You can use a subfunction for it instead:
setlocal enabledelayedexpansion
if 1==1 (
set "original=Questions that may already have your answer"
call :substring "!original!"
set result
)
goto :eof
:substring
set org=%~1
set result=%org: may =&REM %
goto :eof
I have a large string that looks like this
aa bb c d f eeeee ffff
I am not sure of the number of spaces that would come along with the string. The next time, string might have five spaces between aa and bb ansd so on...
aa bb c f eee ff
Is there a way I can split on two or more spaces (variable number of spaces) using delims in batch script?I always want the second token no matter how any spaces are there between first and second tokens.
PS : Edit
I want the second token (by token I mean the token obtained after splitting by exactly two or more spaces). My token itself can contain a single space. Example: a b c d.
In this want to extract b.
Another example: a b b c d .
In this I want to extract b b.
(Collected from various comments: Tokens are delimited by two or more spaces; You need the second token; Every token may or may not have single spaces)
I commented every step and added an echo to show progress.
#echo off
setlocal
set "string=ads ads d b c dsad ds ads"
echo 1: "%string%"
REM remove first token (delimited by ecactly two spaces):
set "string=%string:* =%"
echo 2: "%string%"
REM remove leading spaces:
for /f "tokens=*" %%a in ("%string%") do set "string=%%a"
echo 3: "%string%"
REM remove third and next tokens (delimited by two or more spaces)
set string=%string: =&REM %
echo 4: "%string%"
Only change to my answer to your previous question is the for to remove leading spaces
Notice for future readers:
This answer was posted before OP added the significant text I always want the second token no matter how any spaces are there between first and second tokens.
Please read the original question before voting/commenting
No.
delims means that tokens are separated by any sequence of any of the defined delimiter characters.
Hence your example string would be processed as 7 separate tokens.
If you were to tell us what you are attempting to do,it may help. The approach taken often depends on the desired results.
Probably this is associated with https://stackoverflow.com/a/48808982/2128947 to which you have yet to respond.
This is how I implemented this incase anyone is interested.
#echo off
echo.
set "string=Admin State State Type Interface Name"
echo Parse "%string%"
REM: Parse string delimited by more than one space
for %%R in ("%string: =" "%") do (
REM: Filter empty
if not "%%~R" == "" (
REM: Trim leading space
for /f "tokens=*" %%a in ("%%~R") do set "part=%%a"
call echo - "%%part%%"
)
)
echo.
goto :eof
Output:
Parse "Admin State State Type Interface Name"
- "Admin State"
- "State"
- "Type"
- "Interface Name"
This is the way I would do it:
#echo off
setlocal
set "string=ads ads d b c dsad ds ads"
set "tok1=%string: =" & if not defined tok2 set "tok2=%"
for /F "tokens=*" %%a in ("%tok2%") do set "tok2=%%a"
echo "%tok2%"
If you want to understand the method used, remove the #echo off line and carefully review the executed code...
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can i search from set of strings
i would like a perl script to search for either "aaa", "bbb", "ccc", "ddd"
Searching from txt file
Example 1
some random text
blah
blah
blah
//Car/trunk/sandbox/users/Tom/xyz/bbb/toyota
Desired Output:
//Car/trunk/sandbox/users/Tom/xyz/...
Example 2
some random text
blah
blah
blah
//Car/trunk/sandbox/users/Alex/abc/defg/ddd/honda/accord
Desired Output:
//Car/trunk/sandbox/users/Alex/abc/defg/...
Basically searching from the given set of strings from "aaa", "bbb", "ccc", "ddd" it finds "bbb" in example 1 and takes everything before and adds back slash with "..."
Please note that it is reading from one text file, example 1 is a example of text file and same goes for example 2.
So far this is what i have.
my $infile = 'new5.txt';
my #strings = qw/aaa bbb ccc ddd/;
local #ARGV = ($infile);
while( <> ){
print;
}
The code you have so far is what I would call a hackish way to cat a file.
First of all, we will get the obligatory pragmas out of the way:
use strict;
use warnings;
use autodie;
The last one makes sure the script dies if a file was not found or could not be opened.
You initialized $infile and #strings correctly, good.
The proper or more accepted way to open and loop over the contents of a file is this:
open(my $FILE, "<", $infile);
while(my $line = <$FILE>) {
chomp $line;
...
}
close($FILE);
Within this loop you want to try to match each element of the #strings array. That can be accomplished by looping over each element in the array:
foreach my $elem (#strings) {
...
}
In this loop you want to do the match. If it does, print out what you need (and go the next line in the file). Look up the perlre man page to see what the stuff after =~ is doing:
if ($line =~ m#/$elem/#) {
print SOMETHING;
next;
}
What would that SOMETHING be? According to the perlvar man page, `$`` (among other mnemonics) is the variable that contains the string before the matched string:
print $`."/...\n";
Put it all together and you have your script.