There are few limitations with Windows search, e.g.
-You cannot preview the search results.
-The regular expression in Windows is not unified with other regular expression formats. The Windows search for phase or word do not recognize the syntax like
\(.*\)BEGIN_DECLS
Then, let's use grep to do the work. If you do not have cygwin, install it!
Now, I want to search for the places in .h files where it contains the text "typedef" at the start of the line and may follow with some other strings but must have "_mbstate_t".
$find ../ -iname \*.h -exec grep -B 3 -i "^typedef\(.*\)_mbstate_t" '{}' \; -print
Basically, find command needs the path and the file name you need to search.
Here, I tell the find to search the one directory upward for any files which have .h" or .H at the end of files. -iname option is for case-insensitive.
Remarkably, in Cygwin, you need to have the escape for * like \*.h but you don't need to do this on native *nix. The examples shown here are only tested under Cygwin.
Next, -exec option is to follow with a command where it takes the arguments from find and execute it. We want to grep any lines starting with "typedef" (^typedef) and follow with some string (.*) and _mbstate_t in any files which passed from find command.
"^typedef\(.*\)_mbstate_t" is normal regular expression for this job.
^typedef is to have typedef at the start of the line.
\(.*\)_mbstate_t is to match _mbstate_t which contains any strings or spaces before the search string. The backslash is the escape character to group (.*)
(For more information about regular expression, google regular expression)
-i is for case-insensitive.
-B 3 is to display 3 lines before the matching line.
If we want to search for both .cpp and .h files, we can group the search within (..) as well.
$find ../ \( -iname \*.h -o -iname \*.cpp \)
However, if you often need to repeat this search, you may put it into the shell script instead.
#!/bin/bash
#
# File: fcpp.sh - Simplify find & grep for source & header file
#
if [ $# -lt 2 ]
then
echo ""
echo "Usage: $0 [-i] search_path search_what"
echo "-i for non case-sensitive, default is case-sensitive"
exit 0
fi
case "$1" in
-i)
shift
find $1 \( -name \*.cpp -o -name \*.h \) -exec grep -i $2 "{}" \; -print
;;
*)
find $1 \( -name \*.cpp -o -name \*.h \) -exec grep $2 "{}" \; -print
;;
esac
exit 0
Beyond the example here, in my opinion find command is one of the most often used commands, therefore please don't forget to have a quick look at the find man page.
See also:
- http://content.hccfl.edu/pollock/Unix/FindCmd.htm
---------------
"The process of preparing programs for a digital computer is especially attractive, not only because it can be economically and scientifically rewarding, but also because it can be an aesthetic experience much like composing poetry or music." Donald E. Knuth
Showing posts with label Linux/Unix command. Show all posts
Showing posts with label Linux/Unix command. Show all posts
Wednesday, April 1, 2009
Tuesday, February 24, 2009
BASH 4.0 is out.
It has been announced on slashdot today. Visit:
- BASH main page
- What's new can be found here.
Other useful links for BASH user (from my bookmark):
- Advanced Bash-Scripting Guide from Mendel Cooper. The tutorial covers the detail from ground up. In the appendix, the author also introduced the "sed" and "awk" usages.
- BASH String Manipulations on Linuxgazette. It focuses on string handling with BASH.
- Bash Shell and Beyond (Stack and Queue, Array, Sort, XML parser expat).
---------------
"Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest." Isaac Asimov
- BASH main page
- What's new can be found here.
Other useful links for BASH user (from my bookmark):
- Advanced Bash-Scripting Guide from Mendel Cooper. The tutorial covers the detail from ground up. In the appendix, the author also introduced the "sed" and "awk" usages.
- BASH String Manipulations on Linuxgazette. It focuses on string handling with BASH.
- Bash Shell and Beyond (Stack and Queue, Array, Sort, XML parser expat).
---------------
"Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest." Isaac Asimov
Thursday, January 15, 2009
A story of package updating...#1
Today is not a busy day. The work has been done soon enough. I have plenty of time left ... after finishing several newspapers, I decided to continue upgrading my old Linux PC at work. It is an AMD Athlon 1.0 GHz with 512MB RAM. I took it over a few years ago before the IT guys would put it in the cellar. It has proved to be a good companion over time. Its only one defect is the on-board clock is unreliable. My clock tick is shorter than the normal clock. That meant, after setting it correctly with date, after one hour, it will run faster than the reference system for around 20 minutes. As long as I don't need to synchronize my work to the network, it is still acceptable (Although this is really annoy) I ever tried to synchronize it using NTP server but it did not help, so I gave it up.
After left it untouched for more than a year, I decided to update the system.
I started off from connecting my Linux from my Windows via XDMCP from cygwin with
$XWin -query linux-pc -screen 0 1280x1024
where 0 from screen option is the screen number and 1280x1024 is the geometry I want it to be. Or use the following command:
$XWin -query linux-pc :2
:2 is to tell the X server of the display number, the default is 0. You need to tell the X server of which display you have if you have multiple X accesses to the X server. The X server will assign a unique display number for each connection. Nevertheless, to enable network establishment between X client and server, you need to allow for X forwarding in your X configuration and keep in mind that this connection is insecure. It is therefore recommended only for home network.
One alternative to connect to Linux from Windows is to use VNC, although it has several advantages but using X via VNC is slower than using XDMCP.
---------------
"What Every Computer Consultant Needs to Know:
1) In case of doubt, make it sound convincing.
2) Do not believe in miracles. Rely on them." Murphy's Computer Laws (Finagle's Rules)
After left it untouched for more than a year, I decided to update the system.
I started off from connecting my Linux from my Windows via XDMCP from cygwin with
$XWin -query linux-pc -screen 0 1280x1024
where 0 from screen option is the screen number and 1280x1024 is the geometry I want it to be. Or use the following command:
$XWin -query linux-pc :2
:2 is to tell the X server of the display number, the default is 0. You need to tell the X server of which display you have if you have multiple X accesses to the X server. The X server will assign a unique display number for each connection. Nevertheless, to enable network establishment between X client and server, you need to allow for X forwarding in your X configuration and keep in mind that this connection is insecure. It is therefore recommended only for home network.
One alternative to connect to Linux from Windows is to use VNC, although it has several advantages but using X via VNC is slower than using XDMCP.
---------------
"What Every Computer Consultant Needs to Know:
1) In case of doubt, make it sound convincing.
2) Do not believe in miracles. Rely on them." Murphy's Computer Laws (Finagle's Rules)
Sunday, January 4, 2009
Reset old root passwd with chroot
For at least 2 years, I did not need to transform myself to become superuser, root. Sudo is fit for everyday admin's works and Linux is very stable. (As you know it) Hence, I hardly need to configure anything new. New year has come and I wants to update and clean up things. And the most graceful amenity way to do things is to become the superuser. However, at that moment, I found out that I forgot the root passwd! 1..2..3.....10.....15 tries, ahh..without sucess. Enough is enough. Let's reset the old passwd!
Rebooting the system with a bootable CD. (Here, I did not know where I did keep my Debian CD, I didn't use it for ages. I searched around and ahh..I still have KNOPPIX CD.)
After rebooting the system, at the command prompt, let's create temporary directory mount point.
#mkdir /mnt/hda3
Mount the partition, in my case it locates at /dev/hda3.
#mount -t reiserfs /dev/hda3 /mnt/hda3
Here comes the chroot in action:
#chroot /mnt/hda3
You may be able to see your root shell path changed. Ok, we now call the "passwd" command, the shell will not ask for old passwd but allow us to enter new passwd straightaway. Reboot and remove your bootable CD. Done.
-----------------
Rebooting the system with a bootable CD. (Here, I did not know where I did keep my Debian CD, I didn't use it for ages. I searched around and ahh..I still have KNOPPIX CD.)
After rebooting the system, at the command prompt, let's create temporary directory mount point.
#mkdir /mnt/hda3
Mount the partition, in my case it locates at /dev/hda3.
#mount -t reiserfs /dev/hda3 /mnt/hda3
Here comes the chroot in action:
#chroot /mnt/hda3
You may be able to see your root shell path changed. Ok, we now call the "passwd" command, the shell will not ask for old passwd but allow us to enter new passwd straightaway. Reboot and remove your bootable CD. Done.
-----------------
"With Great Power, Comes Great Responsibility." Uncle Ben to Peter Parker in Spider-Man. I came upon this quote the first time when I first tried "sudo".
Subscribe to:
Posts (Atom)