Inserting new line when joining files in VBScript - vbscript

I have two text files that I want to combine ..I am using the below code to do that ..the issue is at the start of the second file this code is inserting some weird characters like spaces..Is there a way to insert a new line instead of using writeline.
Set txsOutput = FSO.CreateTextFile(strOutputPath)
Set txsInput = FSO.OpenTextFile(strInputPath,1)
txsOutput.Writeline txsInput.ReadAll
Thanks

.ReadAll() reads the trailing EOL(s) of the file. .Writeline will add a further EOL. Use .Write instead to get an exact copy of the first input file as the head of the output file.
If the "weird characters like spaces" are - unwanted - parts of the first file, you'll have to use string ops (Instr, Left, Replace, ...) or a RegExp to clean the data.
If they come from the second file (assuming you used .ReadAll for that too), you should check the encoding of that file and/or clean the data using the methods above.

Related

How to handle improper Data Coming from CSV in Informatica

I have source file (CSV) and need to load into target (Oracle). But I got an error
FR_3065 ROW[4],Filed [Student_rollnumber]:Invalid Number:[.].The row will be skipped
CSV TABL
Student_rollnumber,Studnet_Name,Marks,Subjects
10,'Revanth',70,"Maths",
11,'Satish',85,Science
12,'Anil',75,"Java
",
13,'Surya',90,"C++",
14,'Ramana',85,"python",
15,'Sudheer'70,"Informatica
",
16,'Prakash',85,"SQL"
I found that in line number 4 the qouts and comma(",) are in the next line how to concat that both ("Java",) And make it single column(Subject)
MatchQuotesPastEndOfLine mentioned by Koushik should work.
Alternatively you may use sed with below pattern to replace newline+" with simply just a " - as a result removing the new line at the end of quoted string.
sed ':a;N;$!ba;s/\n"/"/g'
Feel free to test this gist.
This however will remove just the ending new line and will not help if it's anywhere in the middle. As said, the MatchQuotesPastEndOfLine mentioned by Koushik is the best possible solution.
Above has been based on this question.

batch to concatenate %var_1% and "VarText"

Goal: In a batch file, append or concatenate the text in Var_1 with the string JpgListand add the extension .txt to the output file
In the batch this works
Dir *.Jpg > JpgList.txt
In the batch, I have already retrieved a Variable %Var_1%
Problem is: concatenating the Var with the literal string JpgList and appending .txt as the output filename.
Here are code examples of what I have tried along with pseudo code that fails, (Some produce a .txt file with no name, some produce nothing and some produce a file with the Var_1 name, but no content in file).
dir *.jpg Var_1 + jpgList.txtdir > %label_% >>".txt"dir > %%label_%% >>".txt"
I have tried every which way but upside down to put this together, and
appreciate any tips or syntax for making the code work.
Because you haven't provided your batch file, we have no idea of the possible content of %Var_1% or %label_% so answers currently need to speculate.
The following should work, as long as the content of %Var_1% does not include problematic/disallowed characters or a non-existent/inaccessble location.
Dir /B/A-D-L *.jpg>"%Var_1%JpgList.txt"

How to delete the first row in the .csv file

I need to delete the first row of the CSV file.
In my script I received CSV file as argument and it's first row contains some unwanted data.
So I need to remove the first row only before processing it.
I'd do something like this:
Set fso = CreateObject("Scripting.FileSystemObject")
Set csv = fso.OpenTextFile(WScript.Arguments(...))
If Not csv.AtEndOfStream Then csv.SkipLine 'skip first row
Do Until csv.AtEndOfStream
line = csv.ReadLine
'process read line
Loop
csv.Close
Of course you could just as well do a ReadLine without processing the returned value for skipping the first line, as Ekkehard.Horner suggested, but IMO SkipLine better reflects the semantics.
Do a .ReadLine immediately after opening the input file. This will restrict a later line loop -
Do Until .AtEndOfStream
.ReadLine
... process ...
Loop
or a .ReadAll() to the data from the second line onwards only.

sed/awk/bash to replace text between two strings with external file contents

I'm looking to write a script/command, that'll take inputFile1, look for a specific start and end string in it, and replace all the text in between them
with the full contents of inputFile2.
Ideally, but not mandatory, this should work without a need to escape special characters, so I can put the strings in variables that get called by the script (that way I could easily reuse it multiple times).
As an example, I have file inputYes.txt with contents:
DummyOne
Start
That
What
Yes
End
DummyTwo
And inputNo.txt with contents:
This
Why
Not
And I want the script to search inputYes.txt for the strings Start and End, and replace all the text in between with the contents of inputNo.txt, and write to the file.
So after running it, inputYes.txt should read
DummyOne
Start
This
Why
Not
End
DummyTwo
sed '/end_string/rinputFile2
/start_string/,/end_string/d' inputFile1

Concatenating strings fails when read from certain files

I have a web application that is deployed to a server. I am trying to create a script that amoing other things reads the current version of the web application from a properties file that is deployed along with the application.
The file looks like this:
//other content
version=[version number]
build=[buildnumber]
//other content
I want to create a variable that looks like this: version-buildnumber
Here is my script for it:
VERSION_FILE=myfile
VERSION_LINE="$(grep "version=" $VERSION_FILE)"
VERSION=${VERSION_LINE#$"version="}
BUILDNUMBER_LINE=$(grep "build=" $VERSION_FILE)
BUILDNUMBER=${BUILDNUMBER_LINE#$"build="}
THEVERSION=${VERSION}-${BUILDNUMBER}
The strange thing is that this works in some cases but not in others.
The problem I get is when I am trying to concatenate the strings (i.e. the last line above). In some cases it works perfectly, but in others characters from one string replace the characters from the other instead of being placed afterwards.
It does not work in these cases:
When I read from the deployed file
If I copy the deployed file to another location and read from there
It does work in these cases:
If I write a file from scratch and read from that one.
If I create my own file and then copy the content from the deployed file into my created file.
I find this very strange. Is there someone out there recognizing this?
It is likely that your files have carriage returns in them. You can fix that by running dos2unix on the file.
You may also be able to do it on the fly on the strings you're retrieving.
Here are a couple of ways:
Do it with sed instead of grep:
VERSION_LINE="$(sed -n "/version=/{s///;s/\r//g;p}" $VERSION_FILE)"
and you won't need the Bash parameter expansion to strip the "version=".
OR
Do the grep as you have it now and do a second parameter expansion to strip the carriage return.
VERSION=${VERSION_LINE#$"version="}
VERSION=${VERSION//$'\r'}
By the way, I recommend habitually using lowercase or mixed case variable names in order to reduce the chance of name collisions.
Given this foo.txt:
//other content
version=[version number]
build=[buildnumber]
//other content
you can extract a version-build string more easily with awk:
awk -F'=' '$1 == "version" { version = $2}; $1 == "build" { build = $2}; END { print version"-"build}' foo.txt
I don't know why your script doesn't work. Can you provide an example of erroneous output?
From this sentence:
In some cases it works perfectly, but in others characters from one string replace the characters from the other instead of being placed afterwards.
I can't understand what's actually going on (I'm not a native English speaker so it's probably my fault).
Cheers,
Giacomo

Resources