#!/bin/awk -f
BEGIN {i=1;file="modified.txt"}
{
if ($0 !~ /^DS:/) {print $0 >> file} else {
if ($0 ~ /^DS:/) {print "DS: ",i >> file;if (i==8) {i=1} else {i++}};
}
}
END {gzip file}
Can someone explain to me how this above script works, I got it from a friend but not able to understand what is happening inside.
when is do this ./script.sh file.txt This script is changing the DS: * value in the file sequencially from 1to8. But I to like to understand how this script is working.
# BEGIN Block
BEGIN {
i = 1 # Initialize i = 1
file = "modified.txt" # Initialize file = modified.txt
}
{
if ( $0 !~ /^DS:/ ) # If record does not start with DS:
print $0 >> file # Write record to modified.txt
else # If record does start with DS:
print "DS: ", i >> file # Write DS: i value to modified.txt
if ( i == 8) # If i value == 8
i = 1 # Reset i value to 1
else # If i value != 8
i++ # Increment i value by 1
}