Bash Scripting - Output as Multiple Files
Ok here is what I am trying to do. I have wrote a 1 line command that parses a file, locates the IP Address in the file and then trims the output the way I want it, and then sorts numerically and by uniqueness and then >> appends to output.txt
I can get all the IP's into 1 file "output.txt", but what I am really looking for is some type of way to create a text file, for each IP it finds labeled xxx.xxx.xxx.xxx.txt and also put that ip address into that file.. xxx.xxx.xxx.xxx = the ip address it finds Can anyone offer suggestions on the best approach for this...? Thanks |
If you have the IP stored in a variable (using command substitution) you can simply do
Code:
echo $ip > $ip.txt |
Quote:
cat /var/log/secure |grep "Failed password" | awk -F'from' '{ print $2 } ' | cut -d" " -f2 | sort -n -u > list.txt |
awk has all the grep and cut functionality, so that your line can be condensed into:
Code:
awk ' |
uh.... ok? lol I really don't know much awk, and I'm just learning to script. If you would not mind being able to break that down, so I can understand the logic and flow. If not I can research the web on what is actually being stated there. I mean I am able to break down some of it to understand. =)
awk ' <-- that seems to start the awk statement /Failed password/ { <-- parsing ID ip = gensub(/.*from ([[:digit:].]+) .*/,"\\1","g") <-- specify the variable and next deliemiter... dont know what the rest of the line is. if ( ! _[ip]++ ) { <- if statement for the variable ip print ip > (ip ".txt") <-- print each variable with to a txt with the varable as file name print ip <-- ? } }' /var/log/secure | sort -n > list.txt <-- dumping secure log to list.txt file? sorry looks really sloppy |
Well.. here is my explanation: an awk rule is made of
Code:
pattern { action } First we have to extract the IP address from the line. I don't know what the line exactly is in your secure file, but I can guess based on your code. The gensub function can do substitutions in a string. Here we want to ignore all the parts of the string but the IP address: Code:
ip = gensub(/.*from ([[:digit:].]+) .*/,"\\1","g") Now we have extracted the IP address with a reasonable confidence and we want either to write it into a file (named as the IP address itself) and to add it to a complete list. Since we'll use shell redirection later, we send it to standard output. The first task is accomplished by: Code:
print ip > (ip ".txt") Code:
print ip First take in mind that in awk true is any number different from 0 or any non-empty string, whereas false is 0 or the null string. Here Code:
_[ip]++ Code:
++_[ip] However we want a true condition only the first time the IP is encountered in order to print it. Hence we have to invert the logical expression using the not operator (in awk is an exclamation mark): Code:
if ( ! _[ip]++ ) Finally, following your code we want to sort numerically the output (note that we already managed for duplicates) and write it to the list.txt file: Code:
... | sort -n > list.txt |
Quote:
|
Quote:
|
Quote:
Code:
_[ip] Code:
_[ip] = 0 Code:
_[192.168.0.1] = 0 Code:
_[192.168.0.1] = 1 |
Ok, that makes it a lot clearer. I appreciate your advice and explanation on this subject! =) I have been going over the GNU-awk page. Defiantly something I'm going to take deeper, because of its ability with text parsing and output!
Thanks again! =) |
Quote:
|
Ha! =) It's cool, just trying to grasp the concept =)
|
Do you fully understand it now?
|
Quote:
|
That's good. It took me a while to get awk and gawk down to a science....
|
All times are GMT -5. The time now is 12:02 PM. |