Tag Info

New answers tagged

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


0

channel.setPty(true) did the trick


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.


0

Simpler answer than the one's I've seen above. if [ "$a" != "0" -o "$b" != "100" ]; then


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


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


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


0

awk $ awk '{print $1*$3/100}' file 13721725 7.20358e+08 261414528 Assuming you don't want the "scientific" notation: $ awk '{printf "%.1f\n", $1*$3/100}' file 13721725.0 720357926.4 261414528.0


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


0

From man echo Normally you could distinguish between a flag and a string that begins with a hyphen by using a (double hyphen). Since no flags are supported with the echo command, a (double hyphen) is treated literally. And in the 2nd example: Note: You must put the message in quotation marks if it contains escape sequences. Otherwise, the shell ...


0

Another possible option is to use awk: $ echo "Name Age Gender Address" | awk -F' ' '{ printf "%s\t%s\t%s\t%s\n", $1, $2, $3, $4 }' produces: Name Age Gender Address Or using printf directly: $ printf "%s\t%s\t%s\t%s\t\n" Name Age Gender Address produces: Name Age Gender Address


0

Use the \n to add newlines to your header: header="-------------------------------------\nName\tAge\tGender\tAddress\n-------------------------------------\n" Then use for instance echo -e $header to display: echo -e $header ------------------------------------- Name Age Gender Address ------------------------------------- If echo -e does not work, ...


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


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


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

If you know, files are all named backup_*, you should include that in the ls command, so you only handle those and not files that accidentally lands in the directory. Then ls is used in a pipe, it lists only 1 file per line, and then only counting, no need to sort, so count_files=$(ls -U backup_* | wc -l) and for part in $(ls -rt backup_*);do rm -rf ...


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


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


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


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


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


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


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


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


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


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

This topic is interest, so I test the benchmark in 3 ways: sed '1d' d.txt > tmp.txt tail -n +2 d.txt > tmp.txt sed -i '1d' d.txt Note that target d.txt is 5.4GB file Get the result : run 1 : sed '1d' d.txt > r1.txt 14s run 2 : tail -n +2 d.txt > r2.txt 20s run 3 : sed -i '1d' d.txt 88s Conclusion : It seems below be the quickest ...


0

One possible way (untested) is the following. Summary: you have a lot of shell scripts, all calling each other. You want to know what the calling relationship between these scripts is. Let's assume that the scripts are all invoked as separate programs, since Gilles tells me that in that case the called script is a child process of the calling script. Then ...


0

Given a script of $ cat the_script ls /bin/ls touch ./xx.xx ls /bin/ls ls ls /bin/ls ls /bin/ls(tabs) Then $ cat the_script | grep ^[[:space:]]*\t*\/ produces /bin/ls /bin/ls /bin/ls /bin/ls and thus will show those calls if they start at the begging of lines (allows for spaces or tabs) You can also identify executable files for the ...


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


0

Since inputName is a variable you can not use repexp constants /.../ (like in f && /<details input="inputName"/). Instead use dynamic regexps through strings: f && $0 ~ "<details input=" inputName where you concatenate the string "..." with variable inputName. (Note: Since you are processing XML, keep in mind, when processing XML ...


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


0

With the conditions: I cannot use any XML parser tool as I don't have permission , read only My xmllint version does not support xpath, and I cannot update it , read only I dont have xmlstarlet and cannot install it I resorted to finding other unconventional solutions. This awk command got me what I needed awk ' /<service.*name=/ { f=1 ; m=0 ; ...


0

Here's one take on the issue with awk: awk ' /<machine.*name=/ { f=1 ; m=0 ; res="" } f { res = res $0 ORS } f && /PATTERN/ { m=1 } /<\/machine>/ { f=0 ; if (m) print res $0 } ' your_XML_file It's implementing a FSA. Flag f controls whether you are in the desired XML block, flag m signal whether in that block your item had been ...


0

If you are using the latest ksh - by which I mean a recent build of ksh93 - you can actually just use it. ksh93 supports compound variable types - which are a little like a C struct - or an XML node-tree. It doesn't natively support XML at the moment - though I believe that it is planned - but it does support json right now. I used some free online ...


0

OK, first and foremost - don't use grep. XML is not a suitable format for regex based parsing. Use an XML parser instead. My favourite XML Parser is actually a perl module called XML::Twig. #!/usr/bin/perl use strict; use warnings; use XML::Twig; my ($keyword, $filename) = @ARGV; XML::Twig->new( 'pretty_print' => 'indented_a', ...


0

Assuming a sample xml file like this: <services> <service name="GETME" min="1" max="10" idleTime="300" backend="ABC"> <handlerContainer className="com.abc.xyz.wqere.abcqwere"> <handler className="com.abc.xyz.qweqweqwe.werwerwerwer"/> </handlerContainer> ...



Top 50 recent answers are included