Answer 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 ArticleAnswer by Dennis Williamson for determining path to sourced shell script
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/tcsh set sourced=($_) if ("$sourced" != "") then echo "sourced...
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 Article