I need to remove a value from file in local directory.
The format of the file is .txt. I need to remove a complete line.
How can I do this using Robot Framework?
A similar question to this has been asked before: Looping through the content of a file in RobotFramework. Using the example from this link I created a file called test.csv with the following content:
1001
1002
1003
1004
Then proceeded to create a robot file with the following code. It reads the csv file into memory, deletes that file and creates a new one with the same name. The proceeds to loop through the in-memory version and writes every line into the file with the exception of 1 line holding the value 1003.
*** Settings ***
Library OperatingSystem
Library String
*** Test Cases ***
Example of looping over the lines in a file
${contents}= Get File test.csv
Remove File test.csv
Create File test.csv
#{lines}= Split to lines ${contents}
:FOR ${line} IN #{lines}
\ log ${line}
\ Run Keyword If ('${line}'<>'1003') Append To File test.csv ${line}\n
Although this example will work, the real question is whether you should do this in Robot Script. I agree with Bryan Oakley that this would be better suited for Python where you have more control over the file.
Related
realpath <<<'foo' fails "realpath: missing operand". I don't know what that means.
realpath <(<<<'foo') returns /proc/3443695/fd/pipe:[26244650] which I guess means it's creating a temporary pipe which will contain the string "foo".
Or maybe printf is more clear:
❯ printf "%q" <<<'foo' # no output
❯ printf "%q" <(<<<'foo')
/proc/self/fd/11%
The actual program I'm trying to call doesn't like either of those. I think I need an actual file.
I can do that in multiple commands by creating a file with mktemp and then writing to it, and then sending that off as the arg, but does zsh have any convenient syntax for doing this in-place? A 1-liner?
It looks like the =(list) process substitution should do what you want.
From the zshexpn man page:
If =(...) is used instead of <(...), then the file passed as an
argument will be the name of a temporary file containing the output
of the list process. This may be used instead of the < form for a
program that expects to lseek on the input file.
...
The temporary file created by the process substitution will be deleted when the function exits.
On my system, realpath =(<<<'foo') returns something like /private/tmp/zsh3YAdDx, i.e. the name of a temporary file that does indeed appear to be deleted after executing the command.
As a bonus, the documentation notes that in some cases the =(<<<...) form is optimized to execute completely in the current shell.
As part of our process, we get an input file in the .gz format. We need to unzip this file and add some suffix at the end of the file. The input file has timestamp so I am trying to use filter while unzipping and renaming this file.
Input file name :
Mem_Enrollment_20200515130341.dat.gz
Step 1:
Unzipping this file : (working as expected)
gzip -d Mem_Enrollment_*.dat.gz
output :
Mem_Enrollment_20200515130341.dat
Step 2: Renaming this file : (issues while renaming)
Again, I am going with the pattern but I know this won't work in this case. So, what should I do rename this file?
mv Mem_Enrollment_*.dat Mem_Enrollment_*.dat_D11
output :
Mem_Enrollment_*.dat_D11
expected output :
Mem_Enrollment_20200515130341.dat_D11
try
for fn in Mem_Enrollment_*.dat
do
mv ${fn} ${fn}_D11;
done
With just datastage you could loop over ls output from an execute command stage via "ls Mem_Enrollment_*.dat.gz" and then use an #FM as a delimiter when looping the output list. You could then breakout the gzip and rename into two separate commands, which helps with readability in your job.
Only caveat here is that the Start Loop stage doesn't accept the #FM in the delimiter due to some internal funkyness inside Datastage. So you need to set a user variable equal to it and pass that to the mark.
I have copied a file in my current working directory. This file has a text:
order value=78
I want to replace this text with new one
parcel value= 500
How can I do this?
Using Get File from OperatingSystem Library you can read the contents of the file,
and using Replace String from String Library you can replace the string and
using Create File from OperatingSystem library you can create the file.
If the file, in which you want to make replacement, is large AND you don't want to load the whole file contents in a variable, then you can make use of Run keyword from OperatingSystem Library. The keyword will execute the shell command, here it is "sed" and make the replacement.
(Of course, you should be on Linux in this case)
For example:
*** Settings ***
| Library | OperatingSystem
*** Test Cases ***
| Example of replacing the text in a file
| | ${result}= | Run | sed -i 's/foo/bar/g' myfile.txt
The variable result will hold the stdout which you get after running the shell command.
I am using a python script for this. Put the code below in a .py file and load this in your robot file using Library in the settings part.
Next in the robot file use this keyword as:
Replace line in file | file | searchExp1 | replaceLine
def Replace_line_in_file(file,searchExp1,replaceLine):
""" Open a file (like input.txt) and find the line that
contains the string searchExp1.
and replace that complete line by replaceLine.
When there are multiple lines that contain searchExp1
then all those lines will be replaced
"""
for line in fileinput.input(file, inplace=1):
if searchExp1 in line:
line = replaceLine+'\n'
sys.stdout.write(line)
I tried using the above keyword "Replace line in file".
But I get the error: Exception TypeError: TypeError("'NoneType' object is not callable",) in > ignored
What other changes are required.
I am using the shell() command to generate pdf documents from .tex files within a function. This function sometimes gets ran multiple times with adjusted data and so will overwrite the documents. Of course, if the pdf file is open when the .tex file is ran, it generates an error saying it can't run the .tex file. So I want to know whether there are any R or Windows cmd commands which will check whether a file is open or not?
I'm not claiming this as a great solution: it is hacky but maybe it will do. You can make a copy of the file and try to overwrite your original file with it. If it fails, no harm is made. If it succeeds, you'll have modified the file's info (not the contents) but since your end goal is to overwrite it anyway I doubt it will be a huge problem. In either case, you'll be fixed about whether or not the file can be rewritten.
is.writeable <- function(f) {
tmp <- tempfile()
file.copy(f, tmp)
success <- file.copy(tmp, f)
return(success)
}
openfiles /query /v|(findstr /i /c:"C:\Users\David Candy\Documents\Super.xls"&&echo File is open||echo File isn't opened)
Output
592 David Candy 1756 EXCEL.EXE C:\Users\David Candy\Documents\Super.xls
File is open
Findstr returns 0 if found and 1+ if not found or error.
& seperates commands on a line.
&& executes this command only if previous command's errorlevel is 0.
|| (not used above) executes this command only if previous command's errorlevel is NOT 0
> output to a file
>> append output to a file
< input from a file
| output of one command into the input of another command
^ escapes any of the above, including itself, if needed to be passed to a program
" parameters with spaces must be enclosed in quotes
+ used with copy to concatinate files. E.G. copy file1+file2 newfile
, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,
%variablename% a inbuilt or user set environmental variable
!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command
%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.
%* (%*) the entire command line.
%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.
.
--
In R i have a script gets content of multiple xls files <Loop over directory to get Excel content>.
All files are about 2 MB. The script takes a few seconds for 3 files, but is now running for 6 hours on a Debian i7 system without results on 120 files.
A better solution is therefore [hopefully] to convert all xls files to csv using ssconvert, using a bash script <Linux Shell Script For Each File in a Directory Grab the filename and execute a program>:
for f in *.xls ; do xls2csv "$f" "${f%.xls}.csv" ; done
This script does the job, however my content is in sheet nr 14, whereas the csv files produced by this script just return the first sheet [i replaced 'xls2csv' with 'ssconvert'].
Can this script be adopted to pickup only sheet nr 14 in the workbook?
If you know the worksheet name, you can do this:
for f in *.xls ; xls2csv -x "$f" -w sheetName -c "${f%.xls}.csv";done
To see all the xls2csv details see here.
EDIT
The OP find the right answer, so I edit mine to add it :
for f in *.xls ; do xls2csv -x "$f" -f -n 14 -c "${f%.xls}.csv"
For this job I use a python script named ssconverter.py (which you can find here, scroll down and download the two attachments, ssconverter.py and ooutils.py), which I call directly from R using system().
It can extract a specific sheet in the workbook, not only by name but also by sheet number, for example:
ssconverter.py infile.xls:2 outfile.csv
to extract the second sheet.
You need to have python and python-uno installed.