Hot answers tagged

3

If I understood correctly: you want to count how many times there is "P2" anywhere within the 6th field of lines 4,55 and 77 of a few files (named *h3)? You could do this with 1 awk: awk ' ( FNR==4 || FNR==55 || FNR==77 ) { if ( $6 ~ "P2" ) { occurence++ } } END { printf "There was: %d P2 ", occurence printf " among the 6th field on lines ...


2

The key to shell performance is to minimise the number of expensive system calls, in particular fork() and exec(). Don't use grep or sed inside a shell loop. Never use a pipeline with both; in most cases, it can be reduced to just sed or awk. If it gets complicated, use a language that can parse regular expressions, and do loops, like awk or perl. On the ...


2

if [ "1024" == "$((32*32))" ]; then echo "The test worked" else echo "The test failed" fi This ought to work; if your shell does not use $(( )) for arithmetic, the strings will not match. You can also shorthand it with: [ "1024" == "$((32*32))" ] || echo "I can't math!"


2

Sure. Here's one way p2_count=0 for f in *h3; do for ((n=1; n<=77; n++)); do IFS= read -r line if [[ $n == 4|55|77 ]]; then echo "$line" set -f set -- $line set +f if [[ $6 == *P2* ]]; then ((p2_count++)) fi fi done < "$f" done > ...


1

Your first example would work just fine if you called it as software_versions> /bin/sh -c "/opt/xxxxxx/software_versions/test2.sh 1 2 3 4" By using -c, you invoke a subshell to run the command that follows it. Additional arguments will not be passed to the subshell in any predictable fashion. You solve this by sending the full string as an argument.


1

This will sort the files alphanumerically and move the first 25 files into subdirectory dir0, the next 25 into dir1, etc., until all files are moved: n=0; for f in *; do d="dir$((n++ / 25))"; mkdir -p "$d"; mv -- "$f" "$d/$f"; done For those who prefer their commands spread over multiple lines: n=0 for f in * do d="dir$((n++ / 25))" mkdir -p ...


1

Well, you could do it in awk: BEGIN { FS="," } { date=$NF id=$(NF-1) sub(/^ */, "", $1) sub(/"?\[/, "", $1) sub(/\]"?/, "", $(NF-2)) ref = $1 for (i=2; i < NF-1; i++) { ref = ref ":''," $(i) } if (!ref) { ref = "''" } print "update table set cross_refs={" ref ":''} where id='" id "' and effective_date = '" ...


1

temprule will be assigned '$' followed by the value of the variable APPLC_NM. So if APPLC_NM is set to "pizza", temprule will become "$pizza". Note that temprule="\$$APPLC_NM" would do the exact same thing. The brackets are only needed when the variable name is followed by a character that would be valid in a variable name.


1

for shell in for shell in $(sed '1d' /etc/shells); do # or use your own list of shells echo "$shell -" time $shell /path/to/script done



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