New answers tagged

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.


0

I might have overseen something, but this should work as well and is IMHO more readable than set or find. -A just exclude . and .. but include other dotfiles. | head -n 1 is to shorten the ls in case of many files. You can omit it if you expect to have no or few files in the folders you are checking. if [ -z "$(ls -A /path/to/folder | head -n 1)" ] then ...


0

Usually, when a question asks "how do I process a bunch of text files using specific tool(s) in a bash loop?", the answer is, in part, "Don't use a bash loop, use (some or all of) the tool(s) themselves". Sometimes part of the answer is even "Don't use those tools, use this instead". What you want can be done with awk alone, no need for a shell loop. Or ...


0

Or using a bash one-liner: for i in *h3; do sed '4p;55p;77q;d' $i | awk '{print $6}' | grep 'P2'; done | wc -l Or shorter using grep -c: for i in *h3; do sed '4p;55p;77q;d' $i | awk '{print $6}'; done | grep -c 'P2'


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

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

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 ...


0

Use while read input, but due to we want to avoid repeat the echo -en "Press Q to exit \t\t: " statement twice, so we better off to use while true instead (a do while variant): xiaobai:tmp $ cat hello.sh #!/bin/ksh while true; do echo -en "Press Q to exit \t\t: " read input if [[ $input = "q" ]] || [[ $input = "Q" ]] then break ...


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 = '" ...


0

I found the preferred solution, using another channel.... #! /bin/ksh while read a <&3 do echo ------ $a ---------- echo weiter mit return read a done 3< abc


0

Is more -d -1 list good enough? Maybe in a script like: #! /bin/sh echo 'Check each line. Hit spacebar or enter to continue after each line.' echo more -d -1 list


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

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 ...


0

As @Julie Pelletier indicated, this is funny syntax to make a indirect variable, or a nameref. ksh has some specialized syntax to make this work, however. This is a feature of ksh, and might not work in other shells. The more idiomatic way to write the same in ksh would look like this: # Set up the nameref: nameref temprule=APPLC_NM # Assign value to ...


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


0

Nice and easy... sed -n "s/test/est/2p" -n don't print anything s/ substitute "test" with "est" /2 but only the second instance p print the results


1

As stated by steeldriver, if you use GNU sed, you can tell which occurrence should be replaced, e.g.: echo "/test/test/ 12 /test/test" | sed -n -e 's_/._/_2 p' If you can not make use of this feature you can also write: echo "/test/test/ 12 /test/test" | sed -n -r -e 's_(/[a-z]([a-z]+))\1_\1/\2_ p' Biliography: http://www.grymoire.com/Unix/Sed.html ...


0

echo "/test/test/ 12 /test/test" | sed 's/\/test\/t/\/test\//'



Top 50 recent answers are included