1

Topic: Redirect stderr + the message

Greetings. The trivial task, but I can not understand in any way how to make smile There are many scripts. It is necessary for me  stderr in a file and to add the message on what script had been generated an error. Like "script.sh 2>> echo" script.sh - "error.log"

2

Re: Redirect stderr + the message

andrey10;

# ls-1/nofile1/nofile2/tmp 2>> (while read line; do echo "my testing prefix: $line"; done)
/tmp:
hsperfdata_oracle
kde-oracle
ksocket-oracle
vmware-root
my testing prefix: ls: cannot access/nofile1: No such file or directory
my testing prefix: ls: cannot access/nofile2: No such file or directory

3

Re: Redirect stderr + the message

well and to a heap other variants of adding of a prefix in each line
With sed:

$ sed ' s / ^/myprefix_ / ' a.txt
myprefix_aaa
myprefix_bbb
myprefix_ccc
myprefix_ddd

This replaces every line beginning ^ with myprefix_. Note that ^ is not lost, so this allows to add content to the beginning of each line.
You can make your awk's version shorter with:

$ awk ' $0 = "myprefix _" $0 ' a.txt
myprefix_aaa
myprefix_bbb
myprefix_ccc
myprefix_ddd

or passing the value:

$ prefix = "myprefix _"
$ awk-v prefix = "$ prefix" ' $0=prefix$0 ' a.txt
myprefix_aaa
myprefix_bbb
myprefix_ccc
myprefix_ddd

It can also be done with nl:

$ nl-s "prefix _" a.txt | cut - C7 -
prefix_aaa
prefix_bbb
prefix_ccc
prefix_ddd

Finally: as John Zwinck explains, you can also do:

paste-d "<(yes prefix _) a.txt | head-n $ (wc-l a.txt)

on OS X:

paste-d '\0 ' <(yes prefix _) a.txt | head-n $ (wc-l <a.txt)