Friday, 16 October 2020

How to replace multiple patterns at once with sed?

Maybe something like this:

sed 's/ab/~~/g; s/bc/ab/g; s/~~/bc/g' 


from : https://stackoverflow.com/questions/26568952/how-to-replace-multiple-patterns-at-once-with-sed

Thursday, 16 July 2020

How to read a file into a variable in shell?

In cross-platform, lowest-common-denominator sh you use:

#!/bin/sh
value=`cat config.txt`
echo "$value"

In bash or zsh, to read a whole file into a variable without invoking cat:

#!/bin/bash
value=$(<config.txt)
echo "$value"

Bash command to sum a column of numbers

while read -r num; do ((sum += num)); done < inputfile; echo $sum

from : https://stackoverflow.com/questions/3096259/bash-command-to-sum-a-column-of-numbers

How to check if a file contains a specific string using Bash

 In case if you want to check whether file does not contain a specific string, you can do it as follows. if ! grep -q SomeString "$File...