I have put some values in a file. In the puppet manifests I want to get the value of these variables. Is there a specific directory to put my file? Also, what should be the format of the file?
If this file on puppet-server, you can write functions for module. For example:
modules/my_module/lib/puppet/parser/functions/get_var.rb:
$: << File.expand_path(File.join(File.dirname(__FILE__), '.'))
module Puppet::Parser::Functions
newfunction(:get_var,
:type => :rvalue) do |args|
file_name = args[0]
f = File.open(file_name)
s = f.readline()
return s
end
end
And use it in manifests: $test = get_var('/etc/puppet/configs.txt'). This function return first string from file, but you can change it for your needs.
For file on client, you can write facter.
Related
I am trying to write a MATLAB function that extends the "save" command to create the specified path if it doesn't already exist. I have been successful with a single variable, but not with multiple variables. With multiple variables, I receive the following error message:
31 variables_to_save(i) = variable_name;
Conversion to cell from char is not possible.
Error in saveMatVars (line 31)
variables_to_save(i) = variable_name;
Here is my function:
function saveMatVars( file_path, varargin )
%saveMatVars saves variables in varargin to file specified in file_path, creating the file path if it doesn't exist
% varargin is the variables to save
% get path and filename from file_path
[parent_folder, ~] = fileparts(file_path);
% if parent_folder doesn't exist, create it
if ~exist(parent_folder, 'dir')
mkdir(parent_folder);
end
% get names of variables to save
num_vars = length(varargin);
switch num_vars
case 0
return
case 1
variable_name = inputname(2);
variable_name = matlab.lang.makeValidName(variable_name);
variable_val = varargin{1};
feval(#()assignin('caller', variable_name, variable_val)); % create
a dummy function so I can access the current function's workspace
variables_to_save = variable_name;
otherwise % note - this part does not work
variables_to_save = cell(1,1);
for i = 1:num_vars
variable_name = inputname(i+1);
variable_name = matlab.lang.makeValidName(variable_name);
variable_val = varargin{i};
feval(#()assignin('caller', variable_name, variable_val)); % create a dummy function so I can access the current function's workspace
variables_to_save(i) = variable_name;
end
variables_to_save = string(variables_to_save);
end
% save variables_to_save in file_path
save(file_path, variables_to_save);
return
end
The documentation on "save" says the following about the variables to save:
save(filename,variables) saves only the variables or fields of a structure array specified by variables.
I have tried various functions for converting char arrays to strings and to cell arrays to no avail.
I am trying to modify a text file I am using PHP or also I can use the C# the file that I am working on a text file consists of strings for example
TM_len= --------------------------------------------
EMM_len --------------------------------------------
T_len=45 CTGCCTGAGCTCGTCCCCTGGATGTCCGGGTCTCCCCAGGCGG
NM_=2493 ----------------ATATAAAAAGATCTGTCTGGGGCCGAA
and I want to delete those four lines from the file if I found that one line consists of only "-" no characters in it and of course save to the file.
Maybe something like this? I wrote it in a easy to understand and "not-shortened" way:
$newfiledata = "";
$signature = " ";
$handle = fopen("inputfile.txt", "r"); // open file
if ($handle) {
while (($line = fgets($handle)) !== false) { // read line by line
$pos = strpos($line, $signature); // locate spaces in line text
if ($pos) {
$lastpart = trim(substr($line, $pos)); // get second part of text
$newstring = trim(str_replace('-', '', $line)); // remove all dashes
if (len($newstring) > 0) $newfiledata .= $line."\r\n"; // if still there is characters, append it to our variable
}
}
fclose($handle);
}
// write new file
file_put_contents("newfile.txt", $newfiledata);
thanks for your response but there nothing happened on the file please check the link of the file and another link of the desired output for the file.download the file and required output file
For a file path name such as
val path = "$HOME/projects/$P1/myFile.txt"
is there a simpler way to resolve the path and read myFile.txt than this,
import java.io.File
val resolvedPath = path.split(File.separator).map{ s =>
if (s.startsWith("$")) sys.env(s.drop(1))
else s }.
mkString(File.separator)
val res = io.Source.fromFile(resolvedPath).getLines
The way you have seems good to me, but if you are so inclined or need to do something quickly, you could use Process to get the return of executing a bash command:
import scala.sys.process._
val cleanedPath = Seq("bash", "-c", "echo " + path).!!.trim
You can even use this idea to read the file if you want:
val text = Seq("echo", "-c", "cat " + path).!!
One difference between these and your code is that your code will throw an exception if an environment variable is missing, while bash returns an empty string for that variable. If you wish to mimic that, you could use sys.env.get(s.tail).getOrElse("") instead of sys.env(s.drop(1)) or use the dictionary val myEnv = sys.env.withDefaultValue("").
See System.getenv(). You'll be able to find the variables and replace them with the value to resolve your path.
I want to read data from a file and save it into an array. Then insert some new data into this array and then save this new data back into the same file deleting what is already there. My code works perfectly, giving me my required data, when I have 'r+' in the fopen parameters, however when I write to the file again it does not delete the data already in the file just appends it to the end as expected. However when I change the permissions to 'w+' instead of 'r+', my code runs but no data is read in or wrote to the file! Anyone know why this might be the case? My code is as seen below.
N = 1021;
b = [0;0;0;0;0];
% Opens file specified by user.
fid = fopen('testing','w+');
% Read header data
Header = fread(fid, 140);
% Move to start of data
fseek(fid,140,'bof');
% Read from end of config header to end of file and save it in an array
% called data
Data = fread(fid,inf);
Data=reshape(Data,N,[]);
b=repmat(b,[1 size(Data,2)]);
r=[b ; Data];
r=r(:);
r = [Header;r];
% write new values into file
fwrite(fid,r);
fclose(fid);
% Opens file specified by user.
fid = fopen('test');
All = fread(fid,inf);
fclose(fid);
According to the documentation, the w+ option allows you to "Open or create new file for reading and writing. Discard existing contents, if any." The contents of the file are discarded, so Data and Header are empty.
You need to set the position indicator of the filehandle before writing. With frewind(fid) you can set it to the beginning of the file, otherwise the file is written / appended at the current position.
N = 1021;
b = [0;0;0;0;0];
% Opens file specified by user.
fid = fopen('testing','r+');
% Read header data
Header = fread(fid, 140);
% Move to start of data
fseek(fid,140,'bof');
% Read from end of config header to end of file and save it in an array
% called data
Data = fread(fid,inf);
Data=reshape(Data,N,[]);
b=repmat(b,[1 size(Data,2)]);
r=[b ; Data];
r=r(:);
r = [Header;r];
% write new values into file
frewind(fid);
fwrite(fid,r);
fclose(fid);
% Opens file specified by user.
fid = fopen('test');
All = fread(fid,inf);
fclose(fid);
I was wondering how do I get a line into an array with lua in some sort of function
eg. FileToArray("C:/file.txt")?
I know I can use:
var = io.open("file")
Data = var:read()
But it only returns the 1st line, and no other lines.
Anyone know how to fix this or a different way? I'm new to lua and the file system stuff.
You can pass "*a" to read function, it should read the whole file:
local file = io.open("file-name", "r");
local data = file:read("*a")
And if you want to store each line in an array. Like Jane's solution you can use
io:lines () - which returns iterator function (each call gives you a new line)
local file = io.open("file-name", "r");
local arr = {}
for line in file:lines() do
table.insert (arr, line);
end
local file = io.open("c:\\file.txt")
local tbllines = {}
local i = 0
if file then
for line in file:lines() do
i = i + 1
tbllines[i] = line
end
file:close()
else
error('file not found')
end
See: http://lua-users.org/wiki/IoLibraryTutorial for more information.