LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   need a php script to format and display a select amount of text from a file (https://www.linuxquestions.org/questions/linux-software-2/need-a-php-script-to-format-and-display-a-select-amount-of-text-from-a-file-699418/)

steve51184 01-23-2009 01:10 PM

need a php script to format and display a select amount of text from a file
 
hey i need a php script to format and display a select amount of text from a file or a cron job that'll do it (with grep etc) and out put to a file but whatever is easier

i want to display all the banned ip's i have in my .htaccess file

i have a lot of lines in my .htaccess file but i have a line that read like this:

Code:

RewriteCond %{REMOTE_ADDR} (123\.123\.123\.123|123\.123\.123\.123|123\.123\.123\.123)$
the 3 test ip's are 123.123.123.123 and i want the "\" stripping and the "|" removing and each ip to be on it's own line like this:

123.123.123.123
123.123.123.123
123.123.123.123

i'm not bothered if this is a php script that'll read the .htaccess file and display the output or a cron job that'll run every x mins and output it to a .txt file but i'd prefer a php script

can anyone help me with this please?

TB0ne 01-23-2009 02:14 PM

Quote:

Originally Posted by steve51184 (Post 3418899)
hey i need a php script to format and display a select amount of text from a file or a cron job that'll do it (with grep etc) and out put to a file but whatever is easier

i want to display all the banned ip's i have in my .htaccess file

i have a lot of lines in my .htaccess file but i have a line that read like this:

Code:

RewriteCond %{REMOTE_ADDR} (123\.123\.123\.123|123\.123\.123\.123|123\.123\.123\.123)$
the 3 test ip's are 123.123.123.123 and i want the "\" stripping and the "|" removing and each ip to be on it's own line like this:

123.123.123.123
123.123.123.123
123.123.123.123

i'm not bothered if this is a php script that'll read the .htaccess file and display the output or a cron job that'll run every x mins and output it to a .txt file but i'd prefer a php script

can anyone help me with this please?

You can check out the explode function (http://us2.php.net/manual/en/function.explode.php), which should be able to do what you're looking for in PHP.

However, in SED via a small bash script, it's easy too:

sed 's/\\//g' - Will strip out the '\' character.
sed 's/\|/\n/g' - Changes the '|' to a newline.

Check, though...doing this from memory..untested..your mileage may vary. :)

steve51184 01-23-2009 02:24 PM

So can I use grep to strip that line from the file to a new file then use SED to strip the junk?

If so can anyone make me a little script please?

TB0ne 01-23-2009 02:52 PM

Quote:

Originally Posted by steve51184 (Post 3418967)
So can I use grep to strip that line from the file to a new file then use SED to strip the junk?

If so can anyone make me a little script please?

Yes, you can use grep to burp that line through sed. And I'm sure there are lots of folks on here that CAN make you a script. The question is: are they willing to do work for you, for free?

A bash shell script tutorial can be found here http://www.freeos.com/guides/lsst/. Lots more tutorials are on Google, and you were provided with reference to the appropriate PHP function, which you had originally asked about.

steve51184 01-23-2009 03:24 PM

Your right I have all the info I need so I'll give it a go thank you

steve51184 01-24-2009 08:23 PM

right i had a look but explode for php makes no sense to me.. so i need to ask for more help please

TB0ne 01-26-2009 11:04 AM

Quote:

Originally Posted by steve51184 (Post 3420253)
right i had a look but explode for php makes no sense to me.. so i need to ask for more help please

What help do you need? Post what you've written so far, and what error(s) you're getting, and we can try to help.

You originally asked for PHP script, and the explode directive is fairly straightfoward. Take the variable:

$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";

...and explode it. The first field is the character you want to delimit the fields by, in this case, a space. Can be ANY character, though....

$pieces = explode(" ", $pizza);

...then print the value out. In this example, it's printing out the first variable (starts at zero).....

echo $pieces[0]; // piece1

That's it....loop through the array until you're done.

The BASH script tutorial I mentioned has ways of doing it via SED/AWK as well.

steve51184 01-26-2009 11:19 AM

well that's the thing i don't know php so i haven't written anything :\

TB0ne 01-26-2009 11:40 AM

Quote:

Originally Posted by steve51184 (Post 3421784)
well that's the thing i don't know php so i haven't written anything :\

Ok, in your first post you mentioned PHP. I assumed that meant you had knowledge of it. Have you gone through the bash scripting tutorial? Looked at ANY of the thousands of example scripts you can find via Google?

Look at the docs for sed and awk.

steve51184 01-26-2009 11:44 AM

i have knowledge about php as in i can edit it very well i just can't make a whole entire script with it and yes i've looked at the docs for sed and awk but again makes no sence to me as i don't/can't make scripts

i'm sure it wouldn't take to long for someone to code me up a little script :(

TB0ne 01-26-2009 01:59 PM

Quote:

Originally Posted by steve51184 (Post 3421805)
i have knowledge about php as in i can edit it very well i just can't make a whole entire script with it and yes i've looked at the docs for sed and awk but again makes no sence to me as i don't/can't make scripts

i'm sure it wouldn't take to long for someone to code me up a little script :(

You're right, it wouldn't take too long. But I doubt there's anyone here that will do work for you for free. You can hire a programmer or consultant, or look at any of the THOUSANDS of bash scripts and tutorials you can find through Google, and gain the knowledge yourself.

steve51184 01-26-2009 05:04 PM

but what is the point in this forum if no one will help? i thought it was it was here for?

TB0ne 01-27-2009 08:25 AM

Quote:

Originally Posted by steve51184 (Post 3422135)
but what is the point in this forum if no one will help? i thought it was it was here for?

Yes...HELP. Post what scripts you've written, and what error(s) you get, and you will get help.

Ask someone to write you a script, because you don't want to do it, won't get you anything. You've been pointed to links giving you the information you need. This isn't the "free programmer forum", where you post a question and get back scripts.

steve51184 01-27-2009 10:33 AM

Quote:

Originally Posted by TB0ne (Post 3422738)
Yes...HELP. Post what scripts you've written, and what error(s) you get, and you will get help.

Ask someone to write you a script, because you don't want to do it, won't get you anything. You've been pointed to links giving you the information you need. This isn't the "free programmer forum", where you post a question and get back scripts.

wow so rude!! i never said i don't want to do it i'd LOVE to be able to code but i can't and i've really really tried to but i just can't so don't say i won't when i can't!!!

anyway if this was just a bash script someone would've helped like they always do but cos it's php no one will :(

also you've replied to this topic 6 times over 4 days wouldn't it have been quicker for you to make it? :)

TB0ne 01-27-2009 10:45 AM

Quote:

Originally Posted by steve51184 (Post 3422892)
wow so rude!! i never said i don't want to do it i'd LOVE to be able to code but i can't and i've really really tried to but i just can't so don't say i won't when i can't!!!

anyway if this was just a bash script someone would've helped like they always do but cos it's php no one will :(

also you've replied to this topic 6 times over 4 days wouldn't it have been quicker for you to make it? :)

Not being rude to you, but being honest.

I've said from the very beginning that I'm not going to do your work for you, period. I doubt anyone will. If you can't do it, find someone else at your company who can. You've been given links to the PHP function that can do it easily, along with examples, and been pointed to the relevant commands (sed and awk), along with a bash scripting tutorial, but it doesn't appear that you've looked in to any of those resources, or done anything except ask people to write your scripts for you.

This can be done with bash or php..you pick. You say you've "really really tried". Post what you've done and the errors, and we can help you figure out what's wrong. Put some effort forth on your side, and you will get help. If you don't want to do anything but ask someone else to do your work for you, don't complain when they don't.

If you want help, post what you've done and the errors. If you want someone to write this for you, hire a consultant.

steve51184 01-27-2009 10:51 AM

but again you really don't seem to understand i can't do this! some people can some can't

also i've looked at explode and can't figure it out nor do i think it's what i need :\

TB0ne 01-27-2009 10:56 AM

Quote:

Originally Posted by steve51184 (Post 3422916)
but again you really don't seem to understand i can't do this! some people can some can't

also i've looked at explode and can't figure it out nor do i think it's what i need :\


No, I do understand, but you're missing several points. Let me be more clear:

1. PHP is one option. I only mentioned it because YOU started the thread, by bringing up PHP. I gave you the link to the resource and example on how to do it via PHP.

2. BASH script is the second option. I gave you the commands to use (sed and awk), which have ample documentation, both through man pages and thousands of examples that can be easily found through Google.

3. If you can't do it, why do you not think the solutions given to you are what you need?

4. If you can't do it HIRE SOMEONE WHO CAN OR LET SOMEONE ELSE DO IT.

Google for "linux sed search replace". You will find lots of information with sample scripts that you can use. If that's too hard, again....HIRE SOMEONE.

steve51184 01-27-2009 11:27 AM

getting to work now will post what i have soon :)

steve51184 01-27-2009 11:41 AM

right now i'm getting somewhere but i'm having trouble so i'll post what i have:

this strips the "RewriteCond %{REMOTE_ADDR}" line from my file and writes it to a new file
Code:

grep -i "RewriteCond %{REMOTE_ADDR}" /home2/test.txt > /home2/list.txt

this removes the "RewriteCond %{REMOTE_ADDR} (" part
Code:

sed -i 's/RewriteCond %{REMOTE_ADDR} (//g' /home2/list.txt

now i'm trying to use sed and the same command above to remove all the \ but i get this error:

Quote:

# sed -i 's/\//g' /home2/list.txt
sed: -e expression #1, char 6: unterminated `s' command

and i'm also trying to remove )$ at the end of the file but it's not removing it or giving me an error here's the command:

Code:

sed -i 's/)$//g' /home2/list.txt

TB0ne 01-27-2009 12:16 PM

Quote:

Originally Posted by steve51184 (Post 3422963)
right now i'm getting somewhere but i'm having trouble so i'll post what i have:

this strips the "RewriteCond %{REMOTE_ADDR}" line from my file and writes it to a new file
Code:

grep -i "RewriteCond %{REMOTE_ADDR}" /home2/test.txt > /home2/list.txt

this removes the "RewriteCond %{REMOTE_ADDR} (" part
Code:

sed -i 's/RewriteCond %{REMOTE_ADDR} (//g' /home2/list.txt

now i'm trying to use sed and the same command above to remove all the \ but i get this error:




and i'm also trying to remove )$ at the end of the file but it's not removing it or giving me an error here's the command:

Code:

sed -i 's/)$//g' /home2/list.txt

This is where you should have been, 4 days ago.

Quote:

sed -i 's/\//g' /home2/list.txt
To get 'special characters' (like the \, /, |, etc.), to go through, try escaping them with a backslash, like:

Code:

sed 's/\\//g' /home2/list.txt
You don't need the "-i" for this..that means case-insensitive, and since you're only doing a special character, you don't need it. Putting a leading backslash will make sed treat the next character as JUST a character, rather than a 'control' character, which does something special.

This should also work for this:

Code:

sed 's/\)\$//g' /home2/list.txt

You can also (if you want to), string your sed's together on one line, like this:

Code:

cat <filename> | sed <whatever> | sed <whatever> | sed <whatever> > output.file

steve51184 01-27-2009 12:30 PM

if i don't put a -i in this command it just outputs on the screen and not to a file

Code:

sed 's/\\//g' /home2/list.txt
also i can't get the second command to work:

Quote:

# sed 's/\)\$//g' /home2/list.txt
sed: -e expression #1, char 9: Unmatched ) or \)

steve51184 01-27-2009 12:52 PM

right i've almost done it with this so far:

Code:

grep -i "RewriteCond %{REMOTE_ADDR}" /home2/test.txt > /home2/list.txt
sed -i 's/RewriteCond %{REMOTE_ADDR} (//g' /home2/list.txt
sed -i 's/\\//g' /home2/list.txt
tr -d \\n < list.txt | tr \| \\n | sort > list2.txt && mv list2.txt list.txt

the only thing i need to do is strip the )$ bit :)

steve51184 01-27-2009 01:04 PM

nm i've done it with the following:

Code:

grep -i "RewriteCond %{REMOTE_ADDR}" /home2/test.txt > /home2/list.txt
sed -i 's/RewriteCond %{REMOTE_ADDR} (//g' /home2/list.txt
sed -i 's/\\//g' /home2/list.txt
sed -i 's/)//g' /home2/list.txt
sed -i 's/\$//g' /home2/list.txt
tr -d \\n < list.txt | tr \| \\n | sort > list2.txt && mv list2.txt list.txt

thank you very much for helping/forcing me to do this i really had fun doing it and will think/look harder next time :)

TB0ne 01-27-2009 01:06 PM

Quote:

Originally Posted by steve51184 (Post 3423010)
also i can't get the second command to work:

# sed 's/\)\$//g' /home2/list.txt
sed: -e expression #1, char 9: Unmatched ) or \)

Hmm...not at a machine where I can test this, but it's probably got to do something with the backslashes. Sed can be picky...try:

Code:

sed 's/\\)\$//g' /home2/list.txt
and see if that gets it.

steve51184 01-27-2009 01:10 PM

Quote:

Originally Posted by TB0ne (Post 3423058)
Hmm...not at a machine where I can test this, but it's probably got to do something with the backslashes. Sed can be picky...try:

Code:

sed 's/\\)\$//g' /home2/list.txt
and see if that gets it.

read my last post:

http://www.linuxquestions.org/questi...54#post3423054

steve51184 01-27-2009 02:18 PM

seems like i forgot i was wanting this to be a html page so i edited it to display properly:

Code:

grep -i "RewriteCond %{REMOTE_ADDR}" /home2/.htaccess > /home2/test.html
sed -i 's/RewriteCond %{REMOTE_ADDR} (//g' /home2/test.html
sed -i 's/\\//g' /home2/test.html
sed -i 's/)//g' /home2/test.html
sed -i 's/\$//g' /home2/test.html
sed -i 's/|/<br>/g' /home2/test.html

but i want to include other formatting within the file so is there a way with grep/sed/awk to include a line of code at the start/end of a file?

i've searched google but i'm not getting anything other then weird links

TB0ne 01-27-2009 02:25 PM

Quote:

Originally Posted by steve51184 (Post 3423154)
seems like i forgot i was wanting this to be a html page so i edited it to display properly:

Code:

grep -i "RewriteCond %{REMOTE_ADDR}" /home2/.htaccess > /home2/test.html
sed -i 's/RewriteCond %{REMOTE_ADDR} (//g' /home2/test.html
sed -i 's/\\//g' /home2/test.html
sed -i 's/)//g' /home2/test.html
sed -i 's/\$//g' /home2/test.html
sed -i 's/|/<br>/g' /home2/test.html

but i want to include other formatting within the file so is there a way with grep/sed/awk to include a line of code at the start/end of a file?

i've searched google but i'm not getting anything other then weird links

You can do it with a bit of simple 'cheating'. For example, the '>' character. One of them will write to a file (overwriting what ever is there), but if you put two, you will append text to it. So you could do:

Code:

echo "Beginning line of my report..." > output.file
<your shell script here> >> output.file
echo "Ending line of my report..." >> output.file

First line puts text in the new output file. Second line is the shell script you just wrote, which will put it's output after the first line you just wrote. Third line puts the 'ending' on.

There's lots of different ways to do it, that's VERY quick, and VERY dirty.

You must have been posting yours, when I was posting mine.

steve51184 01-27-2009 02:29 PM

wow that's an amazing trick and i had not idea about the double > thank you very much


All times are GMT -5. The time now is 11:32 PM.