LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Script (adding a prefix to output lines) (https://www.linuxquestions.org/questions/programming-9/shell-script-adding-a-prefix-to-output-lines-273775/)

rheza 01-04-2005 01:05 PM

Shell Script (adding a prefix to output lines)
 
Hello, I'd like to ask about a command in the shell which would allow me to add a prefix to the an output of mulitple lines. Like say when I do a command:

ls

is there a way to prefix it outputs with a string "string here" such that it will look like this

string here file1
string here file2
string here file3
string here anotherfile

instead of

file1
file2
file3
anotherfile

Hko 01-04-2005 01:13 PM

Code:

#!/bin/bash

ls | while read FILENAME ; do
        echo "String here $FILENAME"
done


Hko 01-04-2005 01:15 PM

Or just:
Code:

ls | sed 's/.*/string here &/'

rheza 01-04-2005 01:18 PM

I wasn't really using it for ls, but the second one works great. Thanks Hko!


All times are GMT -5. The time now is 08:23 PM.