How to prefix output lines?

0250

To prefix output lines in grep, you can use the -H option, which prints the filename before each matching line. However, if you want to add a custom prefix to each line, you can use sed or awk in combination with grep. Here are examples using both methods:

Using sed:

grep "search_term" filename.txt | sed 's/^/PREFIX: /'

This command searches for "search_term" in filename.txt and prefixes each matching line with "PREFIX: ".

Using awk:

grep "search_term" filename.txt | awk '{print "PREFIX: " $0}'

This command does the same thing, using awk to print "PREFIX: " followed by the original line.

Feel free to ask if you need further assistance!

0 Comments

no data
Be the first to share your comment!