Help with shell script and sed!

Hi folks, maybe somebody here can help me.

I have 2 files. File A and File B. File A contains URLs leading to files on the Internet, with one URL per line; The format of the URLs on each line in File A is [http/filename].

File B contains only the filenames from each line in File A; One filename per line.

Here is my logic:

If any line in File B = A [http/"filename"] then echo the corresponding line in File A ["http/"filename].

I need to echo the results for each line. I'm assuming sed and some sort of loop would be necessary, but I'm still kind of a noob when it comes to this scripting stuff; sometimes I get it and sometimes I don't. I'll keep working on it in the meantime :slight_smile: Thanks in advance.

Try:

awk -F"/" 'NR==FNR{a[$0];next}$NF in a' fileB fileA

Thanks @bartus11. The only problem is that when I forward the output to a new file, the results are exactly the same as File A. I am trying to get the output to be everything that appears before the filenames in File A. Ex. http://blah/blah/blah/etc...

Post exact data (or sample of it) that is in FileA, FileB, and corresponding output file, that you want to produce.

File A

http://lh6.ggpht.com/_0ga0wb33PoQ/S2ulPfVJBTI/AAAAAAAAAYs/1wxZhiPW1mw/BM09_Starlight_-10.jpg
http://lh3.ggpht.com/_WpT8ajiz98w/R7aUf5YVUiI/AAAAAAAAAHA/00XJnAPH7TM/DSC02803.JPG
http://lh4.ggpht.com/_jgRMak8S5Ec/R5M5Zp8WFsI/AAAAAAAACWY/wrYFlk3e72c/Rubix%20Pyramid.JPG
http://lh3.ggpht.com/_WpT8ajiz98w/R7aT35YVUHI/AAAAAAAAADk/opr7BOANg2w/DSC02776.JPG
http://lh6.ggpht.com/_dFjcay6XJ8I/SM8PGL2ytcI/AAAAAAAAFmA/jKMF6wiNyaU/IMG_1367.JPG
http://lh3.ggpht.com/_AMLQDMZjxvk/RvQc2eZhxZI/AAAAAAAABt0/QmXjo-opZqk/DSCF1238.JPG
http://lh5.ggpht.com/_Ehm252Yypl4/S1uM4m374JI/AAAAAAAAKyk/dzsIhkApavc/IMG_5049.JPG
http://lh6.ggpht.com/_dfc1-DMKhn8/SQQTyuwfXLI/AAAAAAAABJs/E5BF5Ype_JU/Rubix%20Cubes%20%26%20Dogs%20010.jpg
http://lh5.ggpht.com/_dfc1-DMKhn8/SQQPXCS4sNI/AAAAAAAABIU/s7c0-tU18jA/Rubix%20Cubes%20%26%20Dogs%20048.jpg
http://lh4.ggpht.com/_tBkR4qZOXa0/Sb8aLUpr-vI/AAAAAAAAAQ8/55tir3tFWQQ/DSC_2723.JPG

File B

BM09_Starlight_-10.jpg
DSC02803.JPG
Rubix%20Pyramid.JPG
DSC02776.JPG
IMG_1367.JPG
DSCF1238.JPG
IMG_5049.JPG
Rubix%20Cubes%20%26%20Dogs%20010.jpg
Rubix%20Cubes%20%26%20Dogs%20048.jpg
DSC_2723.JPG

Output File (Desired Result)

http://lh6.ggpht.com/_0ga0wb33PoQ/S2ulPfVJBTI/AAAAAAAAAYs/1wxZhiPW1mw/
http://lh3.ggpht.com/_WpT8ajiz98w/R7aUf5YVUiI/AAAAAAAAAHA/00XJnAPH7TM/
http://lh4.ggpht.com/_jgRMak8S5Ec/R5M5Zp8WFsI/AAAAAAAACWY/wrYFlk3e72c/
http://lh3.ggpht.com/_WpT8ajiz98w/R7aT35YVUHI/AAAAAAAAADk/opr7BOANg2w/
http://lh6.ggpht.com/_dFjcay6XJ8I/SM8PGL2ytcI/AAAAAAAAFmA/jKMF6wiNyaU/
http://lh3.ggpht.com/_AMLQDMZjxvk/RvQc2eZhxZI/AAAAAAAABt0/QmXjo-opZqk/
http://lh5.ggpht.com/_Ehm252Yypl4/S1uM4m374JI/AAAAAAAAKyk/dzsIhkApavc/
http://lh6.ggpht.com/_dfc1-DMKhn8/SQQTyuwfXLI/AAAAAAAABJs/E5BF5Ype_JU/
http://lh5.ggpht.com/_dfc1-DMKhn8/SQQPXCS4sNI/AAAAAAAABIU/s7c0-tU18jA/
http://lh4.ggpht.com/_tBkR4qZOXa0/Sb8aLUpr-vI/AAAAAAAAAQ8/55tir3tFWQQ/

These links aren't static, they change everyday :slight_smile:

# cat A
[http/filename1212]
[http/filename12]
[http/filename44]
[http/filename565]
 
# cat B
filename1
filename3
filename4
filename5
filename2
filename44
filename6
 
for i in $(sed "" B)
>  do
>  sed -n "/$i\]$/p" A
>  done
[http/filename44]

I'm still having trouble with this :frowning:

fgrep -f FileB FileA |awk -F \/ '{$NF==""}1' 

Thanks rdcwayx, but that still outputs the full url address with the filename included.i'm trying to output the url without the filename included.

 
# for i in $(sed "" B); do sed -n "/$i/p" A | sed 's/[^/]*$//'; done
http://lh6.ggpht.com/_0ga0wb33PoQ/S2ulPfVJBTI/AAAAAAAAAYs/1wxZhiPW1mw/
http://lh3.ggpht.com/_WpT8ajiz98w/R7aUf5YVUiI/AAAAAAAAAHA/00XJnAPH7TM/
http://lh4.ggpht.com/_jgRMak8S5Ec/R5M5Zp8WFsI/AAAAAAAACWY/wrYFlk3e72c/
http://lh3.ggpht.com/_WpT8ajiz98w/R7aT35YVUHI/AAAAAAAAADk/opr7BOANg2w/
http://lh6.ggpht.com/_dFjcay6XJ8I/SM8PGL2ytcI/AAAAAAAAFmA/jKMF6wiNyaU/
http://lh3.ggpht.com/_AMLQDMZjxvk/RvQc2eZhxZI/AAAAAAAABt0/QmXjo-opZqk/
http://lh5.ggpht.com/_Ehm252Yypl4/S1uM4m374JI/AAAAAAAAKyk/dzsIhkApavc/
http://lh6.ggpht.com/_dfc1-DMKhn8/SQQTyuwfXLI/AAAAAAAABJs/E5BF5Ype_JU/
http://lh5.ggpht.com/_dfc1-DMKhn8/SQQPXCS4sNI/AAAAAAAABIU/s7c0-tU18jA/
http://lh4.ggpht.com/_tBkR4qZOXa0/Sb8aLUpr-vI/AAAAAAAAAQ8/55tir3tFWQQ/

Thx ygemici, I will let you know how this goes :slight_smile:

update:

fgrep -f FileB FileA  |awk 'BEGIN{FS=OFS="\/"}{$NF=""}1'