msgbartop
I will happily conduct a FREE basic web security scan for any genuine organization interested in my services to point out whether or not I can find vulnerabilities in your application. Just contact me.
Need a PHP Programmer, PHP staff or project manager? Contact me now.
msgbarbottom

05 Apr 09 Simple text sorting

On the command line we have a number of powerful tools available to us. I’m going to cover some text sorting methods here.

I have a file called ‘testfile’ within this file is the following:

test:~# cat testfile
line1
line3
abcdefg

test
line9

this is a test
test file

test

How to sort alphabetically?

ns3:~# cat testfile|sort

abcdefg
line1
line3
line9
test
test
test file
this is a test

How to show unique lines only?

test:~# cat testfile|sort|uniq

abcdefg
line1
line3
line9
test
test file
this is a test

How to remove blank lines?

test:~# cat testfile|sort|uniq|grep .
abcdefg
line1
line3
line9
test
test file
this is a test

How to show the first word of the line only?

test:~# cat testfile|sort|uniq|grep .|awk ‘{ print $1 }’
abcdefg
line1
line3
line9
test
test
this

How to replace every ‘i’ with an ‘X’?

test:~# cat testfile|sort|uniq|grep .|awk ‘{ print $1 }’|sed s/i/X/g
abcdefg
lXne1
lXne3
lXne9
test
test
thXs
That was a very quick and dirty guide to some simple text sorting!

Tags: , , , , , ,



Reader's Comments

  1. |

    Removing blank lines:

    $ grep -v ‘^$’ test.out
    $ grep ‘.’ test.out
    $ sed ‘/^$/d’ test.out
    $ sed -n ‘/^$/!p’ test.out
    $ awk NF test.out
    $ awk ‘/./’ test.out

    http://unstableme.blogspot.com/2007/01/same-task-different-ways-of-doing-two.html



Leave a Comment

You must be logged in to post a comment.