Linux: How to use the Text-Fu Command in Linux

Text-Fu is a command-line tool available in Linux that is primarily used for manipulating text. It provides a wide range of text-based operations that can be executed using the command-line interface. The Text-Fu tool can be used for various purposes, such as modifying text files, extracting specific information from text files, and generating reports based on text data.

One of the most common uses of Text-Fu is for searching and replacing text in files. With this tool, users can easily search for specific text in a file and replace it with another string. This can be useful when making changes to a large number of files or when trying to automate repetitive tasks. Additionally, Text-Fu can also be used to format text, such as removing or adding line breaks, removing whitespace, and converting text to uppercase or lowercase.

Another use of Text-Fu is for data processing and analysis. With its ability to extract specific information from text files, users can easily generate reports and insights based on text data. For example, Text-Fu can be used to extract data from log files and generate reports on website traffic or server performance.

Linux: Text Fu

Standard Output(stdout), standard input (stdin) and standard error (stderr) are three different data streams in linux. A stream may carry data, which in this example is text.

File Descriptor

Streams are treated similarly to files in Linux. Each file has a file descriptor, which is a unique number that is used to identify it. These numbers are as follows:

  • 0: stdin
  • 1: stdout
  • 2: stderr

File descriptor is used when an action on the file is required.

Standard Output

Standard output (Stdout) is the default file descriptor to which a process can write output in the linux. Let’s run the command.

$ echo Hello world!

PvU69f2dd5e00vB bDyAwOz4mhTkXz5ma 35DHqsXwXt7vXP r0Ez8nUTI7B3LZuPiZzEo7iAN1GYevS1u5qdM62P g3w Jpj90FPxQxw 4rRQ3nRuz4JcJOEJld EpQTrl1JPdt WA5j1MlV - Linux: How to use the Text-Fu Command in Linux

The text appears to be shown in the terminal. The procedure here is that the echo command receives the keyboard input (stdin) and shows the result (stdout). This output can alternatively be directed to a file.

$ echo Hello world! > test.txt

ZT2wmQ4p5gl q40FtdOL4nChQnus7h 1YQZdlPzi7u1iRafwcsnWlmh1ZG wr3uH L8Mmt1FGHFg1cAqZYFtOFY5g2qXIDx5p0aLTOA1nhwRnEGWxFJ1bNBDPvJpP2UnHvB8qBlSLFnxAKsKpj0vi42POIsxganAJyYdMcygOg - Linux: How to use the Text-Fu Command in Linux

The > character is used to redirect standard output to a file, in this example to test.txt rather than the terminal. As a result, the terminal displays nothing. Hoyouver, the text will be visible when you read the file.

$ cat test.txt

yG660fxIkwdfRoYC3pcdx9LYY 5gIc2grFH - Linux: How to use the Text-Fu Command in Linux

If the file does not exist, it will be created, and if the file already exists, it will be overwritten. Using the >> operator, you can now redirect data without overwriting the file. Let’s see what happens.

$ echo Hello world 1 > test2.txt
$ echo Hello world 2 >> test2.txt
$ cat test2.txt

dJlelsZF4LLvsCSxvXO6kbt2R3gxlY05MCjI6ypNEutYezCFlj4roaId2hxk8Mw6 IJRlyRLb7bQM4A5lkGSdn5bV89 ZFpNx2Dcw - Linux: How to use the Text-Fu Command in Linux

Standard In (stdin)

It is associated with taking input. Let’s see how it works.

$ echo Hello there! > linux.txt
$ cat < linux.txt > hello.txt
$ cat hello.txt

S7Gc9F9JzvnW0yUn79t3X4PL9sYR4lbrR52 OZuRTaCsADxBmPEEKbZHC5fsRBznMhI16lKO6MAJO7rWuSgAC4qKbNjcTDZsxoXt3TMyGnDeB34Som66w64kZIdIDiqjsHK1bqnd7drrXlohswOszCyFMxz1h5ZjNXYHQN1IbEc2vIpp0gdSAWI - Linux: How to use the Text-Fu Command in Linux

you first redirected ‘hello there!’ text into the linux.txt file. The < operator is used for taking input. So, linux.txt is taken as input and it is directed to the hello.txt file. Now, if you read the hello.txt, you can see the text is transferred.

Standard Error (stderr)

It is used for displaying the error messages.  For exploration, you can try to read a file named none.txt which does not exist. 

$ cat none.txt

HlX6JHeAQIyPkS9LIZrECGL6m5W63JRAcIxDRMVn5qs4Rsth1LzQgjiev3Vr1H31LOGkbpNUAHzJv64uZFqlmLXoQiUE1B dnZVipQIMMRaxbCCTVEKq Slgmdiua5lUNN - Linux: How to use the Text-Fu Command in Linux

When you run the command, the terminal will throw an error as  ‘No such file or directory’.

Now, if you want this stream to redirect into a file, you have to run the following command.

$ cat none.txt  2> error.txt


n0PHk3VdB 5XFPoWGAnqkt5MxTsWE 20LFOe3M7UAufLi0 KGbQJe3w9TPEtA27IEg5ucFNbYs11A57fR 0FYTqt9B8eX3VF2LYiz33Gg3pt5DcJuAJhJZZuZoJaNI p5XfvryH56MxEHERwKXMTrnjktgdLuVR - Linux: How to use the Text-Fu Command in Linux

It’s worth noting that you have to provide the file descriptor because it’s stderr, not stdout.

Piping

The Pipe is a Linux tool that allows you to combine two or more commands so that the output of one becomes the input for the next. Let’s see how it works.

$ echo Hello there | grep Hello

BHdzYEntk3CbYL2jZo2zCQEzdTZ6DS MSLjBqqtEJ5O3oKjlRbJR8M5mZpBPb2yB7eFPegfDqA1Hj p6BK OKDax 4Z7qHgJdxEolOjMs93eMm5dG4b42SPrC RP - Linux: How to use the Text-Fu Command in Linux

The | operator refers to the piping. In this example, echo generates the output (stdout) text which is then used as input (stdin) for grep command. 

Tee

Tee is used when there is a need to write the output in two different streams. 

$ ls | tee ls.text

SQ kJLE5A9UITkvdHYXM7ZdDKPF6aqeDhDzkLQ0yZleTZsZ6sJ7NUOBgd4BsNnuqcN7zfj6HyGlEm DZuiisp5J9ZmdZ9oYuXI8f0dUP9yv ZmpoZdB - Linux: How to use the Text-Fu Command in Linux

Here, the ls command will show all the directory and files in the terminal and also the same output is written in the ls.txt file.

Environment (env)

In linux, the environment is defined by the environment variables which is a character with an assigned value. Some variables are set by the system and some by the user. Now, let’s run the following command:

$ printenv HOME
$ printenv USER

Or 

$ echo $HOME
$ echo $USER

0JLDnnOBWRv00 Lo1foT3r7Gk9hW3c9jC62Fho21d9bxkQaIwy0rjt o YzQO7Szek4YeP89sK1D3dgX 09XqSLz5Zyml4FkzxQG0nY2tC qQdSFwkY4ek0 C52XCqqy8SDH90rRatldFqg5mogutBh1cNzHo4r1qycKJW mBYW72lu mnFMQRkB - Linux: How to use the Text-Fu Command in Linux

These are some variables in the environment. The PATH variable is another crucial variable. When you run a command, the system looks for these. If you try to execute a program from anywhere other than its installation path, you’ll get a ‘command not found’ error. You must include the program’s installation directory in the PATH variable in this situation. In this way, the system will know where to look for when this command is executed. For finding out  the PATH variable of your system, you have to run the following command.

$ echo $PATH


- Linux: How to use the Text-Fu Command in Linux

The following command will display all of the information about the environment.

$ env 

Or 

$ printenv

Cut

It extracts portions of the text from a file. The portion will be determined by the user. To understand this, first let us create a text file.

$ nano hello.txt
hzu8fpO9oU9fb8E4 F0dM12LEmUbY9i7kKQ tEzkA9MD9jktzlNBQOaHZPxkM94BKQG0is0WmqsORyE3HV52d2VyMajLFER91dVQpqp9lLhQT8ivC3ofd8MWUEcHBmADQPqOjGaf20iMiJCPY0gKd zMJCr6TiMelBxy6yLShsEarQ1SD g1O3D0 - Linux: How to use the Text-Fu Command in Linux

Now let’s say you want to extract the 10th and 8th character from this file. For this you have to run the following command.

$ cut -c 10 hello.txt 

$ cut -c 8 hello.txt

MmogbrFF2IT NJ1GNGpto8vCH 34oJbehGsPf8xBOSk559tgJNeH r8x 88Cg62oPbO wRBQzMv Yojflmcgz8rx nT31UV5ORfjXSGCQrD TPWvFjPkD9S7COLERQzT720IZ5TdeESfiDFSPslIEW8maslZDmGrKlgTfoAjriyRYXe9FtlH6cp9 - Linux: How to use the Text-Fu Command in Linux

Here, -c refers to the character extraction. Important thing to note here is that space also counts as a character. You can also extract the text by a field using the -f. By default, it takes TAB as delimiters for the field. In the hello.txt file, the number of TABs used is 2. So, there are 3 fields.

$ cut -f 3 hello.txt

It should give ‘fur’ as output. Let’s see. 

Zre b5oTQT gexqLETBdIzY 59WbhjVv7V mNxRz Ox3f ONgYuKqUjjH6MOrDKxh88WjYWaJJh6 oWrRXudo3jZvylzT7CHGEp5386sesN7FuuuygmXFsrJz5a8LIB13k5V qigcMDyA91Pv0VBQj3pbzHZ2zXds25Na0cnhLpJiFLD 76ceHu - Linux: How to use the Text-Fu Command in Linux

yeah ! It’s working. Now, the good news is you can set the delimiter that you want. Lets run the following command:

$ echo -f 3 -d “,” hello.txt

U5Rka6ry EhYXecRdeFu ndBsk ZAc3D9ayWxwSNety5gB2 HLBJ6ZA2cPJ8hAlE4Nypi5A3WLxN0o55y0ZgkrwjoAj4vS6mBmMjfGotSHEIE99TjtQt2GGFfWGW69E7akiMCACUpbWzJc hEor V1IKdU FERhdcXcvuQ62RvJ7Aml57EGN64 - Linux: How to use the Text-Fu Command in Linux

The -d is used for setting the delimiters. Here it is set as comma (,). Hence, for the hello.txt file, the result should be ‘little ball of fur’.

Paste

In linux, paste command is used to join files horizontally from each file specified. It displays the standard output where TAB is used as the delimiter. To explore the paste command first, we need to create two files.

HgcUcYLPtZt6OVVyCr0R SQqld9bhI7hTJGL3O3On0cFoz GIc6GItbdDI fXPA93bQlJgfgtFrWiEitUIX cezyklM5Fbr6mh5cLwYA43mMG3 NvraoRRbHwwWfSy4XJFHltzs3Mo7K6SPI JmVb2GEOwPx QT0Fh7CEYchcgwLD PVop0lK0VE - Linux: How to use the Text-Fu Command in Linux

 0mUasiPGen7QN Ayxjzu3g2ho7 3b4xTOPouOMYI5DhxHo4BJzRECCoo24DoPvn6 RmJsSL24Cc0ylskQEgU2ezfuhnrxqjYDPrSocr8eRlovNhW0hKS4kdbyliP YUU12PFCdgElkiJj SiJLH4wrX5SeeAauesFTDjGhI1gdqVs0ndP1y4N0kQ - Linux: How to use the Text-Fu Command in Linux

So, we have created country.txt and capital.txt files. Now let’s explore the paste command.

$ paste country.txt capital.txt


- Linux: How to use the Text-Fu Command in Linux

When no option is used, paste combines the files in parallel, which means it writes matching lines from the files using the TAB delimiter. Let’s explore the options available for the paste command.

$ paste -s  country.txt capital.txt
d FWzAVUuAWK5DGf97yERCA7fg7YcdoYugOrDdiyxvbtFNtO6C5PdyyT2h5S1wtTwts0HXoHSmAsftuofN95v17uF9ufjOn0lNVnqluSrGMDSyMV9yvpDo9QxtLInItXd6s4LCHYFWxlxSV66nCLli UougjysgX9vMNH YFIcWA3RhRwz2yNXo2 - Linux: How to use the Text-Fu Command in Linux

When you use -s option, paste merge the files sequentially. It reads all of the lines from one file before merging it with another. As like in the cut command, here also you can use a custom delimiter. 

$ paste -d “-” country.txt capital.txt 


9dirk9I0vUDAA1ETz9G5FDo31LRapaLpSsu9Wb8K7DMhJ5TFkBBtDgA8mrdNUsyjMThNcP1T4c0NGkGlHDFwmEBorPM - Linux: How to use the Text-Fu Command in Linux

If more than one character is passed as a delimiter, it uses the mentioned characters in a circular fashion. Let’s see how it works.

$ paste -d “;-” country.txt capital.txt 
v3liLLmgdagkzJ8U0Z2AqVPDi3kTyA3smDe7l74Pq0VQTVoCd1JEafrh15vpp3SAj5nubNjBrFNrC2U EZtqmoIjUgxMJqihxYeeiKwSkD8BnNG13xhE lbeGuiaaFwyrJj3DdqqB1 27zLQZ7Tl43IDnc - Linux: How to use the Text-Fu Command in Linux

Head and Tail

It is useful when you have to read a very long which we often encounter. If you try to read it using cat command, the terminal will be overloaded with the text. In this manner, head allows you to read a number of lines as like from to top of the file. So, for testing purpose, you can choose any of your log file and run the following command.

$ head -n 5 /var/log/auth.log
cSY 0klQsr8wkN94LF4JVvtLhLwV5vyzZ MzMcKA630Dvl3 5NyE6U67NBpMAyhlNZ9V9cl2jv po W3OqRU8JpQ7q 2o0SmzQu7fr4w2yA 8BsbBCy4JO4F4K0sD rHZ9BfAAa7DQwf95QFxYHjKoLT5rKhoUtLlUb9oQE3pmYz8Ggjqii eOql - Linux: How to use the Text-Fu Command in Linux

The -n option is used for selecting the number of lines. If number of lines is not specified, it shows the first ten lines.

Similar to the head command, tail is used for read a number of specified lines of long files from the bottom. It also shows 10 lines by default but you can choose as you like.

$ tail -n 4 /var/log/auth.log


XoZedbwsU1mEQ5YLkMceC UVeRrivJVjXZBaV8TPMxtESzaNU2BQmKPDzoL2B7iA3Ae2TsKGObNfV jfwNnuZ Hl5af1hYBsVmEgjtbRXcW6CnbX7RY6IRxdXyFaSNB2RHo4QpJucorPgEydJ28fCQ2PHge2DnZV IepNvWTfzqWLEz97h8ULhOc - Linux: How to use the Text-Fu Command in Linux

There is a interesting option available for this command which is -f. It allows you to see if anything is added  to the file. Let’s run the command.

$ tail -f /var/log/syslog
vsqWyYyj V24u83uA5cG8p2LbGgb6Qhnyjws bksJ3JAeX6qNNTHrrkWMy8EjlVwQGaWBUcN58zT0tdnq1bfzwH5Cqn2Ko5FUofIeKO wxVlTK OfR2Sr3MNZePAI2UyX6CVm qaHitOA0Oh1tk1gRkmabBEudJwmWTzCTjlTjZBEj 53W02kyl - Linux: How to use the Text-Fu Command in Linux

Join

It is used for joining multiple files together by a common field. To explore this, first make two files such that they have a common field. 

 Country.txt

1 Pakistan
2 India
3 Bangladesh
4 Russia

Capital.txt

1 Islamabad
2 New Delhi
3 Dhaka
4 Moscow

You can see, these two files have similar fields of 1,2,3,4. So, you can join them using join command. Now, run the following command to do that.

$ join country.txt capital.txt
2dyvJXQXMSkvJ004ndsv8cA2tn 5hsrWup7EBHofGY6o hkViPxcCCM8GDjNpvtTAYGChc9vSy7vnalbCaHYb32IjzPPF9mkDRDpC1kMP5UkZKUTJGUSFPyeLIWQhDfp S2VCRMcC31cN ERe065ab5 iJUxb5 z5luUz8dDzezPzq tyBg3F l - Linux: How to use the Text-Fu Command in Linux

Now, let’s see another case where files have similar fields but their position is different. 

 Country.txt

Pakistan 1
India 2
Bangladesh 3
Russia 4

Capital.txt

1 Islamabad
2 New Delhi
3 Dhaka
4 Moscow

How can you join these two files? Here, we want to use field 2 for file 1 and field 1 for file 2. Good news is join command allows user to do that. Now, run the following command.

$ join -1 2 -2 1 country.txt capital.txt
RttGGOzaV2pEI5 PCmmxAchxWRP2aZ6mQEEOJ7qtMjobkaITSCcG8 7X2gxVlyBdQjSfE2u9gS6lzpWn Zd5oDNT4oGjV8pV OetjXGEXfFkPzFSCqr8nsn5H8AsB9WqaBcJkEp to Up7v1d K8M2Sd5WGcatMQs GfswwOr4byndSWLNEymRL - Linux: How to use the Text-Fu Command in Linux

In the command -1 refers to the country.txt or 1st file and -2 refers to the capital.txt or 2nd file.

Split

The split commad does the opposite of the join command that means it is used for splitting large files intro smaller files. First make a text file with 10 lines. Now, let’s run the following code and explore yourself.

$ cat large.txt
$ split -l 4 large.txt

- Linux: How to use the Text-Fu Command in Linux

Here, -l options let you choose to decide the number of lines for a file. By default, after 1000 lines, split command will create new files. The created files have name ‘xaa’, ‘xac’, ‘xab’. You can aslo choose the name of ‘x’ here. Run the following command in your terminal.

$ split -l 4 large.txt small_


vQ4w1FVshMaDs9TT9uK hyH67tPYI0JmMuijsuDBDK9SWzkXSNYDU tStACEMfTy11pN2HFeltusNdHdkmxfgW73s6OpW9kFdTa9fLO1J4dkk tam9B1h qb94S48A1Ps UA - Linux: How to use the Text-Fu Command in Linux

A name of ‘small_’ has been after large.txt here. Hence, all the splitted files will be created have name like small_aa, small_ab etc. 

Sort

It is used for sorting files in a specific order. The sort command assumes all the characters of the file were written in ASCII.

$ sort -n sample.txt
$ sort -r sample.txt

PJcWWR1JyOdMW WpWGnAH zmxrrNODOcJh23ITJElOUwWvKNRKsziwNFtBQe54nSph8bDdv2QMpAfGFnlQqCdb1Ku ZHYd6iAIEhVL aZRHd9qhYFZ1H7M1 - Linux: How to use the Text-Fu Command in Linux

When the -n option is specified, the contents of the file get sorted numerically and the -r option allows you to sort reverse of numerically. There is another useful option for sorting which is -u which let you remove the dulplicate contents. Let’s try this one.

$ sort -u duplicate_content.txt

k42xqbYTLbW57pJWdcnahDAaXUK8Az6L J4uRo8BLuda8Xja5gw0LwbuVKTiRV7jxPSkZARC1a sluUcsMJoc53dHH8GBDATkD - Linux: How to use the Text-Fu Command in Linux

 tr

The tr command can translates or deletes from the stdin (standard input) and display the stdout  (standard out). It is useful for removing duplicate characters, find and replacing, lowercase to uppercase conversion. 

$ tr a-z A-Z


wKjmDLiaB kGo 0qwXt tXU4952ZRd4p2JTfD6 oAGYw2Le0lNccKbuv29lAOoSCuuiK96XKZMzHS9yiYvyqnzP0ieSuXX9EXKqDXIq g3z42zdju5rVlwhnxfXunkMYErP5tvk6AB7th6LXxeP2TLFDK19KW6 IoMZKvAPhJq5Db k76Kp27b b - Linux: How to use the Text-Fu Command in Linux

In this case, terminal is accepting input from the user and converting the input into uppercase characters. Now, let’s see how can you replace character using tr. 

$ echo ‘linux-ubuntu’ | tr ‘nu’ ‘ab’

 SYMCGjBJhCdSCPxxO3esvKzUixJ7 b1gOGq1ip 2vxCm5rOOlc6KPbgBHxnX4etk5 gGRLySr9LaUSX9YJnHcKck4r08vVmfftoX bC6LoruvM1eWQXOpvFsvF1JEek8CKO8bpi2IitOJOp ISHHk7KiNX1eot3C8Lwco96ZB w4ovhiruNdqWGw - Linux: How to use the Text-Fu Command in Linux

In this case, every ‘n’ and ‘u’ character will be replaced by ‘a’ and ‘b’ respectively. 

Wc and nl

It is used for showing total number words in a file. It can also show the number of lines and and number of bytes respectively. 

$ wc large.txt


VXlR7P5pp t2JRZhREp53rIJYHh0IrVTJaqurNWdMO5Dv A3BANfNBo4LmHSwMeTy1vJBqtKe kjv4DhjcfnAURPN7eQsX7TSak82Y bl9C58U6th1o810qkxtKc4hEPXQOkCyVkSNhpPFbawcVK5WWk 93giyFQYs uK b vJnYWWe5 bzBoltG - Linux: How to use the Text-Fu Command in Linux

To see just the number of lines you have to run the command below.

$ wc -l large.txt


0UXkp10wvbYk al v8IjM5 MaTXTgJs0lO4 SQpp sTW S90Rb7MKew1kEJzQfjnkdIHKL0GP4dS93BkMbiwyBg Lo n7rDVyMV 3jVPLb sGEVRNW43SuUo7wcYKwaFeLZT2vjIu PbJXtz8oqG tvDbRbSA8BMgrsvWqB - Linux: How to use the Text-Fu Command in Linux

Grep

The grep command allows you to find characters in a file. It is very useful and one of the most popular command in linux. 

$ cat duplicate_content.txt
$ grep USA duplicate_content.txt
B2lT5YgzL RWJjUTgtTDVwVjUVILvBHbYZw9ChPt t3 93biKNZGo6IyQk8tdjoFjOUlEXwITtkIpj8POBsragBjho4AlKHV78hUVzvZSxT14wGm4pGB TxcYVEULY4p71qTCiUsjEXpvkKGwnNk6eS58QD5gBm2v7df45q3I4Ie9 Fz6cN8 R3R - Linux: How to use the Text-Fu Command in Linux

Text-Fu is a powerful tool that can be used for a wide range of text-based operations. Its flexibility and versatility make it a useful tool for developers, sysadmins, and data analysts who work extensively with text data in Linux environments.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.