Manipulate string in file using shell - string

I'm trying to modify a string in a file using shell script.
What in the file version.yaml:
Version: 1.0.0-ghjjh
What I expect:
Add a suffix -{given argument}to the version string given an argument if there is no suffix added yet. Otherwise modify the suffix using the given argument.
For a given argument 0:
For the above file I provided, there is no suffix, the expected result is Version: 1.0.0-ghjjh-0
After this string in the file already has the suffix. Next time for a given argument 6, the expected result is Version: 1.0.0-ghjjh-6
What's the simplest way to do this in shell script?

Store the version in a separate file and regenerate the YAML from a template.

Related

removing the trailing extension from a string in Python3

I have the python script below to iterate over all files ending with 'mkv', and print the same string without the 'mkv' at the end.
But, instead it prints the original filename including the 'mkv', why??
files=os.system('find /media/radamand/230_GB -name *mkv')
for file in str(files):
converted_filename=file[0:-3]
print(converted_filename)
Your os.system call executes your find command, sends its output to your interpreter standard output stream (which is why you're seeing your matching files including the "mkv" at the end, as this output is not the result of your print function in your later code), and then simply returns the exit code.
So your files variable actually gets an assignment of the integer 0.
Your for loop then casts files from an int into a string ('0') and thus your for loop now actually means: "loop through each character of the string files" (there is only one however), which, in this case, due to your slicing of [:-3] on a string of only one character, evaluates as an empty string which gets passed to your print function.
So, os.system isn't designed for what you are trying to achieve.
If you potentially have other folders in the parent folder you are searching, that may also have the filenames you are looking for, then I would recommend using the glob module.
import glob
files = glob.glob("/media/radamand/230_GB/*mkv") # Returns a list of strings for matched files
for file in files:
print(file[:-3])
You can add and set the keyword arguments recursive and/or include_hidden to True if required.
If, however, you are only looking for the files in the current folder, you can use fnmatch:
import fnmatch
import os
for file in os.listdir("/media/radamand/230_GB"):
if fnmatch.fnmatch(file, "*mkv"):
print(file[:-3])

adf function to read only certain portion of filename based on pattern

I have file names as SMP_ACC_STG_20210987654.txt and another filename SMP_ACC_STG_BS_20210987654.txt. I can use #substring(item().name,0,11) and i get SMP_ACC_STG for first file which is correct but for second file I need to get filename as SMP_ACC_STG_BS and it returns same file name as first because i have harcoded the length in substring. I tried using indexof but it didnt give me the expected result.
I need to extract the text before _20210987654.txt and use that as filename.
I have used the below, and got my file names:
#substring(item().name,0,lastindexof(item().name,'_'))
Which gave me:
SMP_ACC_STG
SMP_ACC_STG_BS

How to match line & error in Sublime Text 3?

How can I match this error in the build with regex to locate line and file with result_line_regex & result_file_regex?
project4.dpr(9) Hint: H2164 Variable 'I' is declared but never used in 'Project3'
I have tried this but it won't work.
"result_file_regex": "^.*\\(.*)/.?(.*)$",
"result_line_regex": "^([^\\]*)\.(\w+)$",
As already mentioned in the comments, file_regex is the setting that gets passed to result_line_regex (have a look at the run() method signature of class ExecCommand in Packages/Default/exec.py).
A good regex in your case would be ^([\w-]+\.\w+)\((\d+)\). The first group captures something like my-file.ext and the second one the digit(s) in parentheses.
In order to set that expression in a string in the json file you need to escape each backslash with another backslash (\ is the escape character in strings), so it becomes:
"file_regex": "^([\\w-]+\\.\\w+)\\((\\d+)\\)"
Notice that the matched file has to be in the path of the file that is active when triggering the build system. If you want it to be relative to a certain path no matter where you trigger the build, you can also pass a working directory like:
"working_dir": "/path/to/my/source"
This will be set as result_base_dir in the output view.

How to make a dependency on a command line parameter

I have a command line variable (text string) which I inject into a file (the target file) using a builder. The question is, how to I trigger the building of the target file when the value of the the command line string changes?
One option might be to write the string to a text file, but I'd need to write that text file every time I build. Is there a cleaner way?
Not sure whether this really qualifies as "cleaner" way, but you can create a Value node:
mynode = env.Value(my_var_string)
and then use the Depends() method to let your target depend on this Value node:
env.Depends(final_target, mynode)
Please check the MAN page ( http://www.scons.org/doc/production/HTML/scons-man.html ) for a complete description of the Value method.

Returning Filename with wildcards in VFP 9

I am trying to locate the full name of a file using a wildcard. The code I have is:
MLCNo=crjbis.ffromlot
subfolder=LEFT(mlcno,3)
filename=SYS(2000,'S:\MLC\MLC#\'+subfolder+'xx\'+mlcno+'21391*.pdf')
pathname="S:\MLC\MLC#\"+subfolder+"xx\"+filename
Pathname is passed to a print function to print the file. All of this works great if I don't use a variable in the SYS function (even with a wildcard). I should add that there will only ever be one file returned by the wildcard. Is there another way to do this?
Thanks!!!
Tammy

Resources