Tag Info

Hot answers tagged

6

The standard (POSIX sh and utilities) canonical legible ways would be: string comparison: if [ "$a" != 0 ] || [ "$b" != 100 ]; then... decimal integer comparison (0100 is 100, whether leading blanks are ignored or not depend on the implementation though). if [ "$a" -ne 0 ] || [ "$b" -ne 100 ]; then... integer comparison (0x64, 0144 are 100 (POSIX mode ...


6

Using sed: variable1="$(< inputfile sed -n '3s/ *//p')" variable1="$([...])": runs the command [...] in a subshell and assigns its output to the variable $variable < inputfile: redirects the content of inputfile to sed's stdin -n: suppresses output sed command breakdown: 3: asserts to perform the following command only on the 3rd line of input ...


5

First read the desired line into a variable (line 3 in the example): var=$(sed -n '3p' file1.txt) The sed command prints (p) the 3rd line of the file. The strip the leading spaces using parameter substitution: echo "${var#"${var%%[![:space:]]*}"}" The inner substitution means to remove everyting except the leading spaces. The outer substitution remove ...


5

There is no generic solution as there are countless ways a script could be using to call other scripts. You can do a grep which may work for your scripts but not in general. Which scripts does this call? $(find / -executable -name "*.sh" -print0 | shuf -z -n 1) If you're able to actually run these scripts you could trace them in two ways. set -x will ...


3

Asusming all scripts live in the same directory, they don't have tabs or newlines in their names, and you have the list of the "interesting" ones in a file scripts.txt, one per line, and also assuming your shell can do <(...) process substitutions: #! /bin/sh while read -r s; do fgrep -o -w -f <(fgrep -v -w "$s" scripts.txt) "$s" /dev/null | \ ...


3

awk '$0 == $0+0{ summ += $0 next} { if(summ) format="%06d\n%s\n" else format="%s%s\n" printf format, summ, $0 summ=""} END { if(summ) printf "%06d\n", summ}' ...


3

#! /bin/zsh - for dir (/home/DABA_BACKUP/*) rm -f $dir/*(Nom[61,-1]) For the zsh-ignorant ;-): for var (list) cmd: short version of the for var in list; do cmd; done loop (reminiscent of perl syntax). $dir: zsh variables don't need quoted like they do in other shells as zsh has explicit split and glob operators so doesn't do implicit split+glob upon ...


3

tail -1 file1.txt > variable1 writes to the file variable1. Use command substitution (bash.info 3.5.4, POSIX sh) instead: variable1="$(tail -1 file1.txt)" Btw my version of tail from GNU in cygwin doesn't have the -1 option. Instead, I use sed: # EREGEX: Replace all whitespace at beginning of line # NOTE: BSD sed uses a different flag to enable ...


2

To obtain a list of the oldest entries to delete (thus keeping the 60 latest entries): ls -t | tail -n +61 Note that the principle problem of your approach remains to be addressed here as well: how to handle files with newlines, in case it matters; otherwise you can just use (replacing your quite complex program): cd /home/DABA_BACKUP || exit 1 ls -t | ...


2

If you use ANSI strings: header=$' ------------------------------------- Name\tAge\tGender\tAddress ------------------------------------- ' you have the correct translation of any ANSI escaped characters already in your variable. (Note: the newline characters could also be defined as \n, but it wouldn't add to legibility in your case, so I'd keep your ...


2

With ksh/zsh/bash: IFS=' ' read -r variable < <(tail -n 1 file) read strips leading and trailing space characters if space is found in IFS (which it is by default along with tab and newline). You can also do: while IFS=' ' read -r variable <&3; do something with "$variable" done 3< file To process the file line by line (though that's ...


2

Thanks Jeff Schaller for correcting my syntax error. I found a solution to my problem, this is working in Solaris 10. script: #!/bin/ksh HEX=30 DEC=`printf "%d\n" 0x${HEX}` ##Converted Hex to decimal echo "$DEC" OCT=$(printf '%o' $DEC) ##Converted decimal to octal echo "$OCT" ASCI=$(printf \\$OCT) ##Finally converted OCTAL to ASCII. echo "$ASCI" ...


2

You're mistakenly escaping the $ twice, leading printf to see printf \$( ... instead of (what I assume you want) of substituting the inside printf results. To that end, you could simplify that whole statement to: ASC=$(printf '%03o' $DEC)


2

You probably want something like: #!/bin/ksh typeset -Au codes read -rA codes?"Enter codes: " IFS=, sql="select * from table where request_no in (${codes[*]})" echo "$sql" Running that looks like: Enter codes: foo bar baz select * from table where request_no in (FOO,BAR,BAZ) Proper quoting of values left as an exercise.


2

set -- * filenames="$*" As long as you haven't modified your environment's value for $IFS, the above is all you need to get all of the names of not-dot files in the current directory into a single string as divided by spaces and sorted by locale in any POSIX shell. If you have modified $IFS, then whatever its first character is will sub for the single ...


2

With a new enough ksh version (not sure exactly how new is new enough), you should just be able to do: files_to_delete=( * ) ... mdelete ${files_to_delete[@]} The first line creates files_to_delete as an array whose elements are the files in your current directory (returned by the * glob), and the second line expands files_to_delete as a space-separated ...


2

Use zless utility from that same gzip suite, it combines functionality of gzip -d and less into one


2

This is what pipes were made for: gzip -dc | less


1

Perl has a nifty "paragraph mode" (-00) where records ("lines") are separated by a blank line instead of a single \n character. Using this, you can very easily extract the relevant records. So, first make sure that there is a blank line before each <block>, and then use paragraph mode to find what you're searching for: $ sed ...


1

Try this with ksh: while read A B C; do tmp=$(($A*$C/100)) echo $tmp done < foo.txt > out1.txt Output to out1.txt: 13721725 720357926 261414528 See: Performing arithmetic on variables in the Korn shell


1

Use arithmetic evaluation and parentheses to avoid ambiguity: if (( ($a != 0) || ($b != 100) )); then # some commands Or, if you want to deal with strings as well, use [[.


1

[ "$((a||b^100))" -eq 1 ] && some commands A shell's math expansion will handle the boolean && AND || OR and ! NOT conditions by evaluating the expression to either 1 for true or 0 for false. It will handle the bitwise & AND | OR and ^ XOR operators as well, but obviously those won't necessarily get you a 0 or 1, though a bitwise ...


1

Based on the append to the main post - you seem to be looking for a formatted file that can be opened int Excel. It is more advisable then to convert the entire file to an HTML table, with your particular lines been converted to bold. If it is a simple CSV ( with no commas used purely as separators and not occuring within the columns itself) then you can ...


1

rm60()( IFS=/; set -f; set $( set +f; \ls -1drt ./*) while shift && [ $# -gt 60 ] do [ -d "${1%?.}" ] || rm "./${1%?.}" || exit done ) This will work for you. It will delete the oldest files in the current directory up to a count of 60. It will do this by parsing ls robustly and it ...


1

No, for one thing it will break on filenames containing newlines. It is also more complex than necessary and has all the dangers of parsing ls. A better version would be (using GNU tools): #!/bin/ksh for dir in /home/DABA_BACKUP/* do ## Get the file names and sort them by their ## modification time files=( "$dir"/* ); ## Are there more ...


1

It should be enough to modify your ~/.bash_profile so it reads: if [ -f /bin/ksh93 ] then renice -n 4 $$ exec -l /bin/ksh93 fi The renice -n 4 $$ will set the nice value of the current shell ($$) to four, causing subsequent commands launched by that shell to inherit the same niceness value. I have not tested in a tmux session, but it works as ...


1

In Perl: $ perl -lne 'if(/_sg/){print "$n\n$s" if defined($n); $n=$_; $s=0;} else{$s+=$_}END{print "$n\n$s"}' file Lillypaul_sg 614409 Ammy_sg 3 ramaswamy_sg 36 tommy_sg 137480 If you want the numbers to be 0-padded if they're less than 6 digits (as in your original question): $ perl -lne 'if(/_sg/){printf "%s\n%0.6d\n",$n,$s if ...


1

It looks like you're trying to redirect the output of home/dir/file.txt | awk '{print $2}' to the while loop; first I guess that the correct path should be /home/dir/file.txt (however this is just an assumption); second /home/dir/file.txt | awk '{print $2}' doesn't redirect the content of /home/dir/file.txt to awk, while < /home/dir/file.txt awk '{print ...


1

you should try awk '{print $2}' home/dir/file.txt | while read num do if [ "$num" = "0" ]; then echo "Number is equal to zero" else echo "number is not equal to 0" fi done for a mixed awk/bash solution. As other have pointed out, awk redirection occur later.


1

I am not sure awk will replace a variable by it's value in f && /<details input="inputName"/ you might wish to replace by f && /<details input="/ && index($2,inputName) > 0 edit: final awk part should be: /<machine.*name=/ { f=1 ; m=0 ; res="" } f { res = res $0 ORS } f && /<details ...



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