Search & replace with find & ed

2010-03-28 0 min read Bash Learning Linux
\"Computer
Image via Wikipedia

function sr() {

declare pattern replacement name usage
declare -i pvar=0 rvar=0 tvar=0

usage='usage: sr [-t ] [-n name] [-p pattern] [-r replacement] [– ] [dir1 dir2 …]'

# cf. <a href="http://bsdpants.blogspot.com/2007/02/option-ize-your-shell-scripts.html">http://bsdpants.blogspot.com/2007/02/option-ize-your-shell-scripts.html

while [[ "${1:0:1}" == '-' ]] ; do

[[ "${1}" == '–' ]] && { shift; break; } # – marks end of options

Continue reading

vim search multiple files

2010-03-25 1 min read Linux Vim Tips

Searching some pattern in multiple files is something that is required very often. There are multiple ways to do it, like <a class="zem_slink freebase/en/grep" title="Grep" rel="wikipedia" href="http://en.wikipedia.org/wiki/Grep">grep, <a class="zem_slink freebase/guid/9202a8c04000641f8000000006b66ec6" title="Ack (command-line utility)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Ack_%28command-line_utility%29">ack, find and so on so forth. But if your editor already has the capability to do that, then why leave your favourite vi and go to shell. I will point you to some scripts and some wiki pages on how to do this πŸ™‚

Continue reading

Recursive Regular Expressions

2010-03-24 2 min read Bash Fedora Linux

<img src="http://blog.amit-agarwal.co.in/wp-content/uploads/2010/08/yo-dawg-regex.jpg" alt="Yo dawg, I heard you liked regular expressions, so I put a regex in your regex so you can match while you match!" align="bottom" /> The <a class="zem_slink freebase/en/regular_expression" title="Regular expression" rel="wikipedia" href="http://en.wikipedia.org/wiki/Regular_expression">regular expressions we use in our daily lives are actually not that β€œregular.” Most of the languages support some kind of extended regular expressions that are computationally more powerful than the β€œ<a href="http://en.wikipedia.org/wiki/Regular_expression">regular” regular expressions as defined by the <a href="http://en.wikipedia.org/wiki/Formal_language">formal language theory.

Continue reading

Linux find command – Find file and directories faster and easier

2010-03-16 1 min read Linux

Β 

This command is very powerfull when used with combination of filters and pipes and RE.

I will give some example:

find . -type f –» List all files
find . -type f -exec rm {} <a class=“zem_slink” title=“Path (computing)” href=“http://en.wikipedia.org/wiki/Path_%28computing%29" rel=“wikipedia”>\; –» Delete all files
find . -type d -exec rm {} \; –» Will through some common errors πŸ™‚
find . -name "name"Β  –> find files containing name in the filename
find . -atime 12 –> Find files accessed 12 days ago

Continue reading

Perl script to create csv files with a pattern – Generic script.

2010-01-19 1 min read Learning Linux Perl

I was having a really bad day and needed a quick solution to create some csv files. And this I needed to do for multiple data kinds and patterns, so I created this small script to do the job for me…

#Number of rows required in the output.

$rows = 100;

#The config and the output file

open (CF_FILE, &#8221;<Config.test&#8221;);
open (OUT_FILE, &#8221;>test.csv&#8221;);

#β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# No need to change anything below this.
#β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”

Continue reading

Display the output of a command from the first line until the first instance of a regular expression.

2010-01-04 0 min read Bash Linux
\"Screenshot
Image via Wikipedia

| perl -n -e 'print "$_" if 1 … /«a class="zem_slink freebase/en/regular_expression" title="Regular expression" rel="wikipedia" href="http://en.wikipedia.org/wiki/Regular_expression">regex>/;# This <a class="zem_slink freebase/en/command_line_interface" title="Command-line interface" rel="wikipedia" href="http://en.wikipedia.org/wiki/Command-line_interface">command line will display the output of , from the first line of output, until the first time it sees a <a class="zem_slink freebase/en/pattern_matching" title="Pattern matching" rel="wikipedia" href="http://en.wikipedia.org/wiki/Pattern_matching">pattern matching .

Continue reading
Newer posts