And I want to increment a Variable but it doesnt work.
How can extract digits at the end of a string and increment variable value by 1 in Unix shell scripting or perl?
Thanks again.
e.g. a123_B234-012.txt
I want the output: ------ Post updated at 03:13 AM ------
e.g.
Output
Last edited by mingch; 11-21-2018 at 11:12 PM..
Reason: Code tags
The first is: any command involving sed .... | sed ... is (99.99999% of the times) wrong: sed is perfectly capable of handling several commands in a script with loops, branches and everything a programming language offers. Use that instead of pipes.
Second: you don't need sed at all for this. You can do this with shell-internal variable manipulation. Notice that calling a program is "expensive" in terms of time and resources: the OS needs to load the program, start a sub-process, and so on. Variable manipulation happens within the shell and foregoes all this. The worst thing one sees all the time is:
If your file is long and you replace the sed with variable manipulation it might be ten more lines to write but it will run in 1% of the time.
Parameter expansion (this is the "official" name for the mechanism) works like this:
This you already know: ${variable} produces the content unaltered.
This cuts off <pattern> from the end of the variables contents. This is basically the same as sed "s/.txt$//". Try it out:
You may wonder what the difference between ${variable%pattern} and ${variable%%pattern} is: it is "longest match" ("%%") and "shortest match" ("%"). Suppose we want to cut off not only ".txt" but any extension. We could do so by:
But a filename could contain several dots like this, then there would be a difference between longest and shortest match:
Notice that all these manipuations do NOT change the value of the variable! They just change what is displayed. If you want the effect to be lasting you would have to do:
There is another expansion which cuts off not from the end but from the beginning of a string: ${variable#pattern} and ${variable##pattern}. It works the same way as the "%" otherwise. For instance, you often need to split a pathname ("/some/path/to/a/file.name") into the path and the filename:
There are even more of these expansions which can selectively change patterns within a string, conditionally assign values to variables and more. I hope to have piqued your interest. You are probably eager now to try this to solve your problem yourself.
I hope this helps.
bakunin
These 4 Users Gave Thanks to bakunin For This Post:
For your latest request - increment the last number in the file names - , on top of the "parameter expansion" proposed by bakunin, "arithmetic expansion" could help. Its syntax is (( variable / constant / operator ... )), and it can be mixed with other expansions. For the first two of your sample data, this might work:
As the structure of the other names is different, above simple approach will fail, and it needs to be adapted, but mayhap you have a good starting point from it.
Hello,
I have a log file with logs such as
01/05/2017 10:23:41 : file.log.38: database error, MODE=SINGLE, LEVEL=critical, STATE: 01170255 (mode main
how can i use perl to extract the 8-digit number below from the string
01170255
Thanks (7 Replies)
My input is as below :
/splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt
/splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt
/splunk/scrubbed/loyal/IFIND.HELLO.WROC.txt
/splunk/scrubbed/triumph/ifind.triumph.txt
From the above input I want to extract the file names only .
Basically I want to... (5 Replies)
Hi
I have a very large data file with several hundred columns and millions of lines.
The important data is in the last set of columns with variable numbers of tab delimited fields in front of it on each line.
Im currently trying sed to get the data out - I want anything beetween :RES and... (4 Replies)
Hi,
How to add trailer record at the end of the flat file in the unix ksh shell scripting
can you please let me know the procedure
Regards
Srikanth (3 Replies)
Hi there, im sure this is really simple but i have some strings like this
e1000g123001
e1000g0
nge11101
nge3and i want to create two variables ($DRIVER and $INSTANCE). the first one containing the alpha characters that make up the first part of the string, e.g. e1000g or nge and the... (9 Replies)
Hi
I am new to world on unix scripting so any assistance would be gratefully appreciated,
I am trying to write a script which reads through a file, reads in line by line, searches for a pattern, copies string after it and then to do a search and replace elsehwere in the line,
so the... (7 Replies)
Hi all,
i have such string stored in a variable
var1 = 00000120
i want the o/p
var1 = 120
is it possible to have such o/p in ksh/bash ...
thanx in advance for the help
sonu (3 Replies)
Hi Experts,
Here is what I am trying to do.
1) say I have a file with below strings
database1
database2
database3
data10gdb1
data10gdb2
databasewithoutdigit
2) I want to get the below output.
(- if there is any digit at the end of the string, I need to remove it)
(- Any... (3 Replies)
Hi,
I would like to learn shell scripting in UNIX. Can any one please give me the support and share the information/documents with me.
If any documents please post it to aswanikumar_nimmagadda@yahoo.co.in
Thanks in advance...!!! (3 Replies)