SED - how to delete everything after a special character on every line?

Transfered from Linux Config Disqus comments:

Question:

how to delete everything after a special character on every line?

Hi Pazis,

You forgot to mention what your special character is. But let’s suppose that your special character is “@” and you wish to remove everything after it on every line using SED command:

Here is a content of sample file sed.txt:
sed scriting@linuxcareer
sed scriting@linuxcareer
sed scriting@linuxcareer
sed scriting@linuxcareer
sed scriting@linuxcareer

to remove everything after @ symbol we can use the following command:

$ sed s/@[^@]*$// sed.txt

For example:

$ cat sed.txt 
sed scriting@linuxcareer
sed scriting@linuxcareer
sed scriting@linuxcareer
sed scriting@linuxcareer
sed scriting@linuxcareer
$ sed s/@[^@]*$// sed.txt 
sed scriting
sed scriting
sed scriting
sed scriting
sed scriting

Hope this helps…

Lubos

1 Like

I want to delete a particular string which is commented and with the particular name .sh file.

demo1="#*/05 * * * * /usr/share/batch/MapfrePro/ProducerOneBatch/UpdateRequests.sh"

However, this file contains more commented scripts too.

Hi Abhi_Chawla,

Welcome to our forums.

If you know the exact filename, and it is unique within the textfile, a simple grep should do the trick:

$ cat test.file 
demo1="#*/05 * * * * /usr/share/batch/MapfrePro/ProducerOneBatch/UpdateRequests.sh"
demo2="some-other-file.sh"

And I need all but the first line, where the UpdateRequests.sh resides:

$ grep -v "UpdateRequests.sh" test.file 
demo2="some-other-file.sh"

With the -v option I get everything but the excluded line.

1 Like