0

I have a shell script which is redirecting a specific line from a file using sed.

sed -n "${line_num}p" $1 >> output.txt

I want to color this line before redirecting the line to output.txt.

How I can do it?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Nikhil S
  • 19
  • 2
  • 4
    Text files don't contain color. You can write terminal color codes to the file but then it isn't "text" anymore. – Etan Reisner Feb 24 '15 at 15:05
  • Etan, all I need is to grep some lines from a file based on line numbers and redirect it to another file.Before redirecting I need to modify the color of lines. – Nikhil S Feb 24 '15 at 15:27
  • 1
    Text lines don't **have** color. You can get color to your terminal by using terminal-specific (but generically queryable) escape/binary codes. You *can* write those to a file but then you no longer have a **text** file you have a binary file with text in it and text editors/etc. may not handle it correctly. – Etan Reisner Feb 24 '15 at 15:40

1 Answers1

0

As noted, "text" has no color. But you can do something like this:

BLUE=$(tput setaf 4)
NONE=$(tput op)
sed -n "${line_num}p" $1 | sed -e 's/^\(.*\)$/'"$BLUE"'\1'"$NONE"'/' >> output.txt

to add terminal-specific escape sequences to the output.

tput is a (n)curses utility which (see manpage) looks up terminal capabilities and prints the result -- which you can save into a variable and use in a script. See for example these:

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105