PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 19, 2022

[FIXED] How to compare filenames in two text files on Linux bash?

 November 19, 2022     bash, linux, scripting     No comments   

Issue

I have two lists list1 and list2 with a filename on each line. I want a result with all filenames that are only in list2 and not in list1, regardless of specific file extensions (but not all). Using Linux bash, any commands that do not require any extra installations. In the example lists, I do know all file extensions that I wish to ignore. I made an attempt but it does not work at all, I don't know how to fix it. Apologies for my inexperience.

I wish to ignore the following extensions: .x .xy .yx .y .jpg

list1.txt

text.x
example.xy
file.yx
data.y
edit
edit.jpg

list2.txt

text
rainbow.z
file
data.y
sunshine
edit.test.jpg
edit.random

result.txt

rainbow.z
sunshine
edit.test.jpg
edit.random

My try:

while read LINE
    do
    line2=$LINE
    sed -i 's/\.x$//g' $LINE $line2
    sed -i 's/\.xy$//g' $LINE $line2
    sed -i 's/\.yx$//g' $LINE $line2
    sed -i 's/\.y$//g' $LINE $line2 
    then sed -i -e '$line' result.txt;
    fi
done < list2.txt

Edit: I forgot two requirements. The filenames can have . in them and not all filenames must have an extension. I know the extensions that must be ignored. I ammended the lists accordingly.


Solution

An awk solution might be more efficient for this task:

awk '
              { f=$0; sub(/\.(xy?|yx?|jpg)$/,"",f) }
    NR==FNR   { a[f]; next }
    !(f in a)
' list1.txt list2.txt > result.txt


Answered By - M. Nejat Aydin
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing