Skip to content

Instantly share code, notes, and snippets.

@rtanikella
Last active November 9, 2018 19:35
Show Gist options
  • Save rtanikella/b98fc5bb3b641a588e659038031ad348 to your computer and use it in GitHub Desktop.
Save rtanikella/b98fc5bb3b641a588e659038031ad348 to your computer and use it in GitHub Desktop.
Use this to search and replace text within Classic Salesforce Knowledge Articles. It is globally replaces a string using a perl regular expression and writes to STDOUT only those articles that have been changed.
#!/bin/bash
if [ ! -n "$1" ]
then
help_text=$( cat <<ENDxxx
Use this to search and replace text within Classic Salesforce Knowledge
Articles. It is globally replaces a string using a perl regular expression
and writes to STDOUT only those articles that have been changed.
Usage: $0 "/search/replace-regexp/" input-file.csv [prefix] [--debug]
where
"search/replace-regexp" is a (perl) regular expression, including the
search expression and replacement expression. This expression
must be quoted and in the form "/search/replace/",
where "search" is the regexp to be found, "replace" is what
will replace it. This will become a perl search/replace command
of the form 's/search/replace/g'. The regular expression
should be able to support any perl regular expression.
See https://perldoc.perl.org/perlrequick.html for details
about regular expression syntax
input-file.csv is an extract of Classic Knowledge Articles from
Salesforce in the form of "Id","Article_content". The expectation
is that each record begins with the "Id" column
prefix is an optional string to prepend to the final output file name.
If none is provided then this will be 'Replaced_'
--debug outputs only the final perl expression to be run without
actually doing anything.
$0 will
remove all line breaks (collapsing into a single line, outputting
intermediate temp file chomped.csv)
introduce line breaks ahead of head article Id (making the CSV one article
per line, outputting intermediate temp file one-per-line.csv)
invoke the aforementioned search and replace, writing the final output
to a new file entitled as Replaced-input-file.csv (the input file name
prefixed with 'Replaced-', or with the prefix text if provided.)
ENDxxx
)
echo "$help_text"
exit
else
REGEXP=$1
fi
if [ ! -n "$2" ]
then
echo "You must provide an input CSV file."
exit
else
INFILE=$2
fi
if [ ! -n "$3" -o '--debug'="$3" ]
then
OUTNAME="Replaced_$2"
else
OUTNAME="$3$2"
fi
if [ -n "$4" -o '--debug'="$3" ]
then
DEBUG=1
fi
PERLSCRIPT="if(\$_ =~ s${REGEXP}g){print;}"
PERLINVOKE="perl -ne '$PERLSCRIPT' one-per-line.csv > \"$OUTNAME\""
if [ '1'="$DEBUG" ]
then
echo $PERLINVOKE
exit
else
perl -pe "chomp;" "$INFILE" > chomped.csv
perl -pe 's/"ka1/\n"ka1/sg;' chomped.csv > one-per-line.csv
eval $PERLINVOKE
fi
exit
#perl -ne 'if($_ =~ s/\/na33.salesforce.com\//\/clarivateanalytics.my.salesforce.com\//g){print;}' one-per-line.csv > "$OUTNAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment