Playing with words in text file- Parse

When it comes to Linux, there are plenty of commands to parse the text file and provide desired result. As a beginner we come across many tools to filter, parse, detect and manipulate the string or words in text files or any other data streams. Here I will list some of useful fun element in Linux.

Codes:

This is the sample text which I am saving in a file called Sample.txt

This file is going to be tested for its string worthiness

Reverse String Command:

Now lets reverse the string easily using rev command.

rev Sample.txt
ssenihtrow gnirts sti rof detset eb ot gniog si elif sihT

The rev command reverses the order of the string.

If you double reverse it you get the same text.

rev Sample.txt|rev
This file is going to be tested for its string worthiness

Count Character Command:

If you want to count the number of character in the file then your wc.

 wc -c Sample.txt 
56 Sample.txt
wc -w Sample.txt 
10 Sample.txt
 wc -l Sample.txt 
2 Sample.txt

Now lets take another sample text file.

1 This file is going to tested for its string worthiness
2 Everytime a command parses this file something is filtered
3 The output can be much different or just the same 

Concatenate Command:

And try to cat it, The cat command concatenates the file.

cat Sample.txt
1 This file is going to tested for its string worthiness 
2 Everytime a command parses this file something is filtered 
3 The output can be much different or just the same 

Reverse order command:

Lets try to reverse order of the lines using tac command.

tac Sample.txt 

3 The output can be much different or just the same 
2 Everytime a command parses this file something is filtered
1 This file is going to tested for its string worthiness

So if you combine the rev and tac you get this.

 emas eht tsuj ro tnereffid hcum eb nac tuptuo ehT 3
deretlif si gnihtemos elif siht sesrap dnammoc a emityrevE 2
ssenihtrow gnirts sti rof detset ot gniog si elif sihT 1

and you will get the original back using the same commands again.

tac Sample.txt |rev|tac|rev
1 This file is going to tested for its string worthiness
2 Everytime a command parses this file something is filtered
3 The output can be much different or just the same 

There are many commands which are more fun to learn and play with and I will mention some of
them below. grep, uniq, sort, tr . Read their man pages for more details.

Leave a Reply

Your email address will not be published. Required fields are marked *