Tips and tricks

Last update on February 22, 2009 08:17 AM by aakai1056
Published by aakai1056

Tips and tricks




Substitution


Substitute "foo" with "bar" on each line
Only the 1st occurrence
sed 's / foo / bar /'

For the 3rd case only
sed 's/foo/bar/3'

All occurrences
sed 's / foo / bar / g'

Just before the last occurrence
sed 's / \ (.* \) foo \ (.* foo \) / \ 1bar \ 2 /'

Only the last case
sed 's / \ (.* \) foo / \ 1bar /'

Substitute "foo" with "bar" only lines containing "plop"
sed '/ plop / s / foo / bar / g'

Substitute "foo" with "bar" except the lines containing "plop"
sed '/ plop /! s / foo / bar / g '

Replace "Foo" or "foo" with "bar" on each line
sed 's / [Ff] oo / bar / g'

Replace "blue" or "white" or "red" with "green"
sed 's / blue \ | blank \ | red / green / g'

Display


The 1st line (head -1)
sed q

The first 5 lines (head -5)
sed'5 q '
sed'1, 5! d '


The last line (tail -1)
sed-n '$ p'
sed '$! d "


The last 5 lines (tail -5)
sed-e: a-e '$ q, N, 6, $ D; ba'

The 2 last lines (tail -2)
sed '$! N; $! D'

Only lines matcha a reason or a regular expression
sed-n '/ pattern / p'
sed '/ regexp /! d'


Only lines that matcha not a reason or a regular expression
sed-n '/ pattern /! p'
sed '/ regexp / d'


The line preceding a pattern or a regular expression
sed-n '/ pattern / (g; 1! p;) h'

The line following a pattern or a regular expression
sed-n '/ regexp / (n, p;)'

Remove


===Space and tab==
Removing spaces and tabs

At the beginning of the line
sed 's / ^ [\ t] * / /
sed 's / ^ \ s * / /' # Using the parameter "\ s"

At end of line
sed 's / [\ t ]*$//'

At the beginning and end of line
sed 's / ^ [\ t ]*//; s / [\ t ]*$//'

Blank line

Removing blank lines

All empty lines
sed'/^$/ of
sed'/./! of


Only those at the top
sed'/./,$! of
sed-nr'/./,$ /(.*)/ s \ 1 / p '# thank you Adrien

Only those at end
sed-e: a-e '/ ^ \ n * $ / ($ d N; ba'-e ')'

Regular intervals
Eliminate a line at regular intervals

All lines pairs
sed'1 ~ 2d '

All the odd lines
sed'2 ~ 2d '

Every n lines from the line n
sed'3 ~ 2d '# 2 All lines from line 3

Miscellaneous


Join lines
Attach lines 2 by 2
sed '$! N s / \ n / /'

Attach the 3 lines by 3
sed '$! N s / \ n //;$! N s / \ n / /;'

If a line ends with a backslash (\), add the following line and replace the end of line (\ n) by a space
sed-e: a-e '/ \ \ $ / N s / \ \ \ n / /; ta'

If a line begins with an equal sign (=), add it to the previous line and replace the equal sign (=) with a space
sed-e: a-e '$! N s / \ n = / /; ta'-e 'P, D'
Best answers for « Tips and tricks » in :
Operating Systems - Windows - Tips and Tricks - Using Windows Show Restart Windows 9x without rebooting When you go to restart Windows, you click on Start, Shut Down, and then Restart. To restart Windows wthout having to restart the computer, simply hold while clicking on OK. A shortcut for shutting down...
Excel tips : How to insert date in a cell ShowExcel tips : How to insert date in a cell Below are some tips on how to insert date and time in an excel cell for a specific purpose:- To insert current date, press CTRL ¯+ ;¯ in the chosen cell. To insert current time, press CTRL¯+...
Keyboard /Mouse Tips for Excel ShowKeyboard /Mouse Tips for Excel Intro Other Shortcuts Intro Excel tutorials give hints that make the difference between a needy use and flexibility. Here are some examples: Copy the highlight to the clipboard: [Ctrl + Ins] Paste...
(MRU) Most Recently Used - Definition and Tips Show(MRU) Most Recently Used - Definition and Tips Most Recently Used (MRU) is a data based feature under Microsoft which detects in cached the most recently used items. In other terms, it means that same refer to a list of programs, files and...
Download Visual Task Tips ShowThe display of thumbnails of open windows in the taskbar is a new feature that was introduced in Vista. Visual Task Tips is a small program that lets you do so in XP. If you want your XP look like Vista, then you need this software. For more...
Operating Systems - Windows - Tips and Tricks - Organising Files ShowAdding items to the Start menu You can add shortcuts to files or folders in the Start menu by going to "Start/Settings/Taskbar and Start Menu/Advanced" (or by going to "c:\windows\start"). Windows 98 even lets you drag a program to the Start menu...
Operating Systems - Windows - Tips and Tricks - Customising Appe ShowChange computer's shut-down images These images are in fact camouflaged 256-colour bitmap images (renamed to a file with a ".sys" extension) with a size of 320x400 pixels. The files are:  c:\windows\logow.sys (Please wait while Windows is shutting...