How to parse a word in shell script - shell

I want to parse the following word in shell script
VERSION=METER1.2.1
Here i want to split it as two words as
WORD1=METER
WORD2=1.2.1
Let me help how to parse it?

Far more efficient than using external tools such is sed is bash's built-in parameter expansion support. For instance, if you want the name variable to contain everything until the first number, and the numbers variable to contain everything after the last alpha character:
version=METER1.2.1
name=${version%%[0-9]*}
numbers=${version##*[[:alpha:]]}
To understand this, see the BashFAQ entry on string manipulation in general, or the BashFAQ entry on parameter expansion in particular.

Related

Replacing (escaping) characters in Groovy

For a gradle script, I am composing strings that will be used as command line for a subsequent gradle Test-task. One of the strings is the user's password, which eventually will be passed to the called (exec'ed) "java ..." call using the JVM's -D option, e.g. -Dpassword=foobar.
What complicates things here is, that this password can/should of course contain special characters, that may interfere with the use of the string as command line. In other words: I need to escape special characters (which is OS-specific). :-(
Now to my actual question:
I want to use the String.replaceAll method, i.e. replaceAll(list_of_special characters, EscapeCharacter + Ref_to_matched_character),
e.g. simplified something like replaceAll("[#$%^&]", "^$1")
'^' meaning the escape character and '$1' meaning the matched character here.
Is that possible, i.e. can one refer to the matched pattern in the second argument of replaceAll?
Is that possible, i.e. can one refer to the matched pattern in the second argument of replaceAll?
yes, it's possible
'a#b$c'.replaceAll('([#$%^&])', '^$1')
returns
a^#b^$c
Thanks for the responses and the reviews improving readability. Meanwhile I got my expression working. For those interested:
// handles gthe following: `~!##$%^&*()_+-={}|[]\:;"'<>?,./
escaped = original.replaceAll('[~!##\\$\\%\\^\\&\\*\\(\\)_\\+-={}\\|\\[\\]\\\\:;\"\\\'<>\\?,\\./]', '^$0') // for Windows - cmd.exe

what is the command-line processing order for string operators like ${string#patten} in bash shell?

I was learning the command-line processing of bash shell. I understand the process of substituting the value for a common parameter like $para or ${para}. My question is what would be the processing order when string operator is involved. For example, if para is a variable preceded by a tab then followed by "string". It is known that ${para#\t} is meant to delete the preceding tab in para. So how is this processed by the shell? Is there any command substitution involved?
Can anyone give any hint? Thank you.

Creating a string that calls a variable

I could use some suggestions, this probably isn't the most elegant way to do this but it's what came to mind. Let me add a couple snippets for clarity.
week1="01-\(04\|05\|06\|07\|08\|09\|10\)-2016"
week2="01-\(11\|12\|13\|14\|15\|16\|17\)-2016"
week3="01-\(18\|19\|20\|21\|22\|23\|24\)-2016"
week4="01-\(25\|26\|27\|28\|29\|30\|31\)-2016"
week5="02-\(01\|02\|03\|04\|05\|06\|07\)-2016"
I have a text file that has dates in each line, for example
293232343;01-02-2016;blah;more blah
234872348;02-01-2016;blah;extra blah
I am trying to create a loop so I can grab different data depending on the calendar week.
current_week=`date +%W`
for (( c=1; c<=$current_week; c++ ))
do
sort -k2 archive.txt|tr "\\t" ";"|grep $week$c|while read line; do
My problem is that $week$c doesn't actually translate to $week1, $week2, etc. Any easy solutions here? Thanks in advance.
From man bash:
If the first character of parameter is an exclamation point, a level
of
variable indirection is introduced. Bash uses the value of the vari-
able formed from the rest of parameter as the name of the variable;
this variable is then expanded and that value is used in the rest of
the substitution, rather than the value of parameter itself. This is
known as indirect expansion. The exceptions to this are the expansions
of ${!prefix*} and ${!name[#]} described below. The exclamation point
must immediately follow the left brace in order to introduce indirec-
tion.
Then, what you need is
var="week$c"
... grep "${!var}" ...

Bash string manipulation, extracting/removing parts

I'm modifying an old bash file and am having some trouble manipulating strings. The problem is that the strings can be anything random to the left of _<date>.<num>. For example, from ThisIsAString-Sub_tag_150827.1, I need to extract _150827.1. In bash, this seems very difficult to do. In any other language, I would split on _, and just grab the last element of the list. How do I do this in bash? I've tried a few different ways (including with awk), but cannot seem to get it right.
With bash's Parameter Expansion:
a="ThisIsAString-Sub_tag_150827.1"
echo "${a##*_}"
Output:
150827.1

Windows SED command - simple search and replace without regex

How should I use 'sed' command to find and replace the given word/words/sentence without considering any of them as the special character?
In other words hot to treat find and replace parameters as the plain text.
In following example I want to replace 'sagar' with '+sagar' then I have to give following command
sed "s/sagar/\\+sagar#g"
I know that \ should be escaped with another \ ,but I can't do this manipulation.
As there are so many special characters and theie combinations.
I am going to take find and replace parameters as input from user screen.
I want to execute the sed from c# code.
Simply, I do not want regular expression of sed to use. I want my command text to be treated as plain text?
Is this possible?
If so how can I do it?
While there may be sed versions that have an option like --noregex_matching, most of them don't have that option. Because you're getting the search and replace input by prompting a user, you're best bet is to scan the user input strings for reg-exp special characters and escape them as appropriate.
Also, will your users expect for example, their all caps search input to correctly match and replace a lower or mixed case string? In that case, recall that you could rewrite their target string as [Ss][Aa][Gg][Aa][Rr], and replace with +Sagar.
Note that there are far fewer regex characters used on the replacement side, with '&' meaning "complete string that was matched", and then the numbered replacment groups, like \1,\2,.... Given users that have no knowledge or expectation that they can use such characters, the likelyhood of them using is \1 in their required substitution is pretty low. More likely they may have a valid use for &, so you'll have to scan (at least) for that and replace with \&. In a basic sed, that's about it. (There may be others in the latest gnu seds, or some of the seds that have the genesis as PC tools).
For a replacement string, you shouldn't have to escape the + char at all. Probably yes for \. Again, you can scan your user's "naive" input, and add escape chars as need.
Finally if you're doing this for a "package" that will be distributed, and you'll be relying on the users' version of sed, beware that there are many versions of sed floating around, some that have their roots in Unix/Linux, and others, particularly of super-sed, that (I'm pretty sure) got started as PC-standalones and has a very different feature set.
IHTH.

Resources