0

I have one text file in host level Ex: /tmp/file.txt. So I have one script and I need to read the line by line from file.txt, so I used sed command. So I am using while loop in the script and I have logged in to DB level, how can I use sed command or similar to sed for accessing one line data from file.txt ?in database level

Ex: sed -n '5p' "/tmp/file.txt" (or) awk 'NR==5' /tmp/file.txt

So this output I want to check in db level after isql command? Is it possible. Can any one suggest the alternate?

Thomas
  • 4,225
  • 5
  • 23
  • 28

2 Answers2

0

You are probably looking for the Sybase Adaptive Server's xp_cmdshell:

Description

Executes a native operating system command on the host system running Adaptive Server.

Syntax

xp_cmdshell command [, no_output]

For your commands that would be:

xp_cmdshell 'sed -n 5p /tmp/file.txt'

xp_cmdshell 'awk "NR==5" /tmp/file.txt'

Be careful with different quotation types when using quotes inside quotes.

Esa Jokinen
  • 46,944
  • 3
  • 83
  • 129
  • I have tried that too. It is not working.. 1> xp_cmdshell command [ awk 'NR==5' /userids.txt,no_output] 2> go Msg 102, Level 15, State 181: Server '', Line 1: Incorrect syntax near '[ awk 'NR==5' /userids.txt,no_outpu'. Tried for sed also the same syntax error. i have tried exec xp_cmd shell also but no use – DhanaTeja Jun 23 '17 at 08:38
  • I bet you're supposed to replace 'command' with the actual command: `xp_cmdshell awk 'NR==5' /userids.txt` – Brandon Xavier Jun 23 '17 at 11:58
  • Exactly! That's only the **syntax**. In that kind of lines `[ ]` indicates that the `, no_output` is optional. The `command` is replaced with the actual command. See the linked manual: it has examples and everything. – Esa Jokinen Jun 23 '17 at 12:05
0

Re: So this output I want to check in db level after isql command? Is it possible. Can any one suggest the alternate?

Are you saying: 1) want to process the output of a db level command using shell script syntax? Or, 2) Or take the output of a shell script and send it to be processed in the database?

If 1), there's a alternate to isql called sqsh. You can do this with sqsh:

sqsh -SMYSERVER -Dmydb -Umylogin -w180
<mypassword>
select blah_column from blah_table
go |  sed 's/thingy/widget/g'

If 2), then have your shell script produce SQL commands for output and run that against the db using isql -i (or sqsh -i)

Note, if using awk in sqsh, be careful. Sometimes it needs extra backslashes for the dollar signs

Ben Slade
  • 149
  • 1
  • 5