Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Wednesday, April 24, 2013

Some notes on find and replace using Vim, rename, and perl in command line...


Using rename in bash script

As mentioned in the previous post [1], I need to replace dots to other tokens in the filenames of a bunch of eps figures. The following script was used to do the work [2]:

#!/bin/sh
for f in $(find . -name "*.eps" -type f)
do
    echo "found: "$f
    # option -n is useful to preview the renamed results:
    rename -v 's/(\d+)\.(\d+)\.(\d+)/$1-$2-$3/' $f
done

In this case, I learned how to keep some parts of the old string and to replace other parts. The key concept was to use parentheses to group the parts we want to keep and then use $n to indicate the nth group in substitution expression. Use ``91.1.8.eps'' as the example:
  • \d stands for digits
  • \d+ means at least ONE digit
  • (\d+) hold the parts which are respectively 91, 1, and 8 in this example
  • $1 corresponds to the first group which is 91
  • $2 corresponds to the second group which is 1
  • $3 corresponds to the third group which is 8
Therefore, the dots between the digits will be replaced by dashes.

Substitution in Vim

After renaming all the eps files, I had another more complicated problem. All the corresponding filename strings resided in the tex files also had to be changed! At the beginning I edited one of the tex file in Vim and played with the substitution command in it. The final command I used was [3][4]:

:%s/\(\d*\)\.\(\d*\)\.\(\d*\)/\1-\2-\3/gc

Note that there are some minor differences when writing the expressions. Some of the modifications, e.g. the escaping backslash, were due to the difference between BRE and ERE [5].

Find and Replace in multiple files


Although I could do the find and replace works in Vim, it was not a good idea when there were maybe hundreds of such files. To write a bash script was my first thought and with information found on the internet [6][7][8] I got a usable script as the follows:

#!/bin/sh
for f in $(find . -name "*.tex" -type f)
do
    echo "found: "$f
    perl -p -i -e 's/(\d+)\.(\d+)\.(\d+)/$1-$2-$3/' $f
done

---
[1] XeTeX -- using dots in eps filenames would cause errors
[2] batch renaming with the rename command

[3] Vim Regular Expressions 101: Grouping and Backreferences
[4] Search/Replace in Vim

[5] Basic Regular Expressions and Extended Regular Expressions
[6] Eeasy Search and Replace in Multiple Files on Linux Command Line
[7] bash find directories
[8] Find file or directory in whole directory structure



Monday, January 02, 2012

Screencast of particular window in Ubuntu using ffmpeg

When you want to record the screen output, there have been some software good to use [1]. However, I found none of them can properly record the zone in which a C++ program produced video stream combining webcam input and OpenGL objects.

After some trials, I found an useful solution [2] and made some modifications. It is a shell script that uses ffmpeg to record the video content of target window.

#!/bin/sh

echo -e "Usage: bash $0 outputfile\nClick on the window to be recorded."

size=$(xwininfo -frame | awk '/geometry/ {print $2}')
offset=$(echo $size | awk -F + '{print $2","$3}')

ffmpeg -f alsa -ac 2 -ab 192k -i pulse -f x11grab -s $size -r 30 -i :0.0+$offset -acodec pcm_s16le -vcodec libx264 -vpre lossless_ultrafast -threads 0 $1
---
[1] Such as recordMyDesktop and xvidcap
[2] Run FFMPEG with x11grab

Thursday, November 04, 2010

Floating calculation in bash (with formatted output)

What I wanted was to get a list of incremental floating numbers, like:
0.0 0.2 0.4 0.6 ... 2.0

It is possible to accomplish this mission in bash, with the help of bc. My own script is listed below:
#!/bin/bash
for i in {0..20..2}
do
#f=$(printf %3.1f `echo "scale=1; $i/10" | bc`) # old version
printf -v f %3.1f `echo "scale=1; $i/10" | bc` # new version
echo $f
done

The bc command was used because bash cannot handle floating calculation directly.

Thursday, September 02, 2010

Converting filenames from UPPERCASE to lowercase

I searched and found the following thread:
http://blog.mc-thias.org/?title=rename-files-from-upper-case-filename-to&more=1&c=1&tb=1&pb=1

Based on the post given by Jadu Saikia, I got the bash script of my own version as follows:
ls * | sed -e p -e 's/.*/\L&/g' |xargs -n 2 mv

One new thing I've learned in this example is the ``\L'' part, which can be found in sed's FAQ.