Hot answers tagged

199

Use cut with _ as the field delimiter and get desired fields: A="$(cut -d'_' -f2 <<<'one_two_three_four_five')" B="$(cut -d'_' -f4 <<<'one_two_three_four_five')" You can also use echo and pipe instead of Here string: A="$(echo 'one_two_three_four_five' | cut -d'_' -f2)" B="$(echo 'one_two_three_four_five' | cut -d'_' -f4)" Example: $ s=...


178

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: echo "...


143

co-processes are a ksh feature (already in ksh88). zsh has had the feature from the start (early 90s), while it has just only been added to bash in 4.0 (2009). However, the behaviour and interface is significantly different between the 3 shells. The idea is the same, though: it allows to start a job in background and being able to send it input and read ...


107

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


60

At least for bash the man page defines the export syntax as: export [-fn] [name[=word]] ... It also defines a "name" as: name A word consisting only of alphanumeric characters and under‐ scores, and beginning with an alphabetic character or an under‐ score. Also referred to as an identifier. Hence you really cannot define a ...


51

diff --color option was added to GNU diffutils 3.4 (2016-08-08) This is the default diff implementation on most distros, which will soon be getting it. Ubuntu 18.04 has diffutils 3.6 and therefore has it. On 3.5 it looks like this: Tested: diff --color -u \ <(seq 6 | sed 's/$/ a/') \ <(seq 8 | grep -Ev '^(2|3)$' | sed 's/$/ a/') Apparently ...


47

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


46

POSIX and Hyphens: No Guarantee According to the POSIX standard, a function name must be a valid name and a name can consist of: 3.231 Name In the shell command language, a word consisting solely of underscores, digits, and alphabetics from the portable character set. The first character of a name is not a digit. Additionally, an alias must be a ...


45

I believe this is the command you need: date '+%Y%m%d%H%M%S'


41

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


41

Using find is still the preferred way of deleting files. See http://mywiki.wooledge.org/UsingFind for more. One way of doing this is to create a file with the time-stamp in it. e.g touch -t 201311220000 /tmp/timestamp Now delete the files GNUfind (assuming in the current directory) that match the time-stamp e.g: find . -type f ! -newer /tmp/timestamp -...


41

Ask your self what would this do? command1 \ | command2 Can't see the difference. Neither can I, but the shell can. Look closely, there is a space after the \. This stops the newline from being escaped. Therefore use the other form, as it is safer. Shown here with the same error (a space after the | in this case). But it does not cause a bug. ...


36

It is not possible to give a real answer to this question, but the form of a comment is not sufficient. So I think it may be a good idea to collect points to a editable answer... Two years ago, David and Glenn have been layed off by AT&T - I guess both are now over 65. Half a year later, they have been hired by Google and Glenn confirmed me that their ...


36

Wanted to see an awk answer, so here's one: A=$(awk -F_ '{print $2}' <<< 'one_two_three_four_five') B=$(awk -F_ '{print $4}' <<< 'one_two_three_four_five')


31

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


31

Using only POSIX sh constructs, you can use parameter substitution constructs to parse one delimiter at a time. Note that this code assumes that there is the requisite number of fields, otherwise the last field is repeated. string='one_two_three_four_five' remainder="$string" first="${remainder%%_*}"; remainder="${remainder#*_}" second="${remainder%%_*}"; ...


30

With GNU or some BSD finds: find . ! -newermt 2013-11-22 ! -type d -delete Note that it checks the last modification time of the files. On some BSDs, you can use -newerBt in place of -newermt to check the file's inode birth time if available instead.


30

{ ...; } was already accepted in place of do ...; done in for loops in the first version of the Bourne shell in Unix V7 in the late 70s (see the source code) though never documented as far as I know. All of ksh (both the original one derived from the Bourne shell, and the ksh93 rewrite), pdksh¹ (and derivatives), bash (from the start in 1989) and zsh ...


29

curl -Is http://www.yourURL.com | head -1 You can try this command to check any URL.Status code 200 OK means that the request has succeeded and the URL is reachable. You can also test URL availability and get the response code using telnet command telnet www.yourURL.com 80 80 is the port number.


28

For the most part, multiple slashes are equivalent to a single slash. There's one exception: paths beginning with exactly two slashes (//foo/…, as opposed to /foo/… or ///foo/…) have a different meaning on some Unix variants. The meaning is often to access a remote resource with a path like //hostname/dir1/dir2/dir3/file. (Windows does this too, with \\...


26

I agree with vonbrand. Globing is handled by the shell you're running in. So scp user@machine:/path/* will expand /path/* to the files in the LOCAL /path/*, not on the remote machine. However, just for grins I tried: scp "user@machine:/path/[regex here]" . and .... it worked. Try that. Note the quotes. Very necessary. Let us know. Note: the user ...


24

If you have access to GNU diff you can use its --X-group-format options to get that effect without any additional tools: diff --old-group-format=$'\e[0;31m%<\e[0m' \ --new-group-format=$'\e[0;31m%>\e[0m' \ --unchanged-group-format=$'\e[0;32m%=\e[0m' \ file1 file2 That uses ANSI colour escape codes to get red and green, with ANSI-C ...


23

You could start with this: find /your/file -mtime +182 -exec rm {} + Where +182 are the days quantity.


22

NO tldr: github.com/att/ast and github.com/att/uwin On Jan 19-20, 2016 the following (1|2) messages were posted to the ast-users mailing-list: (and I consider the dgk has some patches comment especially encouraging) Wed, Jan 20 2016; From Glenn Fowler: Thanks Lefty for all the work getting this up and running. I know dgk has some patches in the ...


21

Try like this: java -version 2>&1 | grep version | awk '{print $NF}' Looks like the output is going to stderr. Also, grep is not needed: java -version 2>&1 | awk '/version/{print $NF}'


21

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 way: sed '...


19

You are using the Forsyth PD Korn shell, the usual login shell on OpenBSD. The PD Korn shell does not have a source command. The source built-in command is only available in some shells. The command that you want is the . command. Further reading What is the difference between '.' and 'source' in shells?


18

If you are able to use find and if you are working on a "normal Unix filesystem" (that is, as defined in find(1) under -noleaf option description), then the following command can be used: find . -type d -links 2 Each directory has at least 2 names (hard links): . and its name. Its subdirectories, if any, will have a .. pointing to the parent directory, so ...


18

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


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