Sed

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the unix category.

Last Updated: 2024-04-26

Sed

How to run multiple substitutions

Just separate them by ; in the same sed command string.

$ echo "2 + 2 = foo_bar" | sed 's/+/-/g; s/_/\&/g';
# 2 - 2 = foo&bar

How to substitute a line in package.json file in place using regex

Say the original line is

{
  "database": "bitbucket:someone/database#0.0.1"
}
sed -i '' 's/database#[0-9]*\.[0-9]*\.[0-9]*/database#0.0.2/g' filename

Explanation:

Print just the 2nd line from piped input

The base amount is sed '2p'. But the problem with this is that it also shows all the original input:

ps aux | grep gunicorn | sort -k 2 -t " " | sed '2p'
jackkinsella     65762   0.0  0.1 408897152  35200 s004  S+    2:43PM   0:00.23 /Users/jackkinsella/.virtualenvs/morphmarket/bin/python /Users/jackkinsella/.virtualenvs/morphmarket/bin/gunicorn mysite.asgi:application --pythonpath mysite --timeout 15 --max-requests 10000 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000 --workers 4
jackkinsella     65763   0.1  0.5 410154144 182496 s004  S+    2:43PM   0:02.31 /Users/jackkinsella/.virtualenvs/morphmarket/bin/python /Users/jackkinsella/.virtualenvs/morphmarket/bin/gunicorn mysite.asgi:application --pythonpath mysite --timeout 15 --max-requests 10000 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000 --workers 4
jackkinsella     65763   0.1  0.5 410154144 182496 s004  S+    2:43PM   0:02.31 /Users/jackkinsella/.virtualenvs/morphmarket/bin/python /Users/jackkinsella/.virtualenvs/morphmarket/bin/gunicorn mysite.asgi:application --pythonpath mysite --timeout 15 --max-requests 10000 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000 --workers 4
jackkinsella     65764   0.2  0.5 410155168 182688 s004  S+    2:43PM   0:02.10 /Users/jackkinsella/.virtualenvs/morphmarket/bin/python /Users/jackkinsella/.virtualenvs/morphmarket/bin/gunicorn mysite.asgi:application --pythonpath mysite --timeout 15 --max-requests 10000 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000 --workers 4
jackkinsella     65765   0.1  0.5 410153120 181904 s004  S+    2:43PM   0:02.65 /Users/jackkinsella/.virtualenvs/morphmarket/bin/python /Users/jackkinsella/.virtualenvs/morphmarket/bin/gunicorn mysite.asgi:application --pythonpath mysite --timeout 15 --max-requests 10000 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000 --workers 4
jackkinsella     65766   0.2  0.5 410154144 183648 s004  S+    2:43PM   0:02.30 /Users/jackkinsella/.virtualenvs/morphmarket/bin/python /Users/jackkinsella/.virtualenvs/morphmarket/bin/gunicorn mysite.asgi:application --pythonpath mysite --timeout 15 --max-requests 10000 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000 --workers 4
jackkinsella     66390   0.0  0.0 408628368   1648 s001  S+    2:46PM   0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox gunicorn
 ~/Code/mm/morphmarket   master ●  ps aux | grep gunicorn | sort -k 2 -t " " | sed -n '2p'
jackkinsella     65763   0.1  0.5 410154144 182496 s004  S+    2:43PM   0:02.34 /Users/jackkinsella/.virtualenvs/morphmarket/bin/python /Users/jackkinsella/.virtualenvs/morphmarket/bin/gunicorn mysite.asgi:application --pythonpath mysite --timeout 15 --max-requests 10000 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000 --workers 4

To suppress this -- which you probably always want -- you need the -n flag.

 ps aux | grep gunicorn | sort -k 2 -t " " | sed -n '2p'
jackkinsella     65763   0.1  0.5 410154144 182496 s004  S+    2:43PM   0:02.26 /Users/jackkinsella/.virtualenvs/morphmarket/bin/python /Users/jackkinsella/.virtualenvs/morphmarket/bin/gunicorn mysite.asgi:application --pythonpath mysite --timeout 15 --max-requests 10000 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000 --workers 4