6

Final answer given more comments below and updated sample input/output in question: I'd sort the data first so the act of filling in the missing values is more efficient and uses less memory than doing a 2-pass approach within awk and the final output is much better organized than the input was for readability: $ cat tst.sh #!/usr/bin/env bash awk ' ...


4

Given that you can't know the number of matches until the END, if you want to print the matches themselves after that, then you will need to save those until the END as well1, ex.: awk ' length($1) >= 5 {matches = matches (matches ? ORS : "") $1; count++} END {print "outfile has " count+0 " matches"; if(count) print ...


3

If you are strictly looking to both assign the result to a variable AND output the list of numbers to standard output in a single line then the following will give you what you want (I changed the name of your maximum function to correspond to what it does, with the variable $maximum corresponding to the stored result itself): function calc_max { awk 'NR==...


3

You can save all matching fields to a temporary array and dump an array in END rule: awk 'length($1) >= 5 {a[$1]; count++; } END {print "outfile has", count, "matches"; for (elem in a) {print elem}}' infile


3

The "best" way is to use the right packaging system for the operating system you are targeting. The entire point of packages is to do exactly what you're describing. Make a deb or rpm or whatever it is you want your users to install. If you insist on not doing that, then the other common way is to offer a tar.gz file (not a zip!) which contains the ...


3

Just another very similar solution: a (gnu)awk multiline script, visiting the input file twice, trying to use vertical symmetries. $ cat awklookup #!/usr/bin/awk -f BEGIN{FS=OFS="\t"; ARGC=3; ARGV[2] = ARGV[1]} ## visit the input file twice ARGIND==1 { tab[$1]=$2 } ARGIND==2 && !$4 { $4=tab[$3] } ARGIND==2 ...


3

If the parent always appears before the child .... awk -F"\t" -v OFS="\t" ' {dad[$1]=$2} !$4{$4=dad[$3]}1 ' file And if not then run through twice... awk -F"\t" -v OFS="\t" ' NR==FNR{dad[$1]=$2;next} !$4{$4=dad[$3]}1 ' file file


2

$ files=(*) $ echo "${files[@]}" dd1.txt dd2.txt dd3.txt dd5.txt


2

Assuming there's no need for recursing down into subdirectories: printf '%s\n' * | paste -d ' ' -s - >other/path/file.txt This would list all the visible filenames from the current directory on a single line in the file other/path/file.txt. I'm choosing to write to a file in some other directory, as writing to file.txt in the current directory would make ...


2

Perhaps something like this, using an array r to store 10 random numbers for each iteration of the i loop: #!/bin/ksh for i in $(seq 1 10) ; do r=() for ((k=1; k<=10; k++)); do r+=($RANDOM) done echo "$i, ${r[@]}" done output would be: $ ./seq.ksh 1, 28324 21310 5112 23165 31527 5779 17343 31716 11594 9769 2, 7205 15089 16436 10276 ...


1

To print the names of the non-hidden files in the current directory, sorted lexically, separated by space characters and terminated with a newline character, with the zsh shell, you can do: print -r - *(N) Or: echo -E - *(N) With the fish shell: set files *; echo -E -- $files With the ksh93 shell: print -r - ~(N)* With the yash shell: (set -o nullglob; ...


Only top voted, non community-wiki answers of a minimum length are eligible