2

I have a list of lines in a file that look like this:

6642a51e0ed5e6f89e5e4090b9aa0fb3  /home/device/eth0/uid
d66d1417251a9a151176c34acf046878  /home/device/eth0/uid64
db5c3c9d348ce26169c56901a7606961  /home/device/eth1/dhcp
226c1a53543bddcca5d136fdd5700f4c  /home/device/eth1/ipv4
13b376c06f904b607c2a23bd76115595  /home/device/eth1/netmask

I want to remove certain lines from this file without any blank lines.

I found sed to be the appropriate command, although I've tried a few ways and look at others work I still don't know how to do it.

This is what I have sed -i "s|'/home/device/pn'|d" TestRun.chk

But it doesnt work... I've read articles on how to break the slash and how to not use it. I just don't have a clue to that works for my situation.

Your help is greatly appreciated!

EDIT:

Sample input:

6642a51e0ed5e6f89e5e4090b9aa0fb3  /home/device/eth0/uid
d66d1417251a9a151176c34acf046878  /home/device/eth0/uid64
db5c3c9d348ce26169c56901a7606961  /home/device/eth1/dhcp
226c1a53543bddcca5d136fdd5700f4c  /home/device/eth1/ipv4
13b376c06f904b607c2a23bd76115595  /home/device/eth1/netmask

After running the sed command to remove /home/device/eth1/ipv4 the expected output:

6642a51e0ed5e6f89e5e4090b9aa0fb3  /home/device/eth0/uid
d66d1417251a9a151176c34acf046878  /home/device/eth0/uid64
db5c3c9d348ce26169c56901a7606961  /home/device/eth1/dhcp
13b376c06f904b607c2a23bd76115595  /home/device/eth1/netmask
4
  • I don't want to just remove visible characters in the line, I want to pull the line right out
    – visc
    Mar 22, 2016 at 17:35
  • 1
    sed -e '/\/home\/device\/eth1\/ipv4/d' <file
    – cuonglm
    Mar 22, 2016 at 17:36
  • Why are you using -e?
    – visc
    Mar 22, 2016 at 17:38
  • 3
    Well, get rid of those double quotes and use single quotes instead, remove the s (you're not substituting anything) and escape the opening delimiter: sed '\|/home/device/eth1/ipv4|d' Mar 22, 2016 at 17:39

2 Answers 2

4

Using sed, you can do:

$ sed -i '\| pattern to match |d' File

This will remove lines containing pattern to match in File.

Using grep, you can do:

$ echo "$(grep -v "pattern to match" File)" > File

The -v option allows grepping of lines not containing pattern to match, which is then redirected back to File.

-1

As I see your file contains special character / you can this command to remove any line you should specify string characteristic of each line for example the 2nd line is "uid64":

sed -i '\|uid64|d' testRun.chk
In my pc it's print this without blank line :
6642a51e0ed5e6f89e5e4090b9aa0fb3  /home/device/eth0/uid
db5c3c9d348ce26169c56901a7606961  /home/device/eth1/dhcp
226c1a53543bddcca5d136fdd5700f4c  /home/device/eth1/ipv4
13b376c06f904b607c2a23bd76115595  /home/device/eth1/netmask

As you can see there is no the 2nd line you can do this for the following line "sed -i '\|ipv4|d' testRun.chk" and so one.

Good luck!

1
  • This answer just looks like a duplicate of the first answer
    – visc
    Feb 9, 2018 at 19:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .