arun kumar
arun kumar

Reputation: 41

How to replace a cron in a shell script using sed?

I need to replace a cron entry in a file using sed or awk.

tried this : didnt work

sed -i 's/0 0 * * 0/0 1 * * 1/g' script.sh

script.sh

#!/bin/bash

mkdir -p .github/workflows

cd .github/workflows
touch semgrep.yml

cat << EOF > semgrep.yml
name: Semgrep
on:
  pull_request: {}
  push:
    branches:
      - master
      - main
    paths:
      - .github/workflows/semgrep.yml
  schedule:
    - cron: '0 0 * * 0'
jobs:
  semgrep:
    name: Static Analysis Scan
    runs-on: ubuntu-18-04

Kindly help me with the same .

Upvotes: 0

Views: 219

Answers (1)

pmf
pmf

Reputation: 36033

Using mikefarah/yq to edit the file in place (-i):

yq -i '.on.schedule[].cron = "0 1 * * 1"' semgrep.yml

would turn a semgrep.yml containing

name: Semgrep
on:
  pull_request: {}
  push:
    branches:
      - master
      - main
    paths:
      - .github/workflows/semgrep.yml
  schedule:
    - cron: '0 0 * * 0'
jobs:
  semgrep:
    name: Static Analysis Scan
    runs-on: ubuntu-18-04

into one containing

name: Semgrep
on:
  pull_request: {}
  push:
    branches:
      - master
      - main
    paths:
      - .github/workflows/semgrep.yml
  schedule:
    - cron: '0 1 * * 1'
jobs:
  semgrep:
    name: Static Analysis Scan
    runs-on: ubuntu-18-04

Upvotes: 2

Related Questions