Friday, July 22, 2022

[SOLVED] How to do a custom "grep" in linux terminal

Issue

Suppose I have file which contains only a text like below:

Test transition to drned-internal-asr9k-rt24711
   load drned-internal-asr9k-rt24711
   commit**

Now on the terminal if I do

cat filename | grep load

I would get output something like

load drned-internal-asr9k-rt24711

But how can I modify my grep command to get output as

drned-internal-asr9k-rt24711.txt

i.e. remove "load " and add ".txt" at the end. So how to do that??


Solution

May be not the best solution but :

cat | grep load | cut -c4- | sed 's/$/.txt/'

cut -c4- will delete the 4 first characters

sed 's/$/.txt/' will add the ".txt" at the end of output



Answered By - hicode
Answer Checked By - Senaida (WPSolving Volunteer)