Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirect output to file 1

Status
Not open for further replies.

tikual

Technical User
Jun 10, 2003
237
HK
Now I have a problem in format print in awk and redirect it to a file.

For some reasons, I need to print few command lines to a file to become a command list file.

e.g.
rsh otherhost command_here var='var with space'

I tried to use print:
print "rsh otherhost command_here var='var with space'" > "filename"
Not work.

I tried to use printf:
printf "%s\n", "rsh otherhost command_here var='var with space'" > "filename"
printf "%s%s\n", "rsh otherhost command_here var=", "'var with space'" > "filename"

Both not work.

The difficulty is printing the single quote although using backslash.

Hope someone can help.

Thanks!


tikual
 
An illegible solution :
[tt]
awk '{ print "rsh otherhost command_here var='"'"'var with space'"'"'" > "filename" }'
[/tt]
A better solution, put your script in a separate file and use the -f option of awk.
[tt]
#my_script.awk
{
print "rsh otherhost command_here var='var with space'" > "filename"
}#----

awk -f my_script.awk
[/tt]

Jean Pierre.
 
Aigles, thanks for your help.

Actually my situation is more difficult to this example. Let me share my final version to you.

print "rsh otherhost /opt/OV/bin/OpC/opcmsg a=sensor s=critical o=root msg_g=OS msg_t='"'High Temperature in CRM-PROD. DEVICE: "'"$1"'" TEMPERATURE: "'"$2"'"'"'" > "file1"

I found that the simply print single quote solution.
single double single
And I need to protect the field output, so:
double single double

Now it works fine but make me giddy. :p

Anyway, thanks again! A star for you.


tikual
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top