How to check if a file is empty in VERILOG? - verilog

I would like to know how can I check if a file is empty in a VERILOG or SYSTEMVERILOG testbench.
I have 2 ideas:
Check the file size using $system() task, and put there a linux command which can tell the number of bits or bytes.
Read the first line using $fgets. If the line equal to 0, it means it's empty.
About the first method, I couldn't get a linux command which tells me just the number. I've tried for example ls -l and wc -c, but they give me much more than the number of bits.
About the second method, I really don't know how to read a specific line, in this case, it would be the first line of the file.

Assuming you've already checked the result from $fopen to see if the file exists, you can use $getc or $gets an see if it returns a code less than 1. But the best option depends on what you plan to do with the file after finding it empty or non empty.

Related

copy and append specific lines to a file with specific name format?

I am copying some specific lines from one file to another.
grep '^stringmatch' /path/sfile-*.cfg >> /path/nfile-*.cfg
Here what's happening: its creating a new file called nfile-*.cfg and copying those lines in that. The file names sfile- * and nfile- * are randomly generated and are generally followed by a number. Both sfile-* and nfile-* are existing files and there is only one such file in the same directory. Only the number that follows is randomly generated. The numbers following in sfile and nfile need not be same. The files are not created simultaneously but are generated when a specific command is given. But some lines from one file to the another file needs to be appended.
I'm guessing you actually want something like
for f in /path/sfile-*.cfg; do
grep '^stringmatch' "$f" >"/path/nfile-${f#/path/sfile-}"
done
This will loop over all sfile matches and create an nfile target file with the same number after the dash as the corresponding source sfile. (The parameter substitution ${variable#prefix} returns the value of variable with any leading match on the pattern prefix removed.)
If there is only one matching file, the loop will only run once. If there are no matches on the wildcard, the loop will still run once unless you enable nullglob, which changes the shell's globbing behavior so that wildcards with no matches expand into nothing, instead of to the wildcard expression itself. If you don't want to enable nullglob, a common workaround is to add this inside the loop, before the grep;
test -e "$f" || break
If you want the loop to only process the first match if there are several, add break on a line by itself before the done.
If I interpret your question correctly, you want to output to an existing nfile, which has a random number in it, but instead the shell is creating a file with an asterisk in it, so literally nfile-*.cfg.
This is happening because the nfile doesn't exist when you first run the command. If the file doesn't exist, bash will fail to expand nfile-*.cfg and will instead use the * as a literal character. This is correct behaviour in bash.
So, it looks like the problem is that the nfile doesn't exist when you start your grep. You'll need to create one.
I'll leave code to others, but I hope the explanation is useful.

bash "echo" including ">" in the middle creating file - please explain

When I write:
echo 2*3>5 is a valid inequality
In my bash terminal, a new file named 5 is created in my directory which contains:
2*3 is a valid inequality
I want to know what exactly is going on here and why am I getting this output?
I believe it's obvious that I'm new to Linux!
Thanks
In bash, redirections can occur anywhere in the line (but you shouldn't do it! --- see the bash-hackers tutorial). Bash takes the >5 as a redirection, creates output file 5, and then processes the rest of the arguments. Therefore, echo 2*3 is a valid inequality happens, which gives you the output you see in the output file 5.
What you probably want is
echo "2*3>5 is a valid inequality"
or
echo '2*3>5 is a valid inequality'
(with single-quotes), either of which will give you the message you specify as a printout on the command line. The difference is that, within "", variables (such as $foo) will be filled in, but not within ''.
Edit: The bash man page says that the
redirection operators may precede or appear anywhere within a simple command or may follow a command. Redirections are processed in the order they appear, from left to right.
bash does the output redirection first i.e. >5 is done first and a file named 5 is created (or truncated if it already exists). The resultant file descriptor remains open for the runtime of the echo command.
Then the remaining portion, 2*3 is a valid inequality, runs as the argument to echo and standard output is saved in the (already-open) file 5 eventually.
To get the whole string as the output, use single or double quotes:
echo '2*3>5 is a valid inequality'
This is an example of output redirection. You're instructing the echo statement to, instead of writing to standard out, write to a filename. That filename happens to be "5".
You can avoid that behavior by quoting:
echo "2*3>5 is a valid inequality"

How to append data in a PS file?

I have the following line in a PS file
version
!cd "//''"
the another file contains the following line
remove
I want to append data from another file in between the single quotes of the above line. After Appending the characters(remove) from another the ps file should contain the following data
version
!cd "/remove/''"
I tried using OUTREC but it din work, the characters versions also got changed
SORT FIELDS=COPY
OUTREC FIELDS=(1:C'!cd "/',
6:1,6,
12:C''"')
Your FIELDS (which is better as BUILD) should be conditional to avoid processing every line.
You've not mentioned how you are getting the data from the other file.
Best would be two steps. Step 1, to create a "symbol file" (SYMNAMES DD when it is used in the second step) to take the data from your second file and give it a name.
Then in the second step, with conditional processing (IFTHEN=(WHEN=(logicalexpression)) to use the value of the symbol as the insertion.
I'm assuming your second file can sometimes contain different values? If not, why not just generate the whole thing? Or use your editor?

linux cmp utility output: what is a "line"?

Could someone tell me what the "line" number represents in the output of a cmp command? I ask this because, first, I can't find it explained anywhere. Second, I am getting results comparing a set of files where the "char" outputs are identical (as expected) but the "line" outputs differ wildly.
The "line" outputs reflect the number of newline characters seen prior to that point in the file.
For a file which is not in a textual format, the "line" output is not likely to be meaningful, and can be ignored; for a file which is in textual format, the line number returned could be used in a text editor to navigate to the area with the difference.
Per the POSIX spec for cmp:
For files which are not text files, line numbers simply reflect the presence of a <newline>, without any implication that the file is organized into lines.
Because by default cmp prints only the first difference seen, the line numbers between both files are guaranteed to be identical at that point. When passed -l, cmp continues beyond the first difference -- but no longer prints line numbers, thus avoiding any ambiguity as to which file's line number count is canonical.

How to read specific line of files with a Linux command or Shell Script

I need to be able to read the second last line of all the files within a specific directory.
These files are log files and, that specific line contains the status of tasks that ran, 'successful', 'fail', 'warning'.
I need to pull this to dump it after in reports.
At this stage i am looking only to pull the data, so the entire line, and will worry about the handling after.
As the line numbers are not set, they are irregular, I am looking at doing it with a 'while' loop, so it goes through the whole thing, but i am actually not getting the last 2 lines read, and also, i can read 1 file not all of them.
Any ideas on a nice little script to do this?
And anyone knows if this can be just done with just a linux command?
Use the tail command to get the last 2 lines, and then the head command to get the first of these:
for file in $DIR/*; do
tail -2 "$file" | head -1
done

Resources