Comment by Dennis Williamson on Apply brace expansion in "reverse order"
@muru: Oops. Fixed. Thanks.
View ArticleComment by Dennis Williamson on Why doesn't my Bash script recognize aliases?
@Fmstrat: b () { bash "$@"; } then b -c "echo test | grep test"
View ArticleAnswer by Dennis Williamson for Bash scripting rsync puzzler
Please see BashFAQ/050. This fails because of WordSplitting and because the single quotes inside the variable are literal; not syntactical. You should avoid putting commands in variables when shell...
View ArticleAnswer by Dennis Williamson for How can I convert this bash function to the...
Here is a working function that's similar to your test function. function testvarz switch (count $argv) case 0 echo 'zero' case 1 echo "one $argv[1]" case 2 echo "two $argv[1] $argv[2]" case '*' echo...
View ArticleAnswer by Dennis Williamson for Change terminal prompt Ubuntu
In Bash 4 and later, you can use the special variable PROMPT_DIRTRIM. The escapes \w or \W are available in at least Bash 3.2 and later. These features can be used to control the length of the current...
View ArticleAnswer by Dennis Williamson for Bash: how to get the first number that occurs...
I've put your strings in an array so it can easily be iterated for this demonstration. This uses Bash's builtin regular expression matching. Only a very simple pattern is required. It's recommended to...
View ArticleAnswer by Dennis Williamson for Tmux: equivalent command to screen -d -m -S...
The new-session command with the -d option is similar to the -d -m option in screen. The -s option corresponds to the -S option in screen. Note that -d and -s are options to the new-session command...
View ArticleAnswer by Dennis Williamson for Shell vi mode prints (arg: repetition): How...
When you're in command mode in vi (the the actual editor or the Bash mode), pressing digits inputs an argument (hence "arg") that is usually used to set the number of repetitions to perform the...
View ArticleAnswer by Dennis Williamson for bash_profile stops after file call
From the Bash documentation for aliases: For almost every purpose, shell functions are preferred over aliases. websitecompile () { do_cron && do_config; }
View ArticleAnswer by Dennis Williamson for Redirect stdout over ssh
Try: ssh host 'something > file' Here's a contrived demonstration of a way to handle redirection, pipes and quotes: ssh host date -d yesterday \| awk "'{print $1}'" \> 'file" "with\ spaces.out'...
View ArticleAnswer by Dennis Williamson for How to scroll large datafile while keeping...
Try this (you'll need to install multitail): multitail -du -t "$(head -n 1 filename)" filename or, for headers longer than one line: multitail -wh 2 -l "head -n 2 filename" filename If you want to...
View ArticleAnswer by Dennis Williamson for Increment numeric pattern in file
This prints the whole line by substituting a new value for field 2 and should work regardless of how many fields may appear later in the line. awk -F 'REQ| ' '$1 == "(REF-" {field1 = $1; $1 = ""; num =...
View ArticleAnswer by Dennis Williamson for What is the difference between find . and...
It is sometimes useful to use -print explicitly when you are performing another action so the filename is displayed as that action is performed. find . -print -delete would be similar to rm -rfv *...
View ArticleAnswer by Dennis Williamson for How can I get a count of files in a directory...
Here's another technique along the lines of the one Gilles posted: word_count () { local c=("$@"); echo "${#c[@]}"; } file_count=$(word_count *) which creates an array with 13,923 elements (if that's...
View ArticleAnswer by Dennis Williamson for compare data in two files one has 2 column...
Use join, include sort if your data isn't sorted by the appropriate field. join -1 2 -2 3 -o 2.3 <(sort -k2,2 file1) <(sort -k3,3 file2) If sort isn't needed: join -1 2 -2 3 -o 2.3 file1 file2...
View ArticleAnswer by Dennis Williamson for count lines in a file
Steven D forgot GNU sed: sed -n '$=' file.txt Also, if you want the count without outputting the filename and you're using wc: wc -l < file.txt Just for the heck of it: cat -n file.txt | tail -n 1 |...
View ArticleAnswer by Dennis Williamson for Replace a column and preserve spacing
With GAWK 4, you can preserve the field separators by explicitly splitting a string (or the whole line) and iterating over the result of the split (fields and separators) for output. This example uses...
View ArticleDeduplicating Bash Brace Expansions
Brace expansions can produce multiple instances of file names if there is an overlap in the matches. A simple example: mkdir testdir; cd testdir touch abcd for f in *{b,c}*; do something_to "$f"; done...
View ArticleAnswer by Dennis Williamson for remove extra tilespace from a montage...
Try something like this: montage file1.jpg file2.jpg -geometry +0+0 -background none output.jpg This will make the border between images as small as possible and whatever is there will be transparent....
View ArticleAnswer by Dennis Williamson for How do I print an ASCII character by...
Decimal: chr() { local c for c do printf "\\$((c/64*100+c%64/8*10+c%8))" done } chr 74 Hex: chr $((16#4a)) The function can do sequences: $ chr 74 75 76; echo JKL $
View ArticleAnswer by Dennis Williamson for Is there a basic tutorial for grep, awk and sed?
AWK is particularly well suited for tabular data and has a lower learning curve than some alternatives. AWK: A Tutorial and Introduction An AWK Primer (alt link) RegularExpressions.info sed tutorial...
View ArticleAnswer by Dennis Williamson for Process /etc/passwd file to list all users...
Assuming e.txt is actually /etc/passwd, you should actually use getent passwd instead of parsing the file since user account information may be stored in multiple locations specified in...
View ArticleAnswer by Dennis Williamson for Keeping unique rows based on information from...
If you don't mind that the output is sorted: sort -u -k1,2 file -u - unique -k1,2 - use fields 1 and 2 together as the key
View ArticleHow can I programmatically add entries to Bash history and have timestamps...
On CentOS 6.2 and Bash 4.1.2(1), I have added a file in /etc/profile.d which contains: HISTTIMEFORMAT='%c : ' history -s "# some text as a marker" That works insofar as the the variable gets set and...
View ArticleAnswer by Dennis Williamson for Viewing man pages in vim
Based on this answer this starts vim and exits if there's nothing in the buffer. The disadvantage is that it starts vim so the screen "flashes". It also doesn't set an exit code when a man page isn't...
View ArticleAnswer by Dennis Williamson for Replace a string in a file preceeded by...
This might be better done with awk in which you can use a state-machine style technique: awk '/^reader_1 = newcamd\({/ { section_found = 1} /})/ { section_found = 0 } section_found && /port =...
View Articlegetent shadow shows password hashes for some users
If I execute sudo getent shadow I see password hashes for all the local users who have them. For most of the LDAP accounts I only see * in the password field. However, for a few LDAP users, I see...
View ArticleComment by Dennis Williamson on How to run Dropbox daemon in background?
This answer ammended by Northstrider's comment should be part of the official documentation provided by Dropbox.
View ArticleAnswer by Dennis Williamson for How to determine the path to a sourced tcsh...
In tcsh, $_ at the beginning of the script will contain the location if the file was sourced and $0 contains it if it was run.#!/bin/tcshset sourced=($_)if ("$sourced" != "") then echo "sourced...
View ArticleComment by Dennis Williamson on Obtain a list of files in Terminal in same...
Note that the sort -V (regardless of the -V) example fails if filenames include newlines. This is what I came up with: find . -print0 | sort -Vz | tr '\0' '\n' which includes the dirname (here ./) in...
View ArticleComment by Dennis Williamson on ls command line-wrap prevention
You can use -E with sed to enable enhanced regex and reduce the number of backslashes.
View ArticleComment by Dennis Williamson on Why is the -e option not in the bash command...
Obligatory see also: BashFAQ/105.
View Article--- Article Not Found! ---
*** *** *** RSSing Note: Article is missing! We don't know where we put it!!. *** ***
View Article--- Article Not Found! ---
*** *** *** RSSing Note: Article is missing! We don't know where we put it!!. *** ***
View ArticleComment by Dennis Williamson on Debian/Ubuntu - Is there a man page listing...
gpg: WARNING: no command supplied. Trying to guess what you mean
View ArticleComment by Dennis Williamson on Execute the output of sed as a bash script...
Note that using g in your sed command will do what you want for commands like docker compose some_file or docker ...; docker ... (or with & or && but will not work as intended for a command...
View ArticleComment by Dennis Williamson on I need to have each line of a file run in a...
This still runs the lines sequentially. Gnu parallel is the best answer, but perhaps backgrounding the lines would work instead of taking the OP's question literally (subshells). eval "$line" &
View ArticleComment by Dennis Williamson on How can I restore my default .bashrc file again?
Why cat instead of cp?
View ArticleComment by Dennis Williamson on Does Bourne Shell have a regex validator?
The -P isn't necessary - in fact you should use the flag that corresponds to the flavor of regex you intend to test for - -P is perl, '-E` is extended, for example.
View ArticleComment by Dennis Williamson on How to use argument twice in printf in shell?
/usr/bin/printf and the BSD libc function in MacOS support this. This is fun: /usr/bin/printf '%1$s%0$s' one two (zero is an undefined argument index). It outputs one%1$s%0$stwoone - don't use zero in...
View ArticleComment by Dennis Williamson on How can I rebind shortcut for mysql client?...
See here for an alternative.
View ArticleHow can I diagnose and repair missing drive space?
I have a small ext3 / partition on a 2T drive which appears to have a discrepancy in its free space. How can I determine what is causing it and how it can be fixed. After trying everything I could...
View Article