Assistance in understanding sqr logic - peoplesoft

Hello, I am new to learning how to develop sqr programs within PeopleSoft. I've been going through some programs we are utilizing and wanted to see if someone could help provide clarification with what the below snippet of code is doing in this While loop.
if $path != ''
let $Archive_File = $path || 'ARCHIVE\' || $filename || $Curr_Date || '.dat'
open $Out_File as 1 for-reading record=450:vary status=#fileread
open $Archive_File as 2 for-writing record=450:vary status=#filewrite
While 1
if #END-FILE
break
else
read 1 into $Current_Line:999
write 2 from $Current_Line
end-if
End-While
close 1
close 2
end-if
I'm trying to understand if the WHILE statement is evaluating the "$Out_File as 1" as the logical expression, or is 1 being evaluated as the value of the variable #END-FILE (As I understand this variable is set to either 0 or 1).

Its a true loop, it will go there until reach a BREAK.
In this case if #END-FILE is true, the loop will break.

In addition to the file name, the open command takes a number as a parameter which it uses as a handler for the file. In your example, two files are being opened.
The WHILE 1 statement doesn't have anything to do with file 1. Here 1 means true, instead of 0 for false. So 1 is always true, creating an endless loop unless something within the loop breaks out if it, which in this case is the BREAK command.
The #END-FILE variable contains FALSE when the file cursor is not at EOF, and a TRUE when the the file cursor is at EOF.
An alternative, which uses less lines of code and is easier to understand might look like this:
if $path != ''
let $Archive_File = $path || 'ARCHIVE\' || $filename || $Curr_Date || '.dat'
open $Out_File as 1 for-reading record=450:vary status=#fileread
open $Archive_File as 2 for-writing record=450:vary status=#filewrite
While Not #END-FILE
read 1 into $Current_Line:999
write 2 from $Current_Line
End-While
close 1
close 2
end-if

Related

Need of awk command explaination

I want to know how the below command is working.
awk '/Conditional jump or move depends on uninitialised value/ {block=1} block {str=str sep $0; sep=RS} /^==.*== $/ {block=0; if (str!~/oracle/ && str!~/OCI/ && str!~/tuxedo1222/ && str!~/vprintf/ && str!~/vfprintf/ && str!~/vtrace/) { if (str!~/^$/){print str}} str=sep=""}' file_name.txt >> CondJump_val.txt
I'd also like to know how to check the texts Oracle, OCI, and so on from the second line only. 
The first step is to write it so it's easier to read
awk '
/Conditional jump or move depends on uninitialised value/ {block=1}
block {
str=str sep $0
sep=RS
}
/^==.*== $/ {
block=0
if (str!~/oracle/ && str!~/OCI/ && str!~/tuxedo1222/ && str!~/vprintf/ && str!~/vfprintf/ && str!~/vtrace/) {
if (str!~/^$/) {
print str
}
}
str=sep=""
}
' file_name.txt >> CondJump_val.txt
It accumulates the lines starting with "Conditional jump ..." ending with "==...== " into a variable str.
If the accumulated string does not match several patterns, the string is printed.
I'd also like to know how to check the texts Oracle, OCI, and so on from the second line only.
What does that mean? I assume you don't want to see the "Conditional jump..." line in the output. If that's the case then use the next command to jump to the next line of input.
/Conditional jump or move depends on uninitialised value/ {
block=1
next
}
perhaps consolidate those regex into a single chain ?
if (str !~ "oracle|OCI|tuxedo1222|v[f]?printf|vtrace") {
print str
}
There are two idiomatic awkisms to understand.
The first can be simplified to this:
$ seq 100 | awk '/^22$/{flag=1}
/^31$/{flag=0}
flag'
22
23
...
30
Why does this work? In awk, flag can be tested even if not yet defined which is what the stand alone flag is doing - the input is only printed if flag is true and flag=1 is only executed when after the regex /^22$/. The condition of flag being true ends with the regex /^31$/ in this simple example.
This is an idiom in awk to executed code between two regex matches on different lines.
In your case, the two regex's are:
/Conditional jump or move depends on uninitialised value/ # start
# in-between, block is true and collect the input into str separated by RS
/^==.*== $/ # end
The other 'awkism' is this:
block {str=str sep $0; sep=RS}
When block is true, collect $0 into str and first time though, RS should not be added in-between the last time. The result is:
str="first lineRSsecond lineRSthird lineRS..."
both depend on awk being able to use a undefined variable without error

How to get the variable's name from a file using source command in UNIX?

I have a file named param1.txt which contains certain variables. I have another file as source1.txt which contains place holders. I want to replace the place holders with the values of the variables that I get from the parameter file.
I have basically hard coded the script where the variable names in the parameter.txt file is known before hand. I want to know a dynamic solution to the problem where the variable names will not be known beforehand. In other words, is there any way to find out the variable names in a file using the source command in UNIX?
Here is my script and the files.
Script:
#!/bin/bash
source /root/parameters/param1.txt
sed "s/{DB_NAME}/$DB_NAME/gI;
s/{PLANT_NAME}/$PLANT_NAME/gI" \
/root/sources/source1.txt >
/root/parameters/Output.txt`
param1.txt:
PLANT_NAME=abc
DB_NAME=gef
source1.txt:
kdashkdhkasdkj {PLANT_NAME}
jhdbjhasdjdhas kashdkahdk asdkhakdshk
hfkahfkajdfk ljsadjalsdj {PLANT_NAME}
{DB_NAME}
I cannot comment since I don't have enough points.
But is it correct that this is what you're looking for:
How to reference a file for variables using Bash?
Your problem statement isn't very clear to me. Perhaps you can simplify your problem and desired state.
Don't understand why you try to source param1.txt.
You can try with this awk :
awk '
NR == FNR {
a[$1] = $2
next
}
{
for ( i = 1 ; i <= NF ; i++ ) {
b = $i
gsub ( "^{|}$" , "" , b )
if ( b in a )
sub ( "{" b "}" , a[b] , $i )
}
} 1' FS='=' param1.txt FS=" " source1.txt

Write into tcl dictionary

I am relatively new to tcl dictionaries and don't see a good documentation on how to initialize an empty dictionary, loop over a log and save data into it. Finally I want to print a table that looks like this:
- Table:
HEAD1
Step 1 Start Time End Time
Step 2 Start Time End Time
**
- Log:
**
HEAD1
Step1
Start Time : 10am
.
.
.
End Time: 11am
Step2
Start Time : 11am
.
.
End time : 12pm
HEAD2
Step3
Start Time : 12pm
.
.
.
End Time: 1pm
Step4
Start Time : 1pm
.
.
End time : 2pm
You really don't have to initialise an empty dictionary in Tcl - you can simply start using it and it will get populated as you go along. As mentioned already, dict man page is the best way to start.
Additionally, I would suggest you check the regexp man page as you can use it nicely to parse your text file.
Not having anything better to do atm, I cobbled together a short sample code that should get you started. Use it as a starting tip, adjust it to your particular log layout and add some defensive measures to prevent errors from unexpected input.
# The following line is not strictly necessary as Tcl does not
# require you to first create an empty dictionary.
# You can simply start using 'dict set' commands below and the first
# one will create a dictionary for you.
# However, declaring something as a dict does add to code clarity.
set tableDict [dict create]
# Depending on your log sanity, you may want to declare some defaults
# so as to avoid errors in case the log file misses one of the expected
# lines (e.g. 'HEADx' is missing).
set headNumber {UNKNOWN}
set stepNumber {UNKNOWN}
set start {UNKNOWN}
set stop {UNKNOWN}
# Now read the file line by line and extract the interesting info.
# If the file indeed contains all of the required lines and exactly
# formatted as in your example, this should work.
# If there are discrepancies, adjust regex accordingly.
set log [open log.txt]
while {[gets $log line] != -1} {
if {[regexp {HEAD([0-9]+)} $line all value]} {
set headNumber $value
}
if {[regexp {Step([0-9]+)} $line all value]} {
set stepNumber $value
}
if {[regexp {Start Time : ([0-9]+(?:am|pm))} $line all value]} {
set start $value
}
# NOTE: I am assuming that your example was typed by hand and all
# inconsistencies stem from there. Otherwise, you have to adjust
# the regular expressions as 'End Time' is written with varying
# capitalization and with inconsistent white spaces around ':'
if {[regexp {End Time : ([0-9]+(?:am|pm))} $line all value]} {
set start $value
# NOTE: This short example relies heavily on the log file
# being formatted exactly as described. Therefore, as soon
# as we find 'End Time' line, we assume that we already have
# everything necessary for the next dictionary entry
dict set tableDict HEAD$headNumber Step$stepNumber StartTime $start
dict set tableDict HEAD$headNumber Step$stepNumber EndTime $stop
}
}
close $log
# You can now get your data from the dictionary and output your table
foreach head [dict keys $tableDict] {
puts $head
foreach step [dict keys [dict get $tableDict $head]] {
set start [dict get $tableDict $head $step StartTime]
set stop [dict get $tableDict $head $step EndTime]
puts "$step $start $stop"
}
}

Textmate "comment" command not working properly for css code

I'm having some problems when I toggle the comments in TextMate for CSS source code.
Using the shortcut CMD + / I activate the "Comment Line/Selection" command from the "source" bundle. The problem is that it inserts a series of // for all kinds of languages. For example, in CSS files it is supposed to insert a /**/ block, but it doesn't. In CSS files I also tried the "Insert Block Comment" command from the source bundle with the weird result that I get the following //.
// ----------------------------------------
instead of my code, deleting the code and inserting that.
I know I am supposed to modify the command from the bundle, but I can't figure out how and what.
This is the code of the "Comment Line/Selection" command from the "Source" Bundle:
#!/usr/bin/env ruby
# by James Edward Gray II <james (at) grayproductions.net>
#
# To override the operation of this commond for your language add a Preferences
# bundle item that defines the following valiables as appropriate for your
# language:
#
# TM_COMMENT_START - the character string that starts comments, e.g. /*
# TM_COMMENT_END - the character string that ends comments (if appropriate),
# e.g. */
# TM_COMMENT_MODE - the type of comment to use - either 'line' or 'block'
#
require "#{ENV["TM_SUPPORT_PATH"]}/lib/escape"
def out(*args)
print( *args.map do |arg|
escaped = e_sn(arg)
$selected ? escaped.gsub("}", "\\}") : escaped.sub("\0", "${0}")
end )
end
# find all available comment variables
var_suffixes = [""]
2.upto(1.0/0.0) do |n|
if ENV.include? "TM_COMMENT_START_#{n}"
var_suffixes << "_#{n}"
else
break
end
end
text = STDIN.read
default = nil # the comment we will insert, if none are removed
# maintain selection
if text == ENV["TM_SELECTED_TEXT"]
$selected = true
print "${0:"
at_exit { print "}" }
else
$selected = false
end
# try a removal for each comment...
var_suffixes.each do |suffix|
# build comment
com = { :start => ENV["TM_COMMENT_START#{suffix}"] || "# ",
:end => ENV["TM_COMMENT_END#{suffix}"] || "",
:mode => ENV["TM_COMMENT_MODE#{suffix}"] ||
(ENV["TM_COMMENT_END#{suffix}"] ? "block" : "line"),
:no_indent => ENV["TM_COMMENT_DISABLE_INDENT#{suffix}"] }
com[:esc_start], com[:esc_end] = [com[:start], com[:end]].map do |str|
str.gsub(/[\\|()\[\].?*+{}^$]/, '\\\\\&').
gsub(/\A\s+|\s+\z/, '(?:\&)?')
end
# save the first one as our insertion default
default = com if default.nil?
# try a removal
case com[:mode]
when "line" # line by line comment
if text !~ /\A[\t ]+\z/ &&
text.send(text.respond_to?(:lines) ? :lines : :to_s).
map { |l| !!(l =~ /\A\s*(#{com[:esc_start]}|$)/) }.uniq == [true]
if $selected
out text.gsub( /^(\s*)#{com[:esc_start]}(.*?)#{com[:esc_end]}(\s*)$/,
'\1\2\3' )
exit
else
r = text.sub( /^(\s*)#{com[:esc_start]}(.*?)#{com[:esc_end]}(\s*)$/,
'\1\2\3' )
i = ENV["TM_LINE_INDEX"].to_i
i = i > text.index(/#{com[:esc_start]}/) ?
[[0, i - com[:start].length].max, r.length].min :
[i, r.length].min
r[i, 0] = "\0"
out r
exit
end
end
when "block" # block comment
regex = /\A(\s*)#{com[:esc_start]}(.*?)#{com[:esc_end]}(\s*)\z/m
if text =~ regex
if $selected
out text.sub(regex, '\1\2\3')
exit
else
r = text.sub(regex, '\1\2\3')
i = ENV["TM_LINE_INDEX"].to_i
i = i > text.index(/#{com[:esc_start]}/) ?
[[0, i - com[:start].length].max, r.length].min :
[i, r.length].min
r[i, 0] = "\0"
out r
exit
end
end
end
end
# none of our removals worked, so preform an insert (minding indent setting)
text[ENV["TM_LINE_INDEX"].to_i, 0] = "\0" unless $selected or text.empty?
case default[:mode]
when "line" # apply comment line by line
if text.empty?
out "#{default[:start]}\0#{default[:end]}"
elsif default[:no_indent]
out text.gsub(/^.*$/, "#{default[:start]}\\&#{default[:end]}")
elsif text =~ /\A([\t ]*)\0([\t ]*)\z/
out text.gsub(/^.*$/, "#{$1}#{default[:start]}#{$2}#{default[:end]}")
else
indent = text.scan(/^[\t \0]*(?=\S)/).
min { |a, b| a.length <=> b.length } || ""
text.send(text.respond_to?(:lines) ? :lines : :to_s).map do |line|
if line =~ /^(#{indent})(.*)$(\n?)/ then
out $1 + default[:start] + $2 + default[:end] + $3
elsif line =~ /^(.*)$(\n?)/ then
out indent + default[:start] + $1 + default[:end] + $2
end
end
end
when "block" # apply comment around selection
if text.empty?
out default[:start]
print "${0}"
out default[:end]
elsif text =~ /\A([\t ]*)\0([\t ]*)\z/
out $1, default[:start]
print "${0}"
out $2, default[:end]
elsif default[:no_indent]
out default[:start], text, default[:end]
else
lines = text.to_a
if lines.empty?
out default[:start], default[:end]
else
lines[-1].sub!(/^(.*)$/, "\\1#{default[:end]}")
out lines.shift.sub(/^([\s\0]*)(.*)$/, "\\1#{default[:start]}\\2")
out(*lines) unless lines.empty?
end
end
end
Ensure you have the "Source" bundle installed. In the latest Textmate 2 Alpha at the time of writing, go to TextMate -> Preferences -> Bundles -> Check "Source" bundle to install. The CMD + / shortcut should now work.
It is small syntax problem if you're using a Ruby higher then 1.8.7. You will find that to_a method has been removed. If you want to fix the problem all you need to do is modify the code found in this file.
In order to fix the problem you need to search for any location that they call to_a and replace it with Array("string").
In my case I did this. This also should work for you:
lines = text.to_a
with
lines = text.lines.to_a
This should be a solution for every thing. Look to the image to see what file I ended up fixing.
I had the same problem and it turns out that I had an SCSS bundle installed that had a preference set to use "//" for comments with a scope selector for source.css as well as source.scss.
I would check to make sure that you don't have the same SCSS bundle and if you do, change the scope selector of the comments preference to be just source.scss.
Cmd/ has been working for years and still is. Well, my copy of TM2 alpha is broken (doesn't work with the / in the numeric pad but, well, it's alpha) but TM 1.5.x works as it should.
You are not supposed to modify anything anywhere. The Comment Line/Selection command is smart enough to put the right kind of comment in "any" kind of file.
Did you mess with language definitions? Is your file recognized as "CSS"? Does it work when removing all or certain plugins/bundles?
-- EDIT --

Function to create the array by reading the file

I am creating scripts which will store the contents of pipe delimited file. Each column is stored in a separate array. I then read the information from the arrays and process it. There are 20 pipe delimited files and I need to write 20 scripts. The processing that will happen in each script after the information is stored in the array is different. The number of columns in each pipe delimited file is different (but in no case it would be more than 9 columns). I need to do this activity of storing the information in the array in the beginning of each script. The way I am doing it at present is given below. I want help from you to understand how can I write a function to do this activity.
cat > example_file.txt <<End-of-message
some text first row|other text first row|some other text first row
some text nth row|other text nth row|some other text nth row
End-of-message
# Note that example_file.txt will available. I have created it inside the script just to let you know the format of the file
OIFS=$IFS
IFS='|'
i=0
while read -r first second third ignore
do
first_arr[$i]=$first
second_arr[$i]=$second
third_arr[$i]=$third
(( i=i+1 ))
done < example_file.txt
IFS=$OIFS
Here is a sort-of minimal change to your script that should get you further...
...
...
while read -r first second third ignore
do
arr0[$i]=$first
arr1[$i]=$second
arr2[$i]=$third
(( i=i+1 ))
done < example_file.txt
IFS=$OIFS
proc0 () {
for j in "$#"; do
echo proc0 : "$j"
done
}
proc1 () {
echo proc1
}
proc2 () {
echo proc2
}
for i in 0 1 2; do
t=arr$i'[#]'
proc$i "${!t}"
done

Resources