Skip to content

Instantly share code, notes, and snippets.

@claudius108
Last active June 8, 2024 06:16
Show Gist options
  • Save claudius108/baeba002a79d4f01018339c8f2475b31 to your computer and use it in GitHub Desktop.
Save claudius108/baeba002a79d4f01018339c8f2475b31 to your computer and use it in GitHub Desktop.
Delete line with sed

Delete recursively line containing string

find ./ -name "*.xml" -exec sed -i '/<p> <\/p>/d' {} \;

Find recursively string in files

find . -name "*" -print0 | xargs -0 grep -l "string"

Delete recursively one line following the line containing string, exclusive

find ./ -name "A*.xml" -exec sed -i '/string/{n;d}' {} \;

Delete recursively five lines following the line containing string, inclusive

find . -name "A*.xml" -exec sed -i -e '/string/,+5d' {} \;

Delete only the first line containing string

find . -name "A*.xml" -exec sed -i -e '0,/string/{/string/d}' {} \;

Delete empty lines

find . -name "A*.xml" -exec sed -i -e '/^$/d' {} \;

replace a string inplace for all files in a folder

sed -i 's|replaceable|replacing|g' *

replace a string inplace for all files in a folder and all subfolders

find ./ -type f -name "*.xml" -exec sed -i 's|replaceable|replacing|g' {} \;

replace a regex inplace for all files in a folder and all subfolders

find ./ -type f -exec sed -i -e 's|replaceable|replacing|g' {} \;

####replace text content of xml tag find . -name "A*.xml" -exec sed -i -E 's|(vowels">)([^<]+<)|\1\U\2|' {} \;

find ./ -type f -exec sed -i -e 's|apple|orange|g' {} \;

replace text content of xml tag

find . -name "A*.xml" -exec sed -i -E 's|(vowels">)([^<]+<)|\1\U\2|' {} \;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment