1

I currently have a script (below) that flags values above a given threshold, outputs this value and the following n rows, and replaces these rows with Nan values in the original file.

threshold=5
eventperiod=3

# Flag first occurrence with value over threshold and store the row number as a variable
startrow="$(awk '{print NR " " $1}' tmp.ascii | awk -v threshold=$threshold '$2 > threshold''{print $1;exit}')"
endrow="$(($startrow + $eventperiod - 1))"

# Output range of rows as event
sed -n -e "$startrow,$endrow p" -e "$endrow q" tmp.ascii > output"$startrow".ascii
# Replace rows with Nan value
sed -i "${startrow},${endrow}s/.*/Nan/" tmp.ascii

Example input (tmp.ascii):

 1
 3
 1
 200
 100
 1
 3
 0
 2
 1
 400
 150
 200
 2
 1
 1
 2

Example output event:

 200
 100
 1

Output updated file:

 1
 3
 1
 Nan
 Nan
 Nan
 3
 0
 2
 1
 400
 150
 200
 2
 1
 1
 2

Here, you can see that there is still a value above the threshold in the file (400).

I would like to be able to run this iteratively, such that once the rows have been removed, if there is another occurrence over the threshold in the same file, it will run the sequence of commands again. Is this possible?

Thanks.

2

You can use while, for or until to run the same instructions multiple times. I recommend you to create a function with your code and call it multiple times until all the value are replaced.

For example, a possible solution based on your example:

threshold=5
eventperiod=3

replace_next_value() {
  # Flag first occurrence with value over threshold and store the row number as a variable
  # We need to check also that the input is a number to skip the Nans
  startrow="$(awk '{print NR " " $1}' tmp.ascii | awk -v threshold=$threshold '$2 ~ /^[0-9]+$/ && $2 > threshold {print $1; exit}')"
  [ -z "$startrow" ] && return 1 # No more rows to replace
  endrow="$(($startrow + $eventperiod - 1))"

  # Output range of rows as event
  sed -n -e "$startrow,$endrow p" -e "$endrow q" tmp.ascii > output"$startrow".ascii
  # Replace rows with Nan value
  sed -i "${startrow},${endrow}s/.*/Nan/" tmp.ascii
  return 0
}

# Call the function until it returns 1
while replace_next_value ; do continue; done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.