1

EDIT:

The goal here is to test if lines in fileB still exists in fileA and if not to delete them from fileB

1) fileA holds data that gets overwritten

2) fileB holds data from fileA to do calculations with and manipulate

3) Once fileA changes and particular lines in fileB are no longer in fileA because of the change then fileB must have those line removed aswell

I want to read a file line by line and check if that line still exists in a different file

fileA.txt has inputs of data like so but is over written every 2 seconds:

456  6554  56734657387365873456783456
457  4545  36747365467345735736345665
454  4357  98345783465785345567865535
456  6646  70899089089056879678575775

The content of fileA.txt is appended to fileB.txt

fileA.txt >> fileB.txt

I have fileB.txt that looks like this:

456  6554  56734657387365873456783456
457  4545  36747365467345735736345665
454  4357  98345783465785345567865535
456  6646  70899089089056879678575775

My MAIN GOAL is to read fileB.txt and check if each line still exists in fileA.txt and if not to remove that line from fileB.txt

Here is what I have started:

while read line
  do
    if grep -q "$line" fileA.txt; then
      echo "This part is point less im still learning" 

    else
      sed '/$line/d' ./fileB.txt

    fi

  EDIT:

The goal here is test is lines in fileB still exists in fileA and if not to delete them from fileB

1) fileA holds data that gets overwritten

2) fileB holds data from fileA to do calculations with and manipulate

3) Once fileA changes and lines in fileB that are no longer in fileA because of the change must also be removed from fileB done >fileB.txt

theloosegoos
  • 129
  • 1
  • 10
  • Is fileB.txt expected to contain duplicate lines? If not, the net result of what you are trying would reduce fileB.txt to fileA.txt. I don't seem to understand your requirement. – codeforester Feb 08 '17 at 03:25
  • @codeforester i handled duplicates already so assume theyre not there – theloosegoos Feb 08 '17 at 03:36
  • let me get this straight. so you want to keep the common lines between fileA and fileB in fileB? – joydeep bhattacharjee Feb 08 '17 at 04:09
  • @oydeep bhattacharjee no because your choice of words. My goal is check if the lines in fileb still exists in fileA and if they do not exist it fileA anymore then the line should be removed from fileB. – theloosegoos Feb 08 '17 at 06:13

2 Answers2

5

You can use grep -F for this - it is much more straightforward and efficient:

grep -xF -f fileA.txt fileB.txt > fileB.txt.trimmed
mklement0
  • 382,024
  • 64
  • 607
  • 775
codeforester
  • 39,467
  • 16
  • 112
  • 140
0

You probably will have to create an intermediate file if you want a small command. Also check if you have comm. Pass the flags -1 -2 along with it to get the common lines.

comm fileA.txt fileB.txt -12 > fileB.txt.trimmed
joydeep bhattacharjee
  • 1,249
  • 4
  • 16
  • 42