0

I am writing a KornShell (ksh) script that is logging to a file. I am redirecting the output of one of my commands (scp) to the same file, but I would like to add a tab at the start of those lines in the log file if possible.

Is this possible to do?

EDIT: Also I should mention that the text I am redirecting is coming from stderr. My line currently looks like this:

scp -q ${wks}:${file_location} ${save_directory} >> ${script_log} 2>&1
javaPlease42
  • 4,699
  • 7
  • 36
  • 65
JLZenor
  • 1,460
  • 2
  • 14
  • 23

1 Answers1

2

Note: the below doesn't work for ksh (see this question for possible solutions).


You probably can do something like

my_command | sed 's/^/\t/' >> my.log

The idea is to process the output of the command with a stream editor like sed in some manner. In this case, a tab will be added at the beginning of every line. Consider:

$ echo -e 'Test\nfoobar' | sed 's/^/\t/'
    Test
    foobar

I haven't tested this in ksh, but a quick web search suggests that it should work.

Also note that some commands can write to both stdout and stderr, don't forget to handle it.

Edit: in response to the comment and the edit in the question, the adjusted command can look like

scp -q ${wks}:${file_location} ${save_directory} 2>&1 | \
    sed 's/^/\t/' >> ${script_log}

or, if you want to get rid of stdout completely,

scp -q ${wks}:${file_location} ${save_directory} 2>&1 >/dev/null | \
    sed 's/^/\t/' >> ${script_log}

The technique is described in this answer.

Community
  • 1
  • 1
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
  • Ah yes, you bring up another point I forgot to mention. The text I am logging is coming from stderr. Will that make a difference in how I implement this? – JLZenor Dec 06 '12 at 17:18
  • @MasterZ You just have to adjust the redirections. See [this question](http://stackoverflow.com/q/2342826/1258041), for example. – Lev Levitsky Dec 06 '12 at 17:20
  • I modified my code to take into account the link you gave but now that part of the script does not run at all and I get the error "Unrecognized Command: /^/\t/"... I edited my original question with the code I am using if that helps. – JLZenor Dec 06 '12 at 17:27
  • @MasterZ The problem seems to be in the call to `sed`. Can you show how you use it? – Lev Levitsky Dec 06 '12 at 17:30
  • Thanks for adding the code to your answer. I'm glad to see I was not too far off in how I was testing this, I just forgot the s/ in the sed. But, it is not yet 100%. a "t" is added to the beginning of the line instead of a tab. – JLZenor Dec 06 '12 at 17:32
  • @MasterZ Does adding `$` help? `sed $'s/^/\t/'` – Lev Levitsky Dec 06 '12 at 17:38
  • No, using the $ did not help. Also using multiple back slashes did not help either. – JLZenor Dec 06 '12 at 18:24
  • @MasterZ See the link I added at the top of my answer. It's basically to an identical question, and that one has answers working on `ksh`. – Lev Levitsky Dec 06 '12 at 18:34
  • Thanks for the link. I used the awk command listed in that link "awk '{print "\t" $0}' and it worked great. – JLZenor Dec 06 '12 at 18:59