Skip to content
Advertisement

Read line by line and print matches line by line

I am new to shell scripting, it would be great if I can get some help with the question below.

I want to read a text file line by line, and print all matched patterns in that line to a line in a new text file.

For example:

JavaScript

The intended output is as follows:

JavaScript

I tried the following code:

JavaScript

which produced this output:

JavaScript

Then I also tried this:

JavaScript

But did not help 🙁

Maybe there is another way, I am open to awk/sed/cut or whatever… 🙂

Note: There can be any number of Error codes (i.e. XX:XXXX, the pattern of interest in a single line).

Advertisement

Answer

There’s always perl! And this will grab any number of matches per line.

JavaScript

-e perl code to be run by compiler and -n run one line at a time and -l automatically chomps the line and adds a newline to prints.

The regex implicitly matches against $_. So @matches = $_ =~ //g is overly verbose.

If there is no match, this will not print anything.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement