Tuesday, September 10, 2013

weblog; bash, sed

Bash: creating scripts for cleaning up documentation files

bash script: get just filename from path

http://stackoverflow.com/questions/3362920/bash-script-get-just-filename-from-path
a=/tmp/file.txt
b=$(basename $a)
echo $b

<a href="file.txt" target="_new">file.txt</a>

Used this to extract the filename only from the path. I'm storing files that haven't been processed yet in 'unprocessed'.

#[command line]
find unprocessed -maxdepth 1 -type f -name "[^.]*" | xargs bin/convert_rails_html_to_dev_docs.sh


#[bin/convert_rails_html_to_dev_docs.sh]
for xpathfile in $@
do

xfile=$(basename $xpathfile)
<div class="notes">
  <p>.</p>
</div>

done

xargs: How To Control and Use Command Line Arguments; {} as the argument list marker

http://www.cyberciti.biz/faq/linux-unix-bsd-xargs-construct-argument-lists-utility/
find . -name "*.bak" -print0 | xargs -0 -I {} mv {} ~/old.files

-0 -I {} is especially important; I still need to find out what it means though. -I {} indicates what the replacement symbol is.

escaping newlines in sed replacement string

http://stackoverflow.com/questions/8991275/escaping-newlines-in-sed-replacement-string
[jaypal:~/Temp] echo 'abc' | sed 's/b/\ 
> /'

At first, I tried to enter a ^M () at the point I desired a new line. After reading this SO I decided to try simply putting in a straight-out carriage return and it worked! Like the SO says, you still need to escape the carriage return.

sed 's_.* name="csrf-token" />$_<link href="assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" />\
<script src="assets/application.js?body=1" type="text/javascript"></script>_' $xfile.tmp > work/$xfile

Bash For Loop Examples

http://www.cyberciti.biz/faq/bash-for-loop/
for VARIABLE in file1 file2 file3
do
 command1 on $VARIABLE
 command2
 commandN
done

Or to get all arguments that may have been provided as arguments to the script when it was run (e.g., when using xargs)

for VARIABLE in $@
do
 command1 on $VARIABLE
 command2
 commandN
done

How to read command line arguments in a bash script

http://how-to.wikia.com/wiki/How_to_read_command_line_arguments_in_a_bash_script
command: ./script.bash alpha beta gamma
Variables: $1=='alpha'; $2=='beta'; $3=='gamma' 

using a user defined bash function in xargs [duplicate]

http://stackoverflow.com/questions/11232782/using-a-user-defined-bash-function-in-xargs

I decided to create an actual bash script to funnel xargs to.

find unprocessed -maxdepth 1 -type f -name "[^.]*" | xargs bin/convert_rails_html_to_dev_docs.sh
find work -maxdepth 1 -type f -name "[^.]*" | xargs bin/install_work_files.sh

Recursively list non-Hidden Files

http://ubuntuforums.org/archive/index.php/t-931966.html
find /path -type d -name "[^.]*"

When you need to exclude file entries from your list. This leaves out any .*.swp files that might be hanging around.

find work -maxdepth 1 -type f -name "[^.]*" | xargs bin/install_work_files.sh

Here's another way using ls, although I wasn't able to get it working.

Bash: How list only the files?

http://stackoverflow.com/questions/10574794/bash-how-list-only-the-files
find . -maxdepth 1 -type f

Listing just the file entries (without any directories).

find work -maxdepth 1 -type f

Find all file entries under the work directory.

How do I tell if a file does not exist in bash?

http://stackoverflow.com/questions/638975/how-do-i-tell-if-a-file-does-not-exist-in-bash
if [ ! -f /tmp/foo.txt ]; then
    echo "File not found!"
fi

I used this to determine whether I needed to create a directory or not. My script exited when the directory already existed and I attempted to create it again.

if [ ! -e "unprocessed" ]
then
  mkdir unprocessed
fi

Advanced Bash-Scripting Guide: File test operators

http://tldp.org/LDP/abs/html/fto.html
-e       file exists

Of course, there are many other options listed in this resource besides this one. This is just the one I was looking for.

Passing parameters to a bash function

http://stackoverflow.com/questions/6212219/passing-parameters-to-a-bash-function
function_name () {
   command...
} 

This is how to create a bash function. I was going to use this, but finally gave up and decided to use a shell script instead.

Sed - An Introduction and Tutorial by Bruce Barnett: Using \1 to keep part of the pattern

http://www.grymoire.com/Unix/Sed.html#uh-0
sed 's/\([a-z]*\).*/\1/'

This looks quite comprehesive. As the author says, I fall into the category of those not really knowing how to use sed. I've started my journey :)

Anyhow, sed is a marvelous utility. Unfortunately, most people never learn its real power. The language is very simple, but the documentation is terrible.

http://www.grymoire.com/Unix/Sed.html#uh-0

No comments:

Post a Comment