Selective file copy: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(One intermediate revision by one other user not shown)
Line 20:
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Using formatted transput to process similar files to the COBOL sample.
<langsyntaxhighlight lang="algol68">MODE INPUTRECORD = STRUCT( STRING field a, STRING field b, INT field c, CHAR sign of field c, STRING field d );
MODE OUTPUTRECORD = STRUCT( STRING field a, INT field c, STRING field x );
 
Line 66:
close( output file );
close( input file )
FI</langsyntaxhighlight>
 
Input file:
Line 92:
Output and formatting is done using printf().
 
<langsyntaxhighlight lang="awk"># usage: gawk -f sfcopy.awk input.txt
 
BEGIN {
Line 113:
print "# Done."
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 171:
levels. In this example only levels 01, and 05 are used.
 
<syntaxhighlight lang="cobol">
<lang COBOL>
01 ws-input-record.
:INPUT-RECORD:
</langsyntaxhighlight> will become
 
<syntaxhighlight lang="cobol">
<lang COBOL>
01 ws-input-record.
05 field-a pic x(5).
Line 182:
05 field-c pic s9(5).
05 field-d pic x(5).
</langsyntaxhighlight> after the REPLACE preprocessor directive is finished.
 
And finally, the example selective-copy.cob
<syntaxhighlight lang="cobol">
<lang COBOL>
*> Tectonics:
*> cobc -xj selective-copy.cob
Line 344:
.
 
end program selective-copy.</langsyntaxhighlight>
 
The output file has no newlines, so normal '''cat''' type commands are not of
Line 391:
 
=={{header|Fortran}}==
In principle the contents of a file can be arbitrarily complex, with the structure really only comprehensible to the program that writes (and maybe reads) it especially if numbers are written in binary format whereby any bit sequence may appear - just consider the content of a "database" file. Thus, a separate program that selectively reads some data from such a file is faced with an arbitrarily difficult problem. However, if what is written is in text format only, various possible conventions might be followed, and in particular, Fortran offers the NAMELIST protocol, as exemplified below:<langsyntaxhighlight Fortranlang="fortran"> PROGRAM TEST !Define some data aggregates, then write and read them.
TYPE PLACE
INTEGER N
Line 428:
IS.LATLONG = HERE.LATLONG !Piecemeal, as no common structure was defined.
WRITE (6,*) IS !Reveal the result.
END</langsyntaxhighlight>
This produces a file in a standard format. All output starts with a space in column one (in case this output were to be sent to a lineprinter, for which that would be the carriage control character), and begins the block of output with a special line that gives the name of the NAMELIST prefixed with an ampersand, and ends it with a slash. Each line starts with the name of a variable followed by an equals, then comes its value in the same style as would be used in free-form output, using a field large enough for its largest value. Some extra spaces are supplied before the = according to name length to improve readability. The whole line looks much as if it were an assignment statement in Fortran. There is special provision for listing the values of arrays which normally are shown one after another in the standard order. A run of equal values will be shown with a repetition count as in ... ,6*T, ... for six "true" values in a row, alas not using an @ symbol. CHARACTER variables will be shown with all their characters and even if they are all blank, they will not be shown as "" though that is acceptable for input. There is a default record length of 133 (again for lineprinters) and a variable may require multiple lines; CHARACTER variables split across lines include the content of the first character of the next line, but otherwise the line break is after a comma. Structure names alas use % instead of a full stop, but a full stop may be accepted as input. Before F90, such data aggregates were not available, so only ordinary variables could be named.
<pre>
Line 456:
=={{header|FreeBASIC}}==
{{trans|Julia}}
<langsyntaxhighlight lang="freebasic">Open "m:\in.txt" For Input As #1
Open "m:\out.txt" For Output As #2
 
Line 471:
Loop
Close #1, #2
Sleep</langsyntaxhighlight>
{{out}}<pre>in.txt:
A bbbbB0001+d2345
Line 489:
=={{header|Go}}==
JSON is popular these days for structured data and Go has support for this kind of selective copy in the JSON reader. The reader also supports custom conversions on fields. The common idiom for record construction and field initialization is shown.
<langsyntaxhighlight lang="go">package main
 
import (
Line 575:
}
}
}</langsyntaxhighlight>
{{out|o1.json}}
<pre>
Line 598:
(Remember: J's prompt is three spaces, so indented lines here are J sentences, and the following line(s) is/are the result(s) from the sentence.)
 
First, we'll build the file and write it:<langsyntaxhighlight Jlang="j"> N=: {{(}.":1e4+y),'+-'{~3<y}}
R=: {{5{.y#x}}
('A'&R,'B'&R,N,'D'&R)&>1+i.5
Line 607:
AAAAABBBBB0005-DDDDD
(('A'&R,'B'&R,N,'D'&R)&>1+i.5) fwrite 'example'
100</langsyntaxhighlight>
 
Note that we've written 100 characters here -- there's no newlines in this file:
 
<langsyntaxhighlight Jlang="j"> fread'example'
A B 0001+D AA BB 0002+DD AAA BBB 0003+DDD AAAA BBBB 0004-DDDD AAAAABBBBB0005-DDDDD</langsyntaxhighlight>
 
So we'll need to parse the file based on our knowledge of it -- 20 character lines, and four five character wide fields. In the spirit of the task, we'll code this up as if the fields could be arbitrary (different) widths. But, for conciseness, we'll not show the bit where we would add up the field widths to find the record width:<langsyntaxhighlight Jlang="j"> (##\)5 5 5 5
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
_20 ((##\)5 5 5 5)&(</.)\fread 'example'
Line 627:
├─────┼─────┼─────┼─────┤
│AAAAA│BBBBB│0005-│DDDDD│
└─────┴─────┴─────┴─────┘</langsyntaxhighlight>
 
So that's the characters of the records from the file. We can assign these records to variables -- one for each column:<langsyntaxhighlight Jlang="j"> 'A B C D'=: |:_20 ((##\)5 5 5 5)&(</.)\fread 'example'
A NB. first column
A
Line 635:
AAA
AAAA
AAAAA</langsyntaxhighlight>
 
Also, we want numeric values for that third column. Here's building up to that, and giving the numeric column variable a name:<langsyntaxhighlight Jlang="j"> C
0001+
0002+
Line 657:
_4
_5
N=: ,.0"._1|."1 C</langsyntaxhighlight>
 
Next, we'll want N as a five character wide column -- here, it makes sense to use J's " [https://www.jsoftware.com/help/dictionary/dx008.htm 8!:x format]" mechanism -- and we'll want a fill column: <langsyntaxhighlight Jlang="j"> '5.0' 8!:2 N
1
2
Line 666:
-5
5#'X'
XXXXX</langsyntaxhighlight>
 
Finally, we'll glue the pieces back together and write that to a file:<langsyntaxhighlight Jlang="j"> A,"1('5.0' 8!:2,.N),"1(5#'X')
A 1XXXXX
AA 2XXXXX
Line 675:
AAAAA -5XXXXX
(A,"1('5.0' 8!:2,.N),"1(5#'X')) fwrite 'output'
75</langsyntaxhighlight>
 
Again, there's no newlines in the result file, so we would have to know something about its record structure to make sense of it:
 
<syntaxhighlight lang="text"> fread 'output'
A 1XXXXXAA 2XXXXXAAA 3XXXXXAAAA -4XXXXXAAAAA -5XXXXX
_15 ]\fread 'output'
Line 686:
AAA 3XXXXX
AAAA -4XXXXX
AAAAA -5XXXXX</langsyntaxhighlight>
 
=={{header|Java}}==
With a little help from my friens
<langsyntaxhighlight lang="java">import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.File;
Line 742:
return;
}
}</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">out = open("out.txt", "w")
for line in split(strip(read("in.txt", String)), "\n")
fielda, fieldb, fieldd = line[1:5], line[6:10], line[16:20]
Line 751:
print(out, fielda, fieldc, 'X'^length(fieldd), "\n")
end
</langsyntaxhighlight>{{out}}
<pre>
in.txt:
Line 769:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.51
 
import java.io.File
Line 788:
// check it worked
println(out.readText())
}</langsyntaxhighlight>
 
Contents of selective_input.txt:
Line 810:
=={{header|NetRexx}}==
with a little help from a friend
<langsyntaxhighlight lang="netrexx">/* NetRexx */
-- nrc -keepasjava -savelog copys
options replace format comments java crossref symbols nobinary
Line 842:
ex.printStackTrace()
end
end</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Phix}}
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import strformat, strutils
 
const Data = """
Line 866:
stdout.write s
outfile.write s
outfile.close()</langsyntaxhighlight>
 
 
Line 877:
 
=={{header|ooRexx}}==
<langsyntaxhighlight lang="oorexx">/* REXX */
infile ="in.txt"
outfile="out.txt"
Line 911:
expose instream outstream
instream~close
outstream~close</langsyntaxhighlight>
{{out}}
<pre>AA 01XXXXX
Line 921:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">my %F = ( # arbitrary and made up record format
'field a' => { offset => 0, length => 5, type => 'Str' },
'field b' => { offset => 5, length => 5, type => 'Str' },
Line 947:
}
print "\n" . join "\n", @result;
</syntaxhighlight>
</lang>
{{out}}
<pre>field a : A field b : bbbbB field c : 1 field d : + field e : d2345
Line 965:
{{trans|Kotlin}}
Obviously it does not perform any actual file i/o under pwa/pj2s and merely shows the same screen output.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
Line 1,008:
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fout</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,019:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de record (F)
(in F
(until (eof)
Line 1,032:
(line) ) ) )
 
(record "selective.in")</langsyntaxhighlight>
{{out}}
<pre>A 1XXXXX
Line 1,041:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">*process source attributes xref or(!);
copys: Proc Options(Main);
Dcl 1 s1 unal,
Line 1,072:
eoj:
End;
</syntaxhighlight>
</lang>
{{out}}
<pre>AA 01XXXXX
Line 1,083:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="racket">#lang racket
 
(define (read+write)
Line 1,093:
 
(with-output-to-file "selective-output.txt" #:mode 'text #:exists 'replace
(thunk (with-input-from-file "selective-input.txt" read+write)))</langsyntaxhighlight>
 
{{in}}
Line 1,122:
Since the sfc.dat file is binary encoded, I can't include it here easily as text so [https://github.com/thundergnat/rc/blob/master/sfc.dat here is a link to an online copy] instead.
 
<syntaxhighlight lang="raku" perl6line>my @format = ( # arbitrary and made up record format
'field a' => { offset => 0, length => 5, type => 'Str' },
'field b' => { offset => 5, length => 5, type => 'Str' },
Line 1,159:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Input data as read from ./sfc.dat:
Line 1,177:
=={{header|REXX}}==
{{trans|PL/I}}
<langsyntaxhighlight lang="rexx">in='in.txt'
out='out.txt'; 'erase' out
Do While lines(in)>0
Line 1,188:
Call lineout in
Call lineout out
'type' out</langsyntaxhighlight>
{{out}}
Using the test file produced by PL/I.
Line 1,202:
{{libheader|Wren-ioutil}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "io" for File
import "./ioutil" for FileUtil
import "./fmt" for Fmt
 
var process = Fn.new { |line|
Line 1,222:
}
// check it worked
System.print(File.read("selective_output.txt"))</langsyntaxhighlight>
 
{{out}}
Line 1,231:
=={{header|zkl}}==
Taking a clue from ALGOL 68 and Awk.
<langsyntaxhighlight lang="zkl">File("in.txt").pump(File("out.txt","w"),fcn(line){
// 012345678901234567890...
// A bbbbB0001+d2345
Line 1,238:
if(fld4=="-") fld3=-fld3.toInt();
"%-5s%5d%s\n".fmt(fld1,fld3,"X"*fld5.len())
})</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits