Live Chat!

How to redirect output to a file as well as display it out

* * * * * 1 votos

December 6th, 2007 mysurface

To redirect standard output to a file is easy, you just need to use the redirection symbol, for example:
echo “hello world” > test.txt
But what if I want to display it out as well as store into a file?
Answer: tee
echo “hello world” | tee test.txt
Okay it seems very easy, how about append?
Related PostsRedirect stderr to stdoutVery [...]

Posted in Text Manipulation, pipeline, python, tee | Hits: 28008 | 1 Comment »

pcregrep, grep based on perl compatible regular expressions

          0 votos

November 20th, 2007 mysurface

grep supposedly support perl regex by specified -P option, but somehow my grep do not support that, it complains this when I trigger with -P
grep: The -P option is not supported
But when you check the manpage of grep, you will see this
-P, –perl-regexp
[...]

Posted in Regular Expression, Text Manipulation, pcregrep, printf | Hits: 22507 | No Comments »

smart grouping shorten long command line 2

* * * * * 1 votos

June 19th, 2007 mysurface

This post is to illustrate additional usage of curly brackets { } for range of numbers, continues from smart grouping shorten long command line.
Let say you have a list of logs where you want to pick a range of logs for analysis, the sample log files shows as bellow

tcp_06062007_1.txt

tcp_06062007_180.txt
tcp_06062007_181.txt
tcp_06062007_182.txt

tcp_06062007_230.txt
In order to archive logs from range [...]

Posted in Symbol, curly brackets, tar | Hits: 21177 | No Comments »

CLI magic: programable bash completion

* * * * * 3 votos

May 4th, 2007 mysurface

Ubuntu, a Linux distribution that is getting popular because it has successfully improves it’s user friendliness with simple User interface. You can install any applications with few clicks, configuration under GUI dialogs etc. People usually have the thinking of maybe one day, Ubuntu will become another new operating system like Microsoft Windows.
Never! The concept of [...]

Posted in Bash, apt-cache, apt-get, complete, pipeline, sort, svn | Hits: 27703 | 5 Comments »

Redirect output to multiple processes

          0 votos

November 8th, 2006 toydi

Since tee can read the standard input, and write to multiple files, we may leverage this feature so that it writes to multiple processes (instead of files).
tee >(process1) >(process2) >(process3) | process4
>( ) (see process substitution) pretends itself as a write-only file. Everything you write into it, will be passed to the command (as standard [...]

Posted in Common, grep, process substitution, tee | Hits: 17804 | 24 Comments »

Compare two directory listings

* * * * * 1 votos

November 8th, 2006 toydi

Given two directories: ~/dir-new/ and ~/dir/,
$ ls ~/dir-new/
file-01.txt
file-02.txt
file-new.txt
$ ls ~/dir/
file-01.txt
file-02.txt
file-old.txt
To compare them:
comm

Posted in Text Manipulation, comm, process substitution | Hits: 23010 | 2 Comments »

Rename multiple files

* * * * ½ 7 votos

November 5th, 2006 toydi

Often over time, we will want to reorganize a group of files by renaming them.
To rename *.txt to *.bak
(e.g. to rename ham.txt to ham.bak)
for f in *.txt; do mv “$f” “${f%.txt}.bak”; done
To remove ‘new-’ from new-*
(e.g. to rename new-ham.txt to ham.txt)
for f in new-*; do mv “$f” “${f#new-}”; done
${variable%pattern} vs ${variable#pattern}
The funny-looking symbol, ${f%.txt} [...]

Posted in Common, Regular Expression, for, mv | Hits: 71228 | 14 Comments »

square brackets in regular expression

* * * * * 1 votos

October 17th, 2006 mysurface

Previously we have an example on regular expression, but It doesn’t shows the power of square brackets ( [ ] )
Let say you want to search for string fprintf, vprintf and sprintf using grep, usually what you do is
egrep “fprintf|vprintf|sprintf” *.c
You may be ask why don’t just uses the word “printf”? If uses the word [...]

Posted in Regular Expression, Text Manipulation, egrep | Hits: 8172 | 2 Comments »

Enable a user to gain write permission when mounting

          0 votos

October 10th, 2006 mysurface

We need to perform mounting with root privilege, but after a simple mounting, we need to be root in order to write to the mounting point. This is the default for a simple mounting, check out more mounting examples here.

But we can grant the mount point ownership to other user besides root. We can do [...]

Posted in Admin, cat, mount, pipeline | Hits: 8463 | No Comments »

Regular expression with egrep and ls

* * *     2 votos

October 7th, 2006 mysurface

egrep is actually same as grep -E, it interpret PATTERN as an extended regular expression. Therefore ESCAPE ( \ ) is not needed for certain symbol.
ls is a common tool for listing file that matches the keyword.
Regular expression (RE) is a very important and useful to manipulating strings and text. In this example, I would [...]

Posted in Regular Expression, Symbol, Text Manipulation, egrep, grep, ls | Hits: 15272 | 1 Comment »