Can anyone explain this? Is there a workaraound? Informix 4GL RDS 7.50.UC3 on RHEL/CentOS
$ fglpc test
$ fglgo test
1 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
2 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
3 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
4 +++┌────────────────────┐+++++++++X+++++++++X
5 +++│exit? │+++++++++X+++++++++X
6 +++│ │+++++++++X+++++++++X
7 +++│ │+++++++++X+++++++++X
8 +++│ │+++++++++X+++++++++X
9 +++└────────────────────┘+++++++++X+++++++++X
10 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
11 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
…
$ export CLIENT_LOCALE=en_US.utf8
$ fglgo test
1 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
2 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
3 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
4 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
5 ++++exit? X+++++++++X+++++++++X
6 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
7 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
8 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
9 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
10 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
11 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
…
Here's the code
$ cat test.4gl
main
define
i smallint,
b char(75),
c char
let b = "++++X+++++++++X+++++++++X+++++++++X+++++++++X"
for i = 1 to 24
display i using "#### ", b clipped at i,1
end for
open window test_win at 5,10 with 4 rows, 20 columns
attributes(border)
prompt "exit?" for char c
close window test_win
end main
Bummer, it's my old custom termcap inherited from 4GL 4.0 on OpenServer 5
$ export TERMCAP=/opt/informix/etc/termcap
$ fglgo test
1 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
2 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
3 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
4 ++++--------------------++++++++++X+++++++++X
5 +++|exit? |+++++++++X+++++++++X
6 +++| |+++++++++X+++++++++X
7 +++| |+++++++++X+++++++++X
8 +++| |+++++++++X+++++++++X
9 ++++--------------------++++++++++X+++++++++X
10 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
11 ++++X+++++++++X+++++++++X+++++++++X+++++++++X
Oh dear.
$ echo $TERM
ansi
$ cat evil-termcap
# Minimal ANSI for Telix. 8 Oct 96
#
lx|ansi-t|Telix in ANSI-BBS mode:\
:al=\E[L:am:bs:cd=\E[J:ce=\E[K:cl=\E[2J\E[H:cm=\E[%i%d;%dH:co#80:\
:dc=\E[P:dl=\E[M:do=\E[B:bt=\E[Z:ei=:ho=\E[H:ic=\E[#:im=:li#25:\
:nd=\E[C:pt:so=\E[7m:se=\E[m:us=\E[4m:ue=\E[m:up=\E[A:\
:kb=^h:ku=\E[A:kd=\E[B:kl=\E[D:kr=\E[C:eo:\
:k0=\E[M:k1=\E[N:k2=\E[O:k3=\E[P:k4=\E[Q:k5=\E[R:\
:k6=\E[S:k7=\E[T:k8=\E[U:k9=\E[V:\
:kA=\E[W:kB=\E[X:\
:mb=\E[5m:me=\E[m:\
:gb=\332\300\277\331\304\263:
# Ansi Terminal - In Black AND White
# based on ansi-t,
# adds scrolling (sf,sr)
# and INFORMIX capabilities (GS,GE,GV,GH,etc)
#
lb|ansib|Ansi standard crt:\
:sf=\E[S:sr=\E[T:\
:GS=\E[12m:GE=\E[10m:GV=\63:GH=D:\
:GC=E:GL=\64:GR=C:RT=^J:G1=?:G2=Z:G3=#:G4=Y:GU=A:GD=B:\
:CW=\E[M:NU=\E[N:RF=\E[O:RC=\E[P:\
:WL=\E[S:WR=\E[T:CL=\E[U:CR=\E[V:\
:HM=\E[H:EN=\E[F:PU=\E[I:PD=\E[G:\
:ZA=\E[0%?%p2%t;7%;%?%p3%t;5%;%?%p4%t;4%;m:\
:tc=ansi-t:
#
# Ansi Terminal - In Colour
# based on ansib (b/w) - adds INFORMIX colour handling proglet
li|ansi|Ansi standard crt:\
:ZA=\E[0m\E[%?%p1%{0}%=%t%{7}%e%p1%{1}%=%t%{3}%e%p1%{2}%=%t%{5}%e%p1%{3}%=%t%{1}%e%p1%{4}%=%t%{6}%e%p1%{5}%=%t%{2}%e%p1%{6}%=%t%{4}%e%p1%{7}%=%t%{0}%;%?%p2%t%Pa%?%ga%{6}%=%t34m%e%ga%{7}%=%t30m%e%ga%{1}%=%t1m\E[33m%e%ga%{2}%=%t30m%e%ga%{5}%=%t37m%{1}%Pa%e37m%;%ga%{40}%+\E[%2d%e40m%{30}%+\E[%2d%;m%?%p3%t\E[5m%;%?%p4%t\E[4m%;:\
:tc=ansib:
Related
So I have a C program that outputs many numbers. I have to check them all. The problem is, each time I run my program, I need to change seeds. In order to do that, I've been doing it manually and was trying to make a shell script to work around this.
I've tried using sed but couldn't manage to do it.
I'm trying to get the output like this:
a=$(./algorithm < input.txt)
b=$(./algorithm2 < input.txt)
c=$(./algorithm3 < input.txt)
The output of each algorithm program is something like this:
12 13 315
1 2 3 4 5 6 7 8 10 2 8 9 1 0 0 2 3 4 5
So the variable a has all this output, and what I need is
variable a to contain this whole string
and variable a1 to contain only the third number, in this case, 315.
Another example:
2 3 712
1 23 15 12 31 23 3 2 5 6 6 1 2 3 5 51 2 3 21
echo $b should give this output:
2 3 712
1 23 15 12 31 23 3 2 5 6 6 1 2 3 5 51 2 3 21
and echo $b1 should give this output:
712
Thanks!
Not exactly what you are asking, but one way to do this would be to store the results of your algorithm in arrays, and then dereference the item of interest. You'd write something like:
a=( $(./algorithm < input.txt) )
b=( $(./algorithm2 < input.txt) )
c=( $(./algorithm3 < input.txt) )
Notice the extra () that encloses the statements. Now, a, b and c are arrays, and you can access the item of interest like ${a[0]} or $a[1].
For your particular case, since you want the 3rd element, that would have index = 2, hence:
a1=${a[2]}
b1=${b[2]}
c1=${c[2]}
Since you are using the Bash shell (see your tags), you can use Bash arrays to easily access the individual fields in your output strings. For example like so:
#!/bin/bash
# Your lines to gather the output:
# a=$(./algorithm < input.txt)
# b=$(./algorithm2 < input.txt)
# c=$(./algorithm3 < input.txt)
# Just to use your example output strings:
a="$(printf "12 13 315 \n 1 2 3 4 5 6 7 8 10 2 8 9 1 0 0 2 3 4 5")"
b="$(printf "2 3 712 \n 1 23 15 12 31 23 3 2 5 6 6 1 2 3 5 51 2 3 21")"
# Put the output in arrays.
a_array=($a)
b_array=($b)
# You can access the array elements individually.
# The array index starts from 0.
# (The names a1 and b1 for the third elements were your choice.)
a1="${a_array[2]}"
b1="${b_array[2]}"
# Print output strings.
# (The newlines in $a and $b are gobbled by echo, since they are not quoted.)
echo "Output a:" $a
echo "Output b:" $b
# Print third elements.
echo "3rd from a: $a1"
echo "3rd from b: $b1"
This script outputs
Output a: 12 13 315 1 2 3 4 5 6 7 8 10 2 8 9 1 0 0 2 3 4 5
Output b: 2 3 712 1 23 15 12 31 23 3 2 5 6 6 1 2 3 5 51 2 3 21
3rd from a: 315
3rd from b: 712
Explanation:
The trick here is that array constants (literals) in Bash have the form
(<space_separated_list_of_elements>)
for example
(1 2 3 4 a b c nearly_any_string 99)
Any variable that gets such an array assigned, automatically becomes an array variable. In the script above, this is what happens in a_array=($a): Bash expands the $a to the <space_separated_list_of_elements> and reads the whole expression again interpreting it as an array constant.
Individual elements in such arrays can be referenced like variables by using expressions of the form
<array_name>[<idx>]
like a variable name. Therein, <array_name>is the name of the array and <idx> is an integer that references the individual element. For arrays that are represented by array constants, the index counts elements continuously starting from zero. Therefore, in the script, ${a_array[2]} expands to the third element in the array a_array. If the array would have less elements, a_array[2] would be considered unset.
You can output all elements in the array a_array, the corresponding index array, and the number of elements in the array respectively by
echo "${a_array[#]}"
echo "${!a_array[#]}"
echo "${#a_array[#]}"
These commands can be used to track down the fate of the newline: Given the script above, it is still in $a, as can be seen by (watch the quotes)
echo "$a"
which yields
12 13 315
1 2 3 4 5 6 7 8 10 2 8 9 1 0 0 2 3 4 5
But the newline did not make it into the array a_array. This is because Bash considers it as part of the whitespace that separates the third and the fourth element in the array assignment. The same applies if there are no extra spaces around the newline, like here:
12 13 315\n1 2 3 4 5 6 7 8 10 2 8 9 1 0 0 2 3 4 5
I actually assume that the output of your C program comes in this form.
This will store the full string in a[0] and the individual fields in a[1-N]:
$ tmp=$(printf '12 13 315\n1 2 3 4 5 6 7 8 10 2 8 9 1 0 0 2 3 4 5\n')
$ a=( $(printf '_ %s\n' "$tmp") )
$ a[0]="$tmp"
$ echo "${a[0]}"
12 13 315
1 2 3 4 5 6 7 8 10 2 8 9 1 0 0 2 3 4 5
$ echo "${a[3]}"
315
Obviously replace $(printf '12 13 315\n1 2 3 4 5 6 7 8 10 2 8 9 1 0 0 2 3 4 5\n') with $(./algorithm < input.txt) in your real code.
I am trying to apply simple awk script to the dataset file.
The file has 150 columns, I need cols between 20 to 30 only.
below is the script I used to get the records with field between 20 to 30.
code
BEGIN{}
{
for(f=20;f<=30;f++){
print $f;
}
}
I dont know why I get each value of the 10 fields in next line.
That is,
sample dataset
1 2 3 4 5 6 7
2 2 3 4 5 6 7
3 3 3 4 5 6 7
4 4 4 4 5 6 7
5 5 5 5 5 6 7
6 6 6 6 6 6 7
7 7 7 7 7 7 7
I get output as
1
2
3
4
5
6
7
2
2
3
4
5
6
7
...so on
Below is another way of doing the same
awk -v f=20 -v t=30 '{for(i=f;i<=t;i++) \
printf("%s%s",$i,(i==t)?"\n":OFS)}' file
Notes
f and t are the starting and the ending columns respectively.
We used the ternary operator to control the field separator between the needed columns.
Edit
If you need columns 20 thru 30 and the last column, below would suffice :
awk -v f=20 -v t=30 '{for(i=f;i<=t;i++) \
printf("%s%s",$i,(i==t)?OFS""$NF"\n":OFS)}' file
Solution
BEGIN{FS=" ";}
{
for(f=20;f<=30;f++){
printf("%s ",$f);
}print "";
}
My colleague has given me a file, in which half of the lines are made of 8 columns of info and the other half are made of the 9th column of info. They are always next to each other, e.g.
1 2 3 4 5 6 7 8
1.1
2 3 4 5 6 7 8 9
1.2
...
a b c d e f g h
abcd
I know how to paste every two lines as one and print them out in Python. But I was wondering if it's possible to do that even more conveniently in BASH?
Thanks guys!
You could use sed or awk, as other answers have mentioned. Those answers are all good.
You could also do this easily in pure shell.
$ while read line1; do read line2; echo "$line1 $line2"; done < input.txt
1 2 3 4 5 6 7 8 1.1
2 3 4 5 6 7 8 9 1.2
Note that whitespace is not preserved.
There's another tool available on most unix-like systems called paste:
$ paste - - < input.txt
1 2 3 4 5 6 7 8 1.1
2 3 4 5 6 7 8 9 1.2
In this case, there's a big space in the first line because paste separates columns using tabs, by default, and the trailing space in the first line of input.txt caused the separating tab to be offset to the next column. You can read paste's man page for options to control this.
Another awk
awk '{f=$0;getline;print f,$0}' file
1 2 3 4 5 6 7 8 1.1
2 3 4 5 6 7 8 9 1.2
And just for the fun of it a gnu awk
awk -v RS="[0-9][.][0-9]" '{$1=$1;print $0,RT}' file
1 2 3 4 5 6 7 8 1.1
2 3 4 5 6 7 8 9 1.2
Here is set the Record Separator to the value in line two.
Then the RT will have the actual separator stored.
try:
awk '{printf "%s%s",$0,(NR%2?FS:RS)}' file
or:
awk 'NR%2{printf "%s ",$0;next}7' file
test:
kent$ echo "1 2 3 4 5 6 7 8
1.1
2 3 4 5 6 7 8 9
1.2"|awk '{printf "%s%s",$0,(NR%2?FS:RS)}'
1 2 3 4 5 6 7 8 1.1
2 3 4 5 6 7 8 9 1.2
kent$ echo "1 2 3 4 5 6 7 8
1.1
2 3 4 5 6 7 8 9
1.2"|awk 'NR%2{printf "%s ",$0;next}7'
1 2 3 4 5 6 7 8 1.1
2 3 4 5 6 7 8 9 1.2
You can sed:
sed 'N;s/\n/ /' file
or awk:
awk 'NF==1{print $0}{printf "%s ",$0}' file
One file has 3 columns and looks like this:
1 249250621 225280621
2 243199373 238207373
3 198022430 194797140
4 191154276 187661676
5 180915260 177695260
6 171115067 167395067
7 159138663 155353663
8 146364022 142888922
9 141213431 120143431
10 135534747 131314747
11 135006516 131129516
12 133851895 130481895
13 115169878 95589878
14 107349540 88289540
15 102531392 81694769
16 90354753 78884753
17 81195210 77795210
18 78077248 74657248
19 59128983 55808983
20 63025520 59505520
21 48129895 35108702
22 51304566 34894566
X 155270560 151100560
Y 59373566 25653566
My other file looks like:
5677533
4506000
2272564
2753699
4015846
2163243
3812595
2885199
8064159
3522086
2006115
1490517
1072244
1423429
3009679
2705191
1479591
800436
929876
648000
347993
972862
7812943
9660863
When I try
paste file1.txt file2.txt > file3.txt
I get:
1 567753321 225280621
2 450600073 238207373
3 227256430 194797140
4 275369976 187661676
5 401584660 177695260
6 216324367 167395067
7 381259563 155353663
8 288519922 142888922
9 806415931 120143431
10 352208647 131314747
11 200611516 131129516
12 149051795 130481895
13 107224478 95589878
14 142342940 88289540
15 300967992 81694769
16 27051913 78884753
17 14795910 77795210
18 80043648 74657248
19 92987683 55808983
20 64800020 59505520
21 34799395 35108702
22 97286266 34894566
X 781294360 151100560
Y 96608636 25653566
The data from file2.txt overwrites the 2nd column in file 1. I want the data in file2.txt to be appended to the end in a new column, separated by a tab in file1.txt into the new file3.txt. But it does not seem to be working. Thoughts? Thanks.
Edit: For file2, I can create it with the same first column index 1-22, x, y. But then when I try to join, it doesn't work either. This is my output on join:
1 249250621 225280621
5677533
2 243199373 238207373
4506000
3 198022430 194797140
2272564
4 191154276 187661676
2753699
5 180915260 177695260
4015846
6 171115067 167395067
2163243
7 159138663 155353663
3812595
8 146364022 142888922
2885199
9 141213431 120143431
8064159
10 135534747 131314747
3522086
11 135006516 131129516
2006115
12 133851895 130481895
1490517
13 115169878 95589878
1072244
14 107349540 88289540
1423429
15 102531392 81694769
3009679
16 90354753 78884753
2705191
17 81195210 77795210
1479591
18 78077248 74657248
800436
19 59128983 55808983
929876
20 63025520 59505520
648000
21 48129895 35108702
347993
22 51304566 34894566
972862
X 155270560 151100560
7812943
Y 59373566 25653566 9660863
Try running dos2unix on both file1.txt and file2.txt first.
==> dos2unix file*.txt
file1.txt: done.
file2.txt: done.
==> paste file1.txt file2.txt > file3.txt
==> cat file3.txt
1 249250621 225280621 5677533
2 243199373 238207373 4506000
3 198022430 194797140 2272564
4 191154276 187661676 2753699
5 180915260 177695260 4015846
6 171115067 167395067 2163243
7 159138663 155353663 3812595
8 146364022 142888922 2885199
9 141213431 120143431 8064159
10 135534747 131314747 3522086
11 135006516 131129516 2006115
12 133851895 130481895 1490517
13 115169878 95589878 1072244
14 107349540 88289540 1423429
15 102531392 81694769 3009679
16 90354753 78884753 2705191
17 81195210 77795210 1479591
18 78077248 74657248 800436
19 59128983 55808983 929876
20 63025520 59505520 648000
21 48129895 35108702 347993
22 51304566 34894566 972862
X 155270560 151100560 7812943
Y 59373566 25653566 9660863
I realize gnuplot 4.6 does not have a specific data point addressing capability and I would have to use a script to extract a given value and store it as a variable (for example, to extract a value in the 7th column in the 4th row from the last, I simplistically could use 'tail -4 data.out | head -1 | awk '{print $7}'). How could I store/assign that value as a gnuplot variable and then display it on a chart with the set label 1 sprintf("a = %3.4f",a) at x,y command?
Gnuplot understands backtics the same as your shell. So, to get at the particular value in your datafile:
a=`tail -4 data.dat | head -1 | awk '{print $7}'`
set label 1 sprintf("a=%3.4f",a) at x,y
When reading something like "can't be done with gnuplot", it "hurts" and encourages me
to nevertheless find a gnuplot-only solution which consequently is platform-independent. (See the above comments about Linux and Windows (in)compatibility issues.)
Although, sometimes it's getting cumbersome and less efficient, but sometime it is not much longer than the solution with external tools.
Basically, you can use stats and every for this (check help stats and help every), however, if you need the mth row from the last, you first need to know how many lines you have in total. That's why you have to run stats twice. Check the following example:
Data: SO11560130.dat
1 21 3 4 5 6 78
2 25 3 4 5 6 72
3 23 3 4 5 6 73
4 29 3 4 5 6 74
5 27 3 4 5 6 77
6 28 3 4 5 6 75
7 22 3 4 5 6 73
8 24 3 4 5 6 78
9 26 3 4 5 6 78
Script: (works with gnuplot 4.6.0, March 2012)
### extract specific value from given row/column
reset
FILE = "SO11560130.dat"
M = 4 # row from last
COL = 7 # column no.
stats FILE u 0 nooutput # get total number of lines in variable STATS_records
n = STATS_records - M # index 0-based
stats FILE u (x0=$1,y0=$2,a=column(COL)) every ::n::n nooutput # get the value and coordinates
set label 1 sprintf("a=%3.4f",a) at x0,y0 offset 0,1
plot FILE u 1:2 w lp pt 7 lc rgb "red" notitle
### end of script
Result: