Home > Blockchain >  Using system function in the bash, causes problem with output
Using system function in the bash, causes problem with output

Time:12-09

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

CodePudding user response:

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

CodePudding user response:

This might help with GNU sed:

sed -zE 's/([0-9])\n/\1 /g' file

Output:

/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

See: The Stack Overflow Regular Expressions FAQ

  • Related