Find and replace multiple line string using SSH - ssh

I'm only a light user of SSH and finding this one a struggle.
I have spammy code littered through my header.php and footer.php files in multiple directories that contains something like the following multiline string:
<div style="position:absolute;filter:alpha(opacity=0);opacity:0.001;z-index:10;”>
awful spammy shoes or whatever online
blah blah outlet
</div>
I'm looking to find and replace or delete the code from the files.
Not 100% sure of what linux tools are available (eg: perl) but happy to give recommendations a try.

I've come up with an answer with help from a mate.
If there is a standard beginning and a standard ending then sed comes to the rescue.
With an opening string of:
<div style="position:absolute;filter:alpha(opacity=0);opacity:0.001;z-index:10;">
and a closing string of:
</div>
Then the following finds and removes the opening string, closing string and anything contained within:
find . -type f -name "*.php" -exec sed -i '/<div style="position:absolute;filter:alpha(opacity=0);opacity:0.001;z-index:10;">/,/<\/div>/d' {} \;
Tested, works like a charm!

Related

Remove specific suffix from all files containing it

Long story short, OneDrive has taken all my files and renamed them to include the string "-DESKTOP-9EI0FN7" at the end of the file name, resulting in files such as:
myTextFile-DESKTOP-9EI0FN7.txt
myVideo-DESKTOP-9EI0FN7.mp4
So I'd like to write a batch script that finds all the files with that string in them, and renames them to remove the string, so:
myTextFile-DESKTOP-9EI0FN7.txt becomes myTextFile.txt
The problem is, I know nothing about writing batch files. Any advice?
Test out with this bad boy:
find . -type f -exec rename -n -e 's/(.*)\-DESKTOP\-9EI0FN7(.*)/$1$2/' {} \;
If the output satisfies you, remove the -n portion and it does actually apply the changes.
Good luck, sir!

How to extract the strings in double quotes for localization

I'm trying to extract the strings for localization. There are so many files where some of the strings are tagged as NSLocalizedStrings, and some of them are not.
I'm able to grab the NSLocalizedStrings using ibtool and genstrings, but I'm unable to extract the plain strings without NSLocalizedString.
I'm not good at regex, but I came up with this "[^(]#\""
and with the help of grep:
grep -i -r -I "[^(]#\"" * > out.txt
It worked, and all the strings were actually grabbed into a txt file, but the problem is ,
if in my code there is a line:
..... initWithTitle:#"New Sketch".....
I only expect the grep to grab the #"New Sketch" part, but it grabs the whole line.
So in the out.txt file, I see initWithTitle:#"New Sketch", along with some unwanted lines.
How can I write the regex to grab only the strings in double quotes ?
I tried the grep command with the regex mentioned in here, but it gave me syntax error .
For ex, I tried:
grep -i -r -I (["'])(?:(?=(\\?))\2.)*?\1 * > out.txt
and it gave me
-bash: syntax error near unexpected token `('
In xcode, open your project. Go to Editor->Export For Localization...It will create the folder of files. Everything that was marked for localization will be extracted there. No need to parse it yourself. It will be in the XML format.
If you wanna go hard way, you can then parse those files the way you're trying to do it now ?! It will also have Storyboard strings there too, btw.

Split a batch of text files using pattern

I have a directory of almost a thousand html files. Each file needs to be split up into multiple text files, based on a recurring pattern (a heading). I am on a windows machine, using GnuWin32 tools.
I've found a way to do this, for a single file:
csplit 1.html -b "%04d.txt" /"Words in heading"/ {*}
But I don't know how to repeat this operation over the entire set of HTML files. This:
csplit *.html -b "%04d.txt" /"Words in heading"/ {*}
doesn't work, and neither does this:
for %i in (*.html) do csplit *.html -b "%04d.txt" /"Words in heading"/ {*}
Both result in an invalid pattern error. Help would be much appreciated!
The options/arguments order is important with csplit. And it won’t accept multiple files. It’s help gets you there:
% csplit --help
Usage: csplit [OPTION]... FILE PATTERN...
I’m surprised your first example works for the single file. It really should be changed to:
% csplit -b "%04d.txt" 1.html "/Words in heading/" "{*}"
^^^^^^^^^^^^^ ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^
OPTS/ARGS FILE PATTERNS
Notice also that I changed your your quoting to be around the arguments. You probably also need to have quoted your last "{*}".
I’m not sure what shell you’re using, but if that for-loop syntax is appropriate, then the fixed command should work in the loop.

How to make a variable out of the output of a unix command?

I'm trying to have a variable $totalLines that stores the total lines in a file (given as input $1).
I'm trying to do something like this:
totalLines= grep -c *.* $1
But Unix doesn't like that.
I've tried enclosing it in paranthesis, square brackets, and (), but that doesn't work either. This has got to be super simple but I'm searching for the answer around the web and not finding a page or forum that clearly states it.
Sorry to trouble you guys with such an easy one.
There are two ways to achieve it:
totalLines=$(grep -c *.* $1)
or
totalLines=`grep -c *.* $1`
Like:
totalLines=$(grep -c *.* $1)

wget without any headers

I would like to get the files without headers. I have tried many things like
wget --header="" http://xxxxx.xxxxxx.xx
How can I get any files without headers?
This doesn't quite answer the question, but I got here by looking up "remove default header wget" so I'll put in my 2 cents.
You can remove the User-Agent header with -U "". This was useful for me because the Geometry Dash servers will reject your request if it has a user agent.
Could you assign the output of wget to a string, then use something else to process it to drop headers (or parse them out of the text)?
For example, using bash and grep, you can store the html from a webpage as a string, then use grep to extract the text in the <body> section:
w1=$(wget --quiet --output-document - www.example.com)
echo $w1 | grep --only-matching "<body>.*</body>"
which gives the output below (I have added some newlines to improve how it displays here):
<body> <div>
<h1>Example Domain</h1> <p>
This domain is established to be used for illustrative examples in documents.
You may use this domain in examples without prior coordination or asking for
permission.
</p> <p>
More information...</p>
</div> </body>