Tag: command line

Awk oneliner to find a string in text files

If you want to know the name of files containing a special string, grep -c is your friend. But you also get names of files not containing your string (with count = 0). If you only want the names of files containing “mystring”, awk can help you:

grep -c "mystring" * | awk ' !/:0/ '

And if you don’t even want the number of times “mystring” appears (on 1 line):

grep -c "mystring" * | awk ' !/:0/ '| awk '{split($0, a, ":"); print a[1]}'

Any other ideas?

GNU tools on MS-Windows

When you are used to work on a computer with GNU/Linux and are obliged to process your files on a MS-Windows system for some time, the GnuWin32 project can come in handy. They provide a lot of command-line tools from the GNU collection (sed, iconv, tar, bzip2, … see the whole list of packages they provide).

This evening, I needed to convert a lot of files from UTF-8 to iso-8859-1 (because it seems no decent Windows text editor can correctly translate text between these two encodings). Apparently, the GnuWin32 project removed the recode tool. But it can be easily replaced by iconv. With iconv, it’s done with:

iconv -c -f utf-8 -t iso-8859-1 utf8file.txt > iso8859file.txt

Photo credit: “In the beginning…it was the command line” by Dick Mooran on Flickr