Blog is moving

My blog is moving to http://victormendonca.com/blog/. If you are looking for a specific or older post you are in the right place Otherwise check out my new page for more up to date content.

Monday, December 24, 2007

How to remove repeated lines in a file without changing the order


Using just uniq is no viable as repeated lines need to be next to each other for uniq to identify it. Using sort will just mix them up, loosing their original placement within the file. So here's a work around (originally from Linux Journal).

Let's say we have a document called file:

$ cat file
a
b
c
d
a
b
c
d

Here are the steps we will take:
1- Use nl (or cat -) to add a numbering to the each line;
2- Use “sort -k 2” to place equal line after each other (we have to sort by the second column);
3- “Uniq -f 1” will remove equal lines (we also have to use the second column);
4- “sort -n” will re-add them in the proper order as per the first field, or the numbers
4- “sed 's/[0-9]//g'” will remove the numbers

This is what you command should look like:

$ nl file | sort -k 2 | uniq -f 1 | sort -n | sed 's/[0-9]//g'
a
b
c
d

On my machine I had a problem where nl kept adding empty fields (still trying to find why), so I had to modify my expression a little bit:

$ nl file | expand | tr -s '[:blank:]' | sed 's/^ *//g' | sort -k 2 | uniq -f 1 | sort -n | sed 's/[0-9]//g' | sed 's/^ *//g'

a
b
c
d

Now, let's say your sources.list got mixed up somehow, and all lines are now duplicate. We can apply the same concept like this:

$ grep -v '^#' sources.list | nl | expand | tr -s '[:blank:]' | sed 's/^ *//g' | sort -k 2 | uniq -f 1 | sort -n | sed 's/[0-9] *//g'

deb cdrom:[Ubuntu ._Gutsy Gibbon_ - Release i()]/ gutsy main restricted
deb http://ca.archive.ubuntu.com/ubuntu/ gutsy multiverse
deb-src http://ca.archive.ubuntu.com/ubuntu/ gutsy multiverse
deb http://ca.archive.ubuntu.com/ubuntu/ gutsy-updates multiverse
deb-src http://ca.archive.ubuntu.com/ubuntu/ gutsy-updates multiverse
deb http://ca.archive.ubuntu.com/ubuntu/ gutsy-backports main restricted universe multiverse
deb-src http://ca.archive.ubuntu.com/ubuntu/ gutsy-backports main restricted universe multiverse
deb http://archive.canonical.com/ubuntu gutsy partner
deb-src http://archive.canonical.com/ubuntu gutsy partner
deb http://security.ubuntu.com/ubuntu gutsy-security main restricted
deb-src http://security.ubuntu.com/ubuntu gutsy-security main restricted
deb http://security.ubuntu.com/ubuntu gutsy-security universe
deb-src http://security.ubuntu.com/ubuntu gutsy-security universe
deb http://security.ubuntu.com/ubuntu gutsy-security multiverse
deb-src http://security.ubuntu.com/ubuntu gutsy-security multiverse
deb http://archive.ubuntu.com/ubuntu gutsy universe multiverse
deb-src http://archive.ubuntu.com/ubuntu gutsy universe multiverse
deb http://wine.budgetdedicated.com/apt edgy main
deb http://ca.archive.ubuntu.com/ubuntu/ gutsy main restricted
deb-src http://ca.archive.ubuntu.com/ubuntu/ gutsy main restricted
deb http://ca.archive.ubuntu.com/ubuntu/ gutsy-updates main restricted
deb-src http://ca.archive.ubuntu.com/ubuntu/ gutsy-updates main restricted
deb http://ca.archive.ubuntu.com/ubuntu/ gutsy universe
deb-src http://ca.archive.ubuntu.com/ubuntu/ gutsy universe
deb http://ca.archive.ubuntu.com/ubuntu/ gutsy-updates universe
deb-src http://ca.archive.ubuntu.com/ubuntu/ gutsy-updates universe


Vic.

How to “Ctrl+Alt+Del” on Linux


Here are some steps you can take to regain access to a frozen Linux box. Re-starting the OS is not always the best choice as any un-synched drive cache will not be written and will result in data loss.

1- Try to find out what application is frozen. If it started happening after you opened a specific app, you can bet that application is the cause of the problem. Go to another tty (Ctrl+Alt+F1 to F6), logon and use tools like “ps” and “top” to detect the bad application, and use kill to end it.

2- If that doesn't help or you can not change ttys, try restarting X (some unsaved data will be lost) by pressing Ctrl+Alt+Backspace.

3- Next option would be to do a reboot by pressing Ctrl+alt+del. You can force a reboot by pressing the same combination twice

4- Last resource, press “Ctrl+Alt+Prnt Scrn” and type “reisub”. This will unmount the filesystem and shut it down


Note: This steps where taken from another blog. I don't remember the name, but will post her if I come across again

Wednesday, December 5, 2007

Portable apps


I just found this site for portable apps. They can be used to run applications directly from your portable device (USB, Ipod, external HD, etc...) without installing on the host computer. They can come very handy if you are on an environment where you cannot install a software, or even if you want to keep your "profile" info.

Portable Apps.com

They have firefox, gimp, pidgin, open office, winSCP, VLC, etc... Check it out when you have a chance.


Vic.