Viewing and processing text

Learning objectives

Read source files and logs, search for useful messages, connect commands with pipes, and save normal output and errors separately.

1 Objective→ 2 Learn→ 3 Example→ 4 Practice→ 5 Verify→ 6 Continue
How to study this lessonUnderstand the idea first. Use the example to see the behaviour, practise where provided, then make sure you can explain the result before continuing.

Explanation

Why this matters

Compilers, simulators, lint tools, and regressions produce text logs. An engineer must find failures quickly without changing the original log.

Commands you will use

Command What it does Example
cat Prints a short file. cat result.txt
less Opens a long file for scrolling and searching. Press / to search and q to exit. less compile.log
head Prints the first lines. head -n 20 compile.log
tail Prints the last lines; -f follows a growing log. tail -f simulation.log
grep Prints lines matching text or a regular expression. grep -n 'ERROR' simulation.log
wc Counts lines, words, or bytes. wc -l simulation.log
sort Sorts input lines. sort tests.txt
uniq Combines adjacent duplicate lines, usually after sort. sort tests.txt \| uniq -c
cut Selects fields from each line. cut -d' ' -f1 result.log
awk Selects and calculates with fields. awk '{print $2}' result.log
sed Selects or transforms a text stream. sed -n '1,10p' result.log
diff Shows differences between two files. diff -u expected.txt actual.txt
tee Displays output and saves a copy. command \| tee run.log

Pipes and redirection

Operator Meaning Example
\| Send normal output to the next command. grep PASS run.log \| wc -l
> Replace a file with normal output. command > out.log
>> Append normal output to a file. command >> out.log
2> Replace a file with error output. command 2> error.log
2>&1 Send errors to the same destination as normal output. command > all.log 2>&1

Check the destination before using > because it overwrites existing content.

Worked example

Create a small simulation-style log and analyze it:

mkdir -p "$HOME/asic-linux-practice/logs"
cd "$HOME/asic-linux-practice"
printf 'PASS reset_test\nFAIL write_test\nPASS read_test\n' > logs/run.log
grep -n '^FAIL ' logs/run.log | tee logs/failures.txt
cut -d' ' -f1 logs/run.log | sort | uniq -c
tail -n 2 logs/run.log

Expected result: failures.txt contains FAIL write_test; the count shows two PASS lines and one FAIL line; tail prints the last two tests.

RTL/DV use

grep -REn 'UVM_ERROR|UVM_FATAL|FAIL' logs/
grep -RIl '^module ' rtl tb

The first command finds serious verification messages. The second lists source files containing a module declaration.

Common mistakes

  • Using cat for a very large log instead of less.
  • Forgetting that grep returns status 1 when it finds no match.
  • Running uniq before sort and expecting all duplicates to combine.
  • Overwriting a useful log with >.
  • Omitting quotes around a path stored in a variable.

Check yourself

What is the difference between > and >>? Why is sort normally used before uniq?

Engineering practice

Practice: Viewing and processing text

Task→Run / reason→Check result→Explain why

Create a log with PASS, WARNING, ERROR, and FAIL lines. Produce a summary count and save only the error and failure lines in a second file.

Practice work is non-certified and is not stored by ASIC Campus.