My question is very simple, I am trying to print a combination of text and numbers, by using the SELECT statement. However, when I try to print text using this method, I get a lot of miscellaneous - chars.
How can I use the select statement without having it print the - chars?
For example:
SELECT "HELLO WORLD"
Would result in:
---------------------
HELLO WORLD
When lauching isql use the -b option to suppress header printing.
isql -Uusername -Sservername -b
Related
I have a tab delimited file I am trying to import into SQL Server 2012; the row terminator is CRLF. The following is my BCP statement in PowerShell:
bcp database.dbo.table IN C:\filePath.tsv -SserverName -UuserName -Ppassword -c -t\t -r\n
Which reports a
Unexpected EOF encountered
error.
I can't for the life of me figure out why this is not working. An extra eye would be great.
EDIT:
After review, I think the problem is with -r\n...What is the metacharacter for CRLF?
Encode it in hex:
bcp database.dbo.table IN C:\filePath.tsv -SserverName -UuserName -Ppassword -c -t0x9 -r0xa
You can use multiple characters by encoding each in hex and appending them together. For example, we use the record separator character, carriage return, and newline to separate each row, so we pass 0x1e0d0a as the value of the -r parameter.
I use ASCII Table to do quick lookups for this.
Hi I have search result as,
"abc"
from
perl -lne 'print for /"name":"(.+?)"/g' file > newfile
and
"def"
from
perl -lne 'print for /"title":"(.+?)"/g' file > newfile
I'm trying to get the O/p as
abc:"def",
by combining both one liners. I tried with:
perl -lne 'print for /"name":"(.+?)","title":"(.+?)"/g' *.json > newfile11
but it didn't work
I think I figured this out based on the input from your other question.
I'm assuming you have this as input:
{"card":{"cardName":"10AN10G","portSignalRates":["10AN10G-1-OTU2","10AN10G-1-OTU2E","10AN10G-1-TENGIGE","10AN10G-1-STM64"],"listOfPort":{"10AN10G-1-OTU2":{"portAid":"10AN10G-1-OTU2","signalType":"OTU2","tabNames":["PortDetails"],"requestType":{"PortDetails":"PTP"},"paramDetailsMap":{"PortDetails":[{"type":"dijit.form.TextBox","name":"signalType","title":"Signal Rate","id":"","options":[],"label":"","value":"OTU2","checked":"","enabled":"false","selected":""},{"type":"dijit.form.TextBox","name":"userLabel","title":"Description","id":"","options":[],"label":"","value":"","checked":"","enabled":"true","selected":""},{"type":"dijit.form.Select","name":"Frequency","title":"Transmit Frequency",}}}}}}
or at least a large text file containing those types of lines. You want to parse out the name and title from each line. You can do that with this one line.
Matt#MattPC ~/perl/testing/12
$ perl -ne 'if ( /"name":"([^"]+)","title":"([^"]+?)"/ ) { print $1 . ":\"" . $2 . "\",\n" }' input2.txt
which outputs:
signalType:"Signal Rate",
It works by capturing 2 group in the regex, one for the title and one for the name. The -ne flags go through each line the file and execute the code between the single quotes. $1 and $2 are the group we captured, and they are printed at the end.
Just as a tip, it is much easier to help you if you post your input, expected output, errors you ran into, and code you've tried when asking question.
edit: just wanted to put a disclaimer that it is better to parse JSON with a module, because what if you have escaped " with in a title or name? This regex wouldn't pick it up, but JSON parsers can handle those types of cases for you.
I have a huge list of locations in this form in a text file:
ar,casa de piedra,Casa de Piedra,20,,-49.985133,-68.914673
gr,riziani,RÃziani,18,,39.5286111,20.35
mx,tenextepec,Tenextepec,30,,19.466667,-97.266667
Is there any way with command line to remove everything that isn't between the first and second commas? For example, I want my list to look like this:
casa de piedra
riziani
tenextepec
with Perl
perl -F/,/ -ane 'print $F[1]."\n"' file
Use cut(1):
cut -d, -f2 inputfile
With perl:
perl -pe 's/^.*?,(.*?),.*/$1/' filename
Breakdown of the above code
perl - the command to use the perl programming language.
-pe - flags.
e means "run this as perl code".
p means:
Set $_ to the first line of the file (given by filename)
Run the -e code
Print $_
Repeat from step 1 with the next line of the file
what -p actually does behind the scenes is best explained here.
s/.*?,(.*?),.*/$1/ is a regular expression:
s/pattern/replacement/ looks for pattern in $_ and replaces it with replacement
.*? basically means "anything" (it's more complicated than that but outside the scope of this answer)
, is a comma (nothing special)
() capture whatever is in them and save it in $1
.* is another (slightly different) "anything" (this time it's more like "everything")
$1 is what we captured with ()
so the whole thing basically says to search in $_ for:
anything
a comma
anything (save this bit)
another comma
everything
and replace it with the bit it saved. This effectively saves the stuff between the first and second commas, deletes everything, and then puts what it saved into $_.
filename is the name of your text file
To review, the code goes through your file line by line, applies the regular expression to extract your needed bit, and then prints it out.
If you want the result in a file, use this:
perl -pe 's/^.*?,(.*?),.*/$1/' filename > out.txt
and the result goes into a file named out.txt (that will be placed wherever your terminal is pointed to at the moment.) What this pretty much does is tell the terminal to print the command's result to a file instead of on the screen.
Also, if it isn't crucial to use the command line, you can just import into Excel (it's in CSV format) and work with it graphically.
With awk:
$ awk -F ',' '{ print $2 }' file
When I try to export the text content of a field, and that content have carriage return characters, that chars are output like \N string.
For example:
create table foo ( txt text );
insert into foo ( txt ) values ( 'first line
second line
...
and other lines');
copy foo ( txt ) to '/tmp/foo.txt';
I want to return the following (a):
first line
second line
...
and other lines
But, output is (b):
first line\Nsecond line\N...\Nand other lines
Anybody knows how to get the (a) output?
The \N comes from the fact that one line must correspond to one database row.
This rule is relaxed for the CSV format where multi-line text is possible but then a quote character (by default: ") would enclose the text.
If you want multi-line output and no enclosing character around it, you shouldn't use COPY but SELECT.
Assuming a unix shell as the execution environment of the caller, you could do:
psql -A -t -d dbname -c 'select txt from foo' >/tmp/file.txt
Have you tried: \r\n?
Here's another solution that might work:
E'This is the first part \\n And this is the second'
via https://stackoverflow.com/a/938/1085891
Also, rather than copy the other responses, see here: String literals and escape characters in postgresql
I'm trying to format a postgres dump (pg_dump) to be able to import it using a JDBC connection. pg_dump exports text fields that contain newlines to as just that, text with newlines, so when I later try to import using JDBC I reach the end of line and the statement fails.
What I want to do is take the dump, pass it through sed and escape all newlines, so that I end up with one INSERT statement per line. Problem is that I cannot just remove all newlines, but I can remove all newlines that do no match this );\nINSERT INTO. Is there a simple way to do just this?
Update:
A sample would look like this:
INSERT INTO sometable (123, And here goes some text
with
newlines
in
it', 'some more fields');
and the result I'm looking for is something like this:
INSERT INTO sometable (123, And here goes some text\nwith\nnewlines\nin\nit', 'some more fields');
So that each INSERTstatement is on a single line, with the string's newlines escaped.
Not a sed solution, but might the following work?
cat test_dump.txt | perl -pe "s/[^(\);INSERT INTO)]\n/\\$1\\n/"
You can do it in vim.
vim my_dump.sql
:%s/\();\)\#<!\n\(INSERT\)\#!//c
% .. do for all lines
s .. substitute
\n .. newline (Unix style; you are aware, that Windows has \r\n and Apple \r for line breaks?)
flags:
c .. Confirm each substitution (for testing first)
info on negative lookahead and lookbehind
:help \#!
:help \#<!
sed normally operates on lines, it needs to go out of its way to replace line breaks.
Google for "sed multi-line replace", you'll find stuff like this.