shell - Replace whole line containing a string using Sed

You can use the change command to replace the entire line, and the -i flag to make the changes in-place. For example, using GNU sed: sed -i '/TEXT_TO_BE_REPLACED/c\This line is removed by the admin.'... Read More

awk - How to remove double-quotes in jq output for parsing json files in bash?

Use the -r (or --raw-output) option to emit raw strings as output: jq -r '.name' <json.txt... Read More

linux - How do I delete all lines in a file starting from after a matching line?

If you don't want to print the matched line (or any following lines): sed -n '/The second line/q;p' inputfile This says "when you reach the line that matches the pattern quit, otherwise print each li... Read More

regex - How to output only captured groups with sed?

The key to getting this to work is to tell sed to exclude what you don't want to be output as well as specifying what you do want. string='This is a sample 123 text and some 987 numbers' echo "$strin... Read More

linux - sed insert line with spaces to a specific line

You can escape the space character, for example to add 2 spaces: sed -i "${line} i \ \ ${text}" $file Or you can do it in the definition of your text variable: text="\ \ hello world"... Read More

macos - '+' (one or more occurrences) not working with 'sed' command

If you are using GNU sed then you need to use sed -r which forces sed to use extended regular expressions, including the wanted behavior of +. See man sed: -r, --regexp-extended use extended... Read More

unix - Replace a string in shell script using a variable

If you want to interpret $replace, you should not use single quotes since they prevent variable substitution. Try: echo $LINE | sed -e "s/12345678/\"${replace}\"/g" assuming you want the quotes put i... Read More

shell - Remove part of path on Unix

If you wanted to remove a certain NUMBER of path components, you should use cut with -d'/'. For example, if path=/home/dude/some/deepish/dir: To remove the first two components: # (Add 2 to the numbe... Read More

bash - How to do a recursive find/replace of a string with awk or sed?

find /home/www \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i 's/subdomainA\.example\.com/' -print0 tells find to print each of the results separated by... Read More