Find specific column range and line using VBSCRIPT - vbscript

I have a txt file that I only need to read the first line, but only want values from column 64-70 from line 1. How do I do this in vbscript? I have looked at several ways to do this but can't get exactly what I'm looking for. Please help.

For the reading characters from the line, use Mid(source_str, 64, 6). -- 6 is the length from character 64 to 70.
As for reading the first line from the text file, you will need to set up a loop to read each line until the end of the file, parse them into an array of strings, then process only the 1st.
Or, since you only need the first line, just run fsoStream.ReadLine() once.
So in your case:
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("filename.txt")
'This only reads the first line of the file.
'To read any others, we would need a loop.
line = file.ReadLine()
thisStr = Mid(line,64,6)

Related

How can I convert this VBS script to Bash?

I have this code in VBS that I need to rebuild in Bash. This is a snippet of a larger script that I have to convert. Can someone please help me with this?
I have read a lot of documentation on Bash in the last two days that I have been working on the larger script but am still not sure how to go about doing this. My coworker/trainer essentially handed me this and told me to use Google to learn Bash in order to do this. I feel it's also important to note that I am not familar with VBScript either.
Function Build_Param_Array()
Set objFSO = WScript.CreateObject("Scripting.Filesystemobject")
Set ReadFile = objFSO.OpenTextFile("Param_List.txt")
While Not ReadFile.AtEndOfStream
thisline = ReadFile.ReadLine
Pcount = Pcount + 1
ReDim preserve arrParam(Pcount)
If Not Right(thisline,1) = "|" Then thisline = thisline & "|"
arrParam(Pcount) = thisline
Wend
End Function
Not a vbs guru, but winging it for a general guess, it looks like it reads Param_List.txt, makes sure each line ends with a pipe character, then pushes it onto an array (arrParam).
typeset -a arrParam # declare an array
while read l # read each line from stdin into l
do arrParam+=("${l%|}|") # push the line onto the array, assuring a |
done < Param_List.txt # put the file on the loop's stdin
$l is the line read. ${l%|} is the line read, with any pipe as the last character removed; thus "${l%|}|" explicitly removes a pipe if there was one, then adds one whether or not one was removed.

How to remove invisible new line characters in Perl

I am writing a shell script in perl that takes values from two databases and compares them. When the script is finished it outputs a report that is supposed to be formatted this way:
Table Name Date value from Database 1 value from Database 2 Difference
The output is printed into a report file, but even when it is output to the command console it looks like this:
tablename 2017-06-20 7629628
7629628
0
Here's my code that makes the string then outputs it to the file:
$outputstring="$tablelist[0] $DATErowcount0[$output_iteration] $rowcount0[$output_iteration] $TDrowcount0[$output_iteration] $count_dif\n";
print FILE $outputstring;
There seems to be a newline character hidden after $rowcount0[$output_iteration] and before $count_dif. What do I need to do to fix this/print it all in one line?
To fill the arrays with values, values are read from files created by SQL commands.
Here's some of the code:
`$num_from_TDfile=substr $r2, 16;
$date_from_TDfile = substr $r2, 0, 12;
$TDrowcount0[$TDnum_rows0]=$num_from_TDfile;
$DATETDrowcount0[$TDnum_rows0]=$date_from_TDfile;
$TDnum_rows0=$TDnum_rows0+1;`
Adding the chomp to each of the strings taken from the files as suggested by tadman fixed the output so that it was all on one line rather than three lines as in the question's example.

writing in specific position in a text file using ruby

I want to insert data in specific positions in a text file, like in line 1 starting from position 10, how can I do it using ruby?
I also want to pass fake data into this file using fakker gem or in any other way possible. Like sending phone number, name, SSN etc.
Here's a sample script that takes two arguments and writes a modified copy of the first file's contents to the second file:
require 'faker'
input = File.open(ARGV[0], 'r')
lines = input.readlines
lines[0].gsub!(/^(.{0,10})/, '\1' + Faker::Base.numerify('###').to_s)
output = File.open(ARGV[1], 'w')
lines.each do |line|
output.write(line)
end
If you have an input file that looks like:
12345678901234567890
^^^ fake data
the output might look like:
12345678909451234567890
^^^ fake data
Since I opened the output file after reading the input file, you can pass the same file name as both the first and the second argument. That isn't exactly inserting the string into the file, but it's as close as you'll get.
The key line is:
lines[0].gsub!(/^(.{0,10})/, '\1' + Faker::Base.numerify('###').to_s)
It takes the fist line and substitutes in place a random 3-digit integer. If there are fewer than 10 characters in the first line, it'll append the random data to the end of the line. If you'd prefer to not substitute, you might want to remove the beginning of the range in the regex:
/^(.{10})/
Or maybe do something else if lines[0].length < 10.

Inserting new line when joining files in 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.

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.

Resources