Tag Info

Hot answers tagged

18

{ cat sample1.txt; tail -n +4 sample2.txt; tail -n +4 sample3.txt; } > out.txt


14

It's showing the contents of the special variable $@, in Bash. It contains all the command line arguments, and this command is taking all the arguments from the second one on and storing them in a variable, variable. Example Here's an exampe script. #!/bin/bash echo ${@:2} variable=${@:3} echo $variable Example run: ./ex.bash 1 2 3 4 5 2 3 4 5 3 4 5 ...


13

Here is a simple script to demonstrates the different between $* and $@: #!/bin/bash function test_param() { echo "Receive $# parameters"; echo Using '$*'; echo for param in $*; do echo "==>$param<=="; done; echo echo Using '"$*"'; for param in "$*"; do echo "==>$param<=="; done; ...


12

The reason file.txt is empty after that command is the order in which the shell does things. The first thing that happens with that line is the redirection. The file "file.txt" is opened and truncated to 0 bytes. After that the sed command runs, but at the point the file is already empty. There are a few options, most involve writing to a temporary file. ...


11

Short answer: use "$@" (note the double quotes). The other forms are very rarely useful. "$@" is a rather strange syntax. It is replaced by all the positional parameters, as separate fields. If there are no positional parameters ($# is 0), then "$@" expands to nothing (not an empty string, but a list with 0 elements), if there is one positional parameter ...


10

That's a ksh feature also found in bash and recent versions of zsh. In ksh and bash, you can access several elements of an array by using the ${array[@]:first:length} syntax, which expands to up to length (or all if length is omitted) elements of the array array (in the list of elements of the array sorted numerically on the indexes), starting with the ...


8

The only reference I can find to the special parameter $_ in POSIX is in the rationale section on Shell Variables. This excerpt implies that it was used by a variety of shells, but not in a standard way by all and was omitted intentionally: _ (Underscore.) While underscore is historical practice, its overloaded usage in the KornShell is confusing, ...


8

The weird string "@(#)" is actually used by the ancient SCCS version control system. Specifically, the what command would look through a file (binary or text) and find ASCII-Nul-terminated strings that started with "@(#)", and print that string out. That allowed you to embed printable ASCII version numbers in ".o" files and ultimately executables, so you ...


7

You should use it this way: ls -l | awk -v d="$Date" '$0 ~ d {print $NF}' Explanation is here But may be it's better to use find in your script. find . -maxdepth 1 -type f -daystart -ctime -`date "+%d"` If you have classic awk instead of gawk: find * -prune -type f -cmin -`date '+%d %H %M' | awk '{print ($1*24+$2)*60+$3}'`


7

ex can be used for true in-place editing that does not involve a temp file ex -c ':1d' -c ':wq' file.txt


7

The LINE parameter isn't quoted so wordsplitting happens upon the expansion of $LINE in echo $LINE and by the time awk receives any input, you have 7 words(as seen by the shell) all separated by a single space. You want echo to output it as one word(again, as seen by the shell) so the whitespace in your line isn't mangled before awk can process it. That is ...


7

sed '4,${/^---/d;/^Date/d;}' sample1.txt sample2.txt sample3.txt > out.txt


7

The grep with awk is redundant: hasys -display | awk '/Shutdown/ { printf "%s ", $1 }'


7

With awk: awk '{ print $(NF-1) }' NF is the number of fields -- all that happens here is that one is subtracted from the total field length to get the penultimate field. With perl: perl -lane 'print $F[-2]' An array containing the fields is created as @F (that's what -a does), and we get the value of the second last field (with index -2). Using sed ...


6

All you need is join join -t\, <(sort Output1.csv) <(sort Output2.csv) -or- join -t "," <(sort Output1.csv) <(sort Output2.csv) or awk awk -F, 'FNR==NR{a[$1]=$2;next}{ print $0 "," a[$1]}' Output2.csv Output1.csv


6

ksh 93 has a nameref command that let's you create "aliases" to variables: var1EMI=a var2EMI=b for v in var1 var2; do nameref var=${v}EMI echo "${v}EMI is $var" done var1EMI is a var2EMI is b For ksh88, you may be forced to use eval: replace nameref var=${v}EMI with eval var=\$${v}EMI


6

With GNU sort and GNU du (which it appears you have, since you state you are using du's -h option): du -sh -- * | sort -rh # Files and directories, or du -sh -- */ | sort -rh # Directories only The output looks something like this: 22G foo/ 21G bar/ 5.4G baz/ 2.1G qux/ 1021M wibble/ 4.0K wobble/


6

awk -Fy -v OFS=y '{gsub(",","u",$2); print}' file


6

Try "./a.sh" when trying to execute it. It needs to know where the file is at. The './' tells it to look in the current directory.


5

If you were on Linux, you could simply use: date -d @1381260225 Or you could use awk: echo "1381260225" | awk '{print strftime("%c",$1)}' Or Python: python -c "import datetime; print datetime.datetime.fromtimestamp(1381865497)" Or Perl: perl -e 'print(scalar(localtime(1381865497)), "\n";' However none of these solutions are available on a stock ...


5

You're checking if the file exists by using -f, but that's not what you want to do. The file exists in the tar file, but -f has no way of reading inside tar archives by itself. For example, if your file is at "foo/bar" inside the tar file, it will look for "foo/bar" relative your current directory, which doesn't exist. The better way is to just check the ...


5

If you're sure that end_time is always greater than start_time, you can use Perl like so: export start_time=06:07:25 export end_time=07:02:08 perl -e ' ($h1,$m1,$s1) = split /:/,$ENV{start_time}; ($h2,$m2,$s2) = split /:/,$ENV{end_time}; $delta_h = $h2 - $h1; $delta_m = $m2 - $m1; if( $delta_m < 0 ) { $delta_m = $delta_h-- * 60 + $m2 ...


5

There are 2 redirects there. The last bit, 2>&1 is actually merging STDERR in with STDOUT. This looks to me like someone set this up to log output to the doit.log file but then wanted to disable it. Chaining redirects in this manner, basically negates the earlier ones, so that only the output, if there is any, will get directed to the last redirected ...


5

You could put the read and your case in a while loop and break out of it when the condition is satisfied: while : ; do echo "yes or no?" read ans case $ans in [yY]*) echo "yes" break ;; [nN]*) echo "no" break ;; *) echo "yes or no only" ;; esac done The while : ; do ... ...


4

If you want to redirect the STDOUT and STDERR to /dev/null for xset -q, you should do: xset -q > /dev/null 2>&1 || { echo "The Display Server is BROKEN. Aborting."; exit 1; } The redirection of using &> only works within bash or zsh. Therefore you should use 2>&1 to let redirection work in all Bourne-like shells.


4

Make sure the command history is enabled (off by default for non-interactive shells) and use that: #!/bin/bash set -o history function trapper () { printf "culprit: " history 1 } trap trapper ERR # your errors go here


4

You can also use grep: grep "91_987986787688899.*successful" file Unless your file has a very strange format, you could also probably just do this: awk '/91_987986787688899.*successful/' file There is no need for print $0, it is implied.


4

Also take a look at sponge from moreutils. sponge soaks in data from standard input until standard input's writing end closes before writing to a file. It is used like so: sed '1d' file.txt | sponge file.txt


4

An alternative very lightweight option is just to 'tail' everything but the first line (this can be an easy way to remove file headers generally): # -n +2 : start at line 2 of the file. tail -n +2 file.txt > file.stdout Following @Evan Teitelman, you can: tail -n +2 file.txt | sponge file.txt To avoid a temporary file. Another option might be: ...


4

You can do it in two steps: newdir="$(find . -type d | grep "${4:0:4}.*${4:4:2}.*${4:6:2}")" if [ -z "$newdir" ];then # error out here else cd "$newdir" fi ...



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