Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to sed. What is wrong with the sed command on deleting the line which matches the input data?





expected output in the Datatodelete.txt



echo "the script starts now"
while read EachLine
  echo $EachLine
  sed "/$EachLine/d" < /home/Datatodelete.txt >/home/dummy
done < /home/InputData.txt
share|improve this question

Your sed command doesn't work because during the loop, each time it reads a line, it deletes that line (and only that line) from the full input file, outputting it to /home/dummy. This means that the output file gets overwritten each time. So the first iteration of the loop removes the line starting with 123, but then the second iteration uses the original full file which still includes this line.

Try grep instead:

grep -vFf /home/InputData.txt /home/Datatodelete.txt > /home/dummy

From man grep:

   -F, --fixed-strings
          Interpret PATTERN as a  list  of  fixed  strings,  separated  by
          newlines,  any  of  which is to be matched.  (-F is specified by

   -f FILE, --file=FILE
          Obtain  patterns  from  FILE,  one  per  line.   The  empty file
          contains zero patterns, and therefore matches nothing.   (-f  is
          specified by POSIX.)
   -v, --invert-match
          Invert the sense of matching, to select non-matching lines.  (-v
          is specified by POSIX.)
share|improve this answer
Depending on what else is in Input.txt you may want to use grep -F if there are characters that are intended to be matched verbatim. Eg . – Graeme Mar 19 '14 at 15:17
The above grep command is still not removing the records from Datatodelete.txt. It is still printing the whole content in dummy file – user3438085 Mar 19 '14 at 21:00

An awk version:

awk -F, 'FNR==NR{a[$1];next} !($1 in a)' InputData.txt Datatodelete.txt
share|improve this answer
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Flup Mar 19 '14 at 15:45
@Flup, it does provide the output the OP seeks. – glenn jackman Mar 19 '14 at 15:46
...but doesn't answer the OP's question. – Flup Mar 19 '14 at 15:47
@Flup This isn't a story-telling competition. – devnull Mar 19 '14 at 16:22

First, you don't want while read in there anywhere - sed will read your file. Next, you need to make sure that you handle greedy matches - sed will pull in as much as it can. So

    echo 1234 | sed -n '/123/p' 

See? It prints it.

So you need, based on what you've shown, something like this:

    </home/InputData.txt \
        sed -n '/1234/s//& hellofirstline/p;\
        /123[^4]/s//& hellosecondline/p;\
        /14676/s//& hellothirdline/p;\ 
        /1453/s//& hellofourthline/p' >/home/dummy

If your sed script is in a file:

    </home/InputData.txt \
        sed -nf ./delete.sed >/home/dummy         
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.