0

So, i want to filter some values using awk, to insert them to the file after this so that they don't appear in the script file, for this im trying to use system grep function

and im getting this output everytime i use the script

/sbin/ufw deny from 187.210.68.101
to any
/sbin/ufw deny from 45.88.221.190
to any
/sbin/ufw deny from 5.253.235.40
to any

but i need

/sbin/ufw deny from 187.210.68.101 to any
/sbin/ufw deny from 45.88.221.190 to any
/sbin/ufw deny from 5.253.235.40 to any  

also, my 4 string is getting removed

lines brokes everytime

awk '{ if (system("grep -L "$1" banned.txt")) print "/sbin/ufw deny from "$1 to any" "; }' /usr/local/test/susp.txt >> /usr/local/test/script.sh

Im trying to fix it of course, so that the ufw deny rule is displayed on the same line with all arguments without any bugs, and does not output anywhere there banned.txt because it shouldn't be written there

  • 3
    Please provide a sample of your actual input file, not just an image of the output – Dan Bonachea Dec 08 '22 at 22:50
  • 1
    remove the image and instead cut-n-paste 5-10 lines into the question; please provide samples from all input files – markp-fuso Dec 08 '22 at 22:51
  • 1
    also, update the question to show the expected output (corresponding to the sample inputs) – markp-fuso Dec 08 '22 at 23:22
  • @Cyrus how you html help me with the bash? – Conspiracy Dec 08 '22 at 23:34
  • @Conspiracy: you can use markdown or/and HTML syntax to format your question. – Cyrus Dec 09 '22 at 00:08
  • 1
    Regarding `Using system function in the bash` - the system function is in your awk script, not bash. Your question really has very little, if anything, to do with bash. Having said that, a shell such as bash is a tool designed to sequence calls to other tools while awk is a tool designed to manipulate text - you're trying to use awk as if it were a shell which is generally a bad idea. – Ed Morton Dec 09 '22 at 00:44
  • 1
    So far you've shown us output you get and output you want but you haven't shown is the input you're parsing to get that output - [edit] your question to add that input so we can help you. – Ed Morton Dec 09 '22 at 00:48

1 Answers1

1

grep -L prints the names of the files that matched the regular expression, so it prints banned.txt whenever it finds a match. That output is being included in the awk output, so it gets written to the output file.

If you just want to know if grep found a match, use grep -q. This suppresses the output, so you can just test the exit status.

Using grep for this seems like overkill. It looks like you just want to find all the lines that are in common between the two files. You can do this with the comm command.

comm -12 <(sort banned.txt) <(sort /usr/local/test/susp.txt) | awk '{print "/sbin/ufw deny from ", $0, "to any"}' >> /usr/local/test/script.sh
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you very much for you answer, but i still have problem with your code, to any on other line https://i.imgur.com/Qz2r7yc.png – Conspiracy Dec 08 '22 at 23:04
  • Make sure your files have Unix newlines, not DOS CRLF. Use `dos2unix` to fix the files. – Barmar Dec 08 '22 at 23:05
  • I was missing a space before `"to any"`, I've updated the answer. – Barmar Dec 08 '22 at 23:06
  • Okay, it works now, but i don't know, everytime i use this, my line in end of file removes, i should have 4 strings in results, but i have 3 – Conspiracy Dec 08 '22 at 23:10
  • @Conspiracy: please stop posting images of text. Thank you. – Cyrus Dec 08 '22 at 23:10
  • Unix text tools generally require that all files end with a newline. If the last line is missing a newline, the last line may be missed. @Conspiracy – Barmar Dec 08 '22 at 23:12