How to traverse all subdirectories with sed and feed xgettext - sed

I have this console command to make substitute in all PHP files in the directory. Then it feeds the xgettext program.
sed "s/\/\/_/_/g" *.php | xgettext -o output.pot --language=PHP - --from-code=UTF-8
How to change it so that sed traverse all PHP files in all subdirectories?

Related

Just trying to delete lines of several .txt files - but keep getting SED error

So, I am trying to delete the first 23 lines of many .txt files. This is what I am currently doing:
sed -i -e 1,23d * .txt
but it gives me a weird error:
sed: 1: "1,23": command expected
I have no idea what to do. _
Like this:
sed -i -e '1,23d' *.txt
# ^ mandatory 'd'
# ^
# no space between * and .txt
The original question (before editing) showed:
sed -i -e 1,23d * .txt
but it gives me a weird error:
sed: Applications: in-place editing only works for regular files
The space between * and .txt was a mistake, but is an explanation of the weird error: It looks for all files as well as for the hidden file .txt.
Linux considers the directory Application as a file, but sed does not work on files.
The directory Application suggests that the files might come from Windows.
Another question is why echo *.txt don't show the txt files Mia was expecting. A logical explanation is that the files originated from Windows and Mia doesn't know that Linux is case sensitive. Files like A.TXT and b.Txt don't match *.txt.
When all txt files end with TXT, you can do
sed -i -e '1,23d' *.TXT
When you have a mix of upper- and lower case, the easiest way is
sed -i -e '1,23d' *.[tT][xX][tT]

Bash Script to append for specific file extensions in a directory

How do I prepends a special character in front of all the lines in all .txt files in my directory? Im new to writing bash scripts and having trouble doing this. I only know of using the grep function but thats only to search for keyword.
For now, I have this,
sed -i 's/^/#/' Machine1.txt
However, this is only for that specific .txt file. I want to do this for all files with a .txt extension in my directory. There are other extensions like .tar, .rpm, .sh files which I want to ignore. Thank you!
Just give a wildcard filename argument.
sed -i 's/^/#/' *.txt
you can use for loop.
For example
cd your_folder
for f in *.txt; do
sed -i 's/^/#/' "$f";
done

Search and replace with grep and perl

I need to replace "vi_chiron" with "vi_huracan" from all the files below. I am using the following command line and also have changed the permission to full access for all the underlying files:
grep -ri "vi_chiron" ./ | grep -v Header | xargs perl -e "s/vi_chiron/vi_huracan/" -pi
I am getting the error:
"Can't open ./build/drivers_file.min:#: No such file or directory.
" and many other similar errors. Any idea why ? Below is the permission for the file:
ll build/drivers_file.min
-rwxrwxrwx 1 ask vi 5860 Mar 13 12:07 build/drivers_file.min
You can change your command in the following ways:
grep -ril "vi_chiron" . | xargs grep -vl Header | xargs perl -e "s/vi_chiron/vi_huracan/" -pi
or
find . -type f -exec grep -ril "vi_chiron" | xargs grep -vl Header | xargs perl -e "s/vi_chiron/vi_huracan/" -pi
In order to manipulate all files that do not contain Header and change vi_chiron into vi_hurican.
If I were you, I would simplify the chain and do it the other way around:
grep -rvl Header . | xargs sed -i.bak 's/vi_chiron/vi_hurican/'
If you are confident enough change the -i.bak into -i in order to have sed not taking any backups.
Also note that your change and replace is not global, if you want to put it globally use: sed -i.bak 's/vi_chiron/vi_hurican/g' instead.
'-i[SUFFIX]' '--in-place[=SUFFIX]'
This option specifies that files are to be edited in-place. GNU
'sed' does this by creating a temporary file and sending output to
this file rather than to the standard output.(1).
This option implies '-s'.
When the end of the file is reached, the temporary file is renamed
to the output file's original name. The extension, if supplied, is
used to modify the name of the old file before renaming the
temporary file, thereby making a backup copy(2)).
This rule is followed: if the extension doesn't contain a '*', then
it is appended to the end of the current filename as a suffix; if
the extension does contain one or more '*' characters, then _each_
asterisk is replaced with the current filename. This allows you to
add a prefix to the backup file, instead of (or in addition to) a
suffix, or even to place backup copies of the original files into
another directory (provided the directory already exists).
If no extension is supplied, the original file is overwritten
without making a backup.
source: https://www.gnu.org/software/sed/manual/sed.txt

Appending URL to output file with wget

I'm using wget to read a batch of urls from an input file and download everything to a single output file, and I'd like to append each url before its downloaded content, anyone knows how to do that?
Thanks!
afaik wget does not directly support the use case you are envisioning. however, using standard tools, you can emulate this feature.
we will proceed as follows:
call wget with logging enabled
let sed process the log executing the script detailed below
execute the transformation result as a shell/batch script
conventions: use the following filenames:
wgetin.txt: the file with the urls to fetch using wget
wgetout.sed: sed script
wgetout.final: the final result
wgetass.sh/.cmd: shell/batch script to assemble the downloaded files weaving in the url data
wget.log: the log file of the wget call
Linux
the sed script (linux):
# delete lines _not_ matching the regex
/^\(Saving to: .\|--[0-9: \-]\+-- \)/! { d; }
# turn remaining content into something else
s/^--[0-9: \-]\+-- \(.*\)$/echo '\1\n' >>wgetout.final/
s/^Saving to: .\(.*\).$/cat '\1' >>wgetout.final/
the command line (linux):
rm wgetout.final | rm wgetass.sh | wget -i wgetin.txt -o wget.log | sed -f wgetout.sed -r wget.log >wgetass.sh | chmod 755 wgetass.sh | ./wgetass.sh
Windows
the syntax for windows batch scripts is slightly different. of course, the windows ports of wget and sed have to be installed first.
the sed script (windows):
# delete lines _not_ matching the regex
/^\(Saving to: .\|--[0-9: \-]\+-- \)/! { d; }
# turn remaining content into something else
s/^--[0-9: \-]\+-- \(.*\)$/echo "\1" >>wgetout.final/
s/^Saving to: .\(.*\).$/type "\1" >>wgetout.final/
the command line (windows):
del wgetout.final && del wgetass.cmd && wget -i wgetin.txt -o wget.log && sed -f wgetout.sed -r wget.log >wgetass.cmd && wgetass.cmd

Remove string/script from all files (recursive)

One of my websites has been hacked, all the index.html and index.php files have been infected with a certain Javascript. I would like to have a unix command to remove this script from all files.
Script is here: http://pastie.org/private/6osrvd5zhphe372gblrc6w
I am trying to figure this out with sed but no luck so far
Thanks!
sed -i 's/<script>.*<\/script>//' fileName
will remove the tag script and all its content.
This works if you only have one <script> tag.
If you haven't only one, extend it with try keyword in the following way
sed -i 's/<script>try.*<\/script>//' fileName
Edit
If you want to do it on all files in a recursive way, you can use a find command like this:
find . -name "index.html" -print | xargs sed -i 's/<script>try.*<\/script>//' fileName
where . is the current directory
You can try this
find src/ -name "index.html" -print | xargs sed -i 's/<script>try{document.body++}catch(dgsgsdg){zxc=12;ww=window;}if(zxc).*<\/scri‌​pt>//
perl -pi -e 's/<script>.*<\/script>//g' index.html