I have a few files that contain IP addresses. I’m creating a script and have to figure out how to create a new user file with an IP address that is based off the file created before it. If the last file contains an IP of A.B.C.D the new file needs to be A.B.C.(D+4).
I think I need to use the ‘sed’ and ‘awk’ commands, but haven’t been able to get anything working. How would I go about writing this part of the script?
Here’s something to get you started: suppose there is a file called
inputlooks like this:Input: contents of
inputYou can do on the cmdline:
Explanation on what awk is doing here:
awk '...'– invoke awk, a tool used primarily for line-by-line manipulation of files, the stuff enclosed by single quotes are instructions to awk.BEGIN{FS=OFS="."}– tell awk to use.as the delimiter for both input and output.FSstands for “Field Separator”{$4=$4+4; print}–$4means the 4th field. Since.is the delimiter,Dcorresponds to the 4th field and we add the integer value4to the 4th field. Theprinthere is just short hand for printing the entire line.input– name the input file as argument to awk; save a cat> output– redirect the output to a file so you can inspect them for any issues before making the user files based on it.Output: contents of
outputAnd then you can read
outputone line at a time to create new user files as needed, maybe another script with something along the lines of:(and adjust it to your needs)
Finally, as long as you understand what’s going on in the above, you can skip the
outputfile altogether and just do this all in a one-liner: