tcl split string with variables - string

how can split the name in tcl?
NEW in ARCHIVE XVID/J/JURASSIC.WORLD
path is not always the same, it may also be so
/XVID-BOXSET/007.A.VIEW.TO.A.KILL
and set this as variable, one variable set the name and one the path.
I need the following variables:
Name example : JURASSIC.WORLD bzw 007.A.VIEW.TO.A.KILL
Path example : XVID/J/ bzw /XVID-BOXSET/

set pn [lindex {NEW in ARCHIVE XVID/J/JURASSIC.WORLD} 3]
# -> XVID/J/JURASSIC.WORLD
set path [file dirname $pn]
# -> XVID/J
set name [file tail $pn]
# -> JURASSIC.WORLD

Related

Reading the lines of file1 and insert them in between file2 line for every match

I need help to achieve this task of matching every line of file-1 and inserting them in between all the commands in file-2.
File-1(has 200+ entries nd some with quotes if spaces are used)
LES000A4304_UAT
LES000A2287_UAT
LES000A4273_UAT
LES000A4298_UAT
LES000A4285_UAT
"VES00134RT OIT"
"ALL LES0A PORT13200"
LES000A23473_UAT
LES000A2073_UAT
.
.
.
.
File-2(has 4 command lines)
set setting target name devices 01245678
set setting target name devices 04532342
set setting target name devices 03424524
set setting target name devices 09823424
The objective is to match every element(including the one with "") in file1 to be inserted in between the command string in file 2. So the output looks like this
Output
set setting target name LES000A4304_UAT devices 01245678
set setting target name LES000A4304_UAT devices 04532342
set setting target name LES000A4304_UAT devices 03424524
set setting target name LES000A4304_UAT devices 09823424
OR
set setting target name "VES00134RT OIT" devices 01245678
..
..
..
How can we achieve this using awk or sed
I managed to find a solution to my question using python for iterating through list
infile = open("file1.txt", "r")
for x in infile:
print('cmd1',x,'rest')
print('cmd2',x,'rest')
infile.close()

Pathlib with_name does not rename file

File is not renamed by with_name(). A test file is created at Path p using touch() and updated using with_name().
1.) Is there an issue caused by usage of Path vs PurePath? (no)
2.) Is it necessary to call replace() using the updated path object to change the file name on disk? (yes)
from pathlib import Path
p = Path('./test.txt')
p.touch()
print(f'p before: {p}')
# ==> p before: test.txt
# p is not updated
p.with_name('test_new.txt')
print(f'p after: {p}')
# ==> p after: test.txt
Just changing the path using with_name() is insufficient to rename the corresponding file on the filesystem. I was able to rename the file by explicitly calling .replace() with the updated path object (see below).
p.replace(p.with_name('test_new.txt'))
Per MisterMiyagi's comment above:
From the docs: "PurePath.with_name(name) Return a new path with the
name changed. [...]". You have to assign the result of p.with_name(),
or it is lost.
### WORKS ###
from pathlib import Path
# sets original path
p = Path('./test.txt')
print(p)
# ==> test.txt
# create file on disk
p.touch()
# prints updated path, but does not update p (due to immutability?)
print(p.with_name('test_new.txt'))
# ==> test_new.txt
# p not updated
print(p)
# ==> test.txt
# assignment to q stores updated path
q = p.with_name('test_new.txt')
print(q)
# ==> test_new.txt
# file on disk is updated with new file name
p.replace(p.with_name('test_new.txt'))
# p.replace(q) # also works
print(p)
# ==> test_new.txt
path.with_name() will generate a new Path object.
path.rename() will generate a new Path object and call the new path object's touch method.
So the path.rename() will be the proper way.

How can I get the relative path of the current director up to an arbitrary parent dir?

This is not a module just a workspace.
Folder structure:
workspace1
- workspace2
- workspace3
- workspace4
- workspace5
If I CD into workspace for the full path is: /Users/me/my-files/terraform/workspace1/workspace3/workspace4
How can I use terraform functions to be able to get just the path workspace1/workspace3/workspace4
Is there a way I can get the full path (https://www.terraform.io/docs/configuration/functions/abspath.html) and then trim out everything before workspace1? perhaps replace() can do that? There are many more features in the latest TF version though I want to check there isn't a feature that makes this easy I could not find in the docs.
# trying to use capture groups doesn't seem to work (just outputs full path)
locals {
test = replace(
abspath(path.root),
"/(.*)(workspace1.*)",
"$2"
)
}
output "test" { value = "${local.test}"
Edit
This match should work but it's not supported:
test = replace(
abspath(path.root),
"/.+?(?=workspace1)/",
"$1"
unsupported Perl syntax: `(?=`.
Assuming there aren't any complications like symlinks to make the problem more "interesting", perhaps you could do it by using abspath both on path.module and on a relative path from there to your codebase root and then use the length of the latter to trim the former. For example:
locals {
module_path = abspath(path.module)
codebase_root_path = abspath("${path.module}/../..")
# Trim local.codebase_root_path and one additional slash from local.module_path
module_rel_path = substr(local.module_path, length(local.codebase_root_path)+1, length(local.module_path))
}
In the above I'm assuming that the current module is two nesting levels under the codebase root, and using that to discover the absolute path of the codebase root in order to trim it off the absolute module path.
This works I think i had the syntax wrong:
#Folder structure
#workspace1
# - workspace2
# - workspace3
# - workspace4
# - workspace5
#abs path on disk: /Users/me/my-files/terraform/workspace1/workspace3/workspace4
locals {
test = replace(
abspath(path.root),
"/.+?(workspace1.*)/",
"$1"
)
}
output "test" { value = "${local.test}" }

Passing a string variable to glob

I'm trying to pass a directory to glob which will be read to a variable from a config file.
If I just do this it works fine:
path = '//Server/Company/Official Documents/**/*.pdf'
Files = glob.glob(path,recursive=True)
But if I try to do this I get an empty list:
path = Config[1][1]
Files = glob.glob('{path}**/*.pdf'.format(path=path),recursive=True)
For information,
print(Config [1][1])
gives this
'//Server/Company/Official Documents/'
You are missinng / in '{path}**/*.pdf'.format(path=path)
Try changinng it to:
'{path}/**/*.pdf'.format(path=path1)
like:
glob.glob('{path}/**/*.pdf'.format(path=path),recursive=True)

How to get the name of the directory from the name of the directory + the file

In an application, I can get the path to a file which resides in a directory as a string:
"/path/to/the/file.txt"
In order to write another another file into that same directory, I want to change the string "/path/to/the/file.txt" and remove the part "file.txt" to finally only get
"/path/to/the/"
as a string
I could use
string = "/path/to/the/file.txt"
string.split('/')
and then glue all the term (except the last one) together with a loop
Is there an easy way to do it?
You can use os.path.basename for getting last part of path and delete it with using replace.
import os
path = "/path/to/the/file.txt"
delete = os.path.basename(os.path.normpath(path))
print(delete) # will return file.txt
#Remove file.txt in path
path = path.replace(delete,'')
print(path)
OUTPUT :
file.txt
/path/to/the/
Let say you have an array include txt files . you can get all path like
new_path = ['file2.txt','file3.txt','file4.txt']
for get_new_path in new_path:
print(path + get_new_path)
OUTPUT :
/path/to/the/file2.txt
/path/to/the/file3.txt
/path/to/the/file4.txt
Here is what I finally used
iter = len(string.split('/'))-1
directory_path_str = ""
for i in range(0,iter):
directory_path_str = directory_path_str + srtr.split('/')[i] + "/"

Resources