Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

InputData.txt

123,
1234,
1453,

Datatodelete.txt

1234,hellofirstline
123,hellosecondline
14676,hellothirdline
1453,hellofourthline

expected output in the Datatodelete.txt

14676,hellothirdline

Script:

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

3 Answers 3

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
          POSIX.)

   -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
2  
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 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 at 21:00
add comment

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 at 15:45
    
@Flup, it does provide the output the OP seeks. –  glenn jackman Mar 19 at 15:46
    
...but doesn't answer the OP's question. –  Flup Mar 19 at 15:47
1  
@Flup This isn't a story-telling competition. –  devnull Mar 19 at 16:22
add comment

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' 
1234

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
add comment

Your Answer

 
discard

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.