Sed if statement (totally stuck) apache


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed if statement (totally stuck) apache
# 1  
Old 03-02-2009
Sed if statement (totally stuck) apache

I have a irritating problem with a "if"-statement or what you should call it, in sed. I would love some help here since I am very, very stuck.

I have this statement that I want to do:
if line contains a:
do this
if none of the lines contained a:
do this

The problem with this statement is that I cant find out if it's possible and if so, how to implement this statement. In any case I've found, both "do this" will be made and I will get two outputs.


I've tried things such as:
"/searchstring/b ifpart"
"1s/^/<!-- ELSE -->/"
"b end"
":ifpart"
"s/searchstring\1<!-- IF -->/"
":end"

The problem is that in any case, the "ELSE" comment will be printed and if the searchstring is found, the IF comment will be printed as well. I only want ONE of them simultaniously, not both. I would really love to hear if it's possible or not and if you have any clue how to do it as wellSmilie

Thank you!
# 2  
Old 03-02-2009
Quote:
Originally Posted by hjalle
I have a irritating problem with a "if"-statement or what you should call it, in sed. I would love some help here since I am very, very stuck.

I have this statement that I want to do:
if line contains a:
do this
if none of the lines contained a:
do this

That's not a job for sed; use the shell:

Code:
sstr=a
found=0

while IFS= read -r line
do
   case $line in 
     *"$sstr"*) : do whatever; found=1 ;;
   esac
done < file

if [ $found -eq 0 ]
then
  : do something else
fi

# 3  
Old 03-02-2009
I have to use sed, whether its a job for it or not.

it's an application where sed commands are the only option, which means that I just have to solve it some way:/
# 4  
Old 03-02-2009
If sed is the only option this sounds like coursework. Why are you restricted to sed only?
# 5  
Old 03-03-2009
Because I'm using the mod_sed's Outputsed-directive in apache.. This do mean that I only have that option, right? :P
# 6  
Old 03-03-2009
We have a rule against homework, no rule against torturing sed into something it was not meant for.

you can if then else in sed but it is plain ugly... work thru this:

http://student.northpark.edu/pemente/sed/ifelse.txt
# 7  
Old 03-03-2009
haha Smilie

I know that it's a bit weird to try what I do, but I just want to atleast try to see how it performs.

Thanks for that link, and I did try those if/elses, but the problem is that it works line by line (as it should, but thats the problem!) which means that it won't work for me. I want to see if a string was found in the ENTIRE document, not only if it contains on line by line basis.

(Thats an edit from the previously mentioned pseudo I had, since I didn't know I would get this problem)

If the string is found in the entire document:
do this
else
do this

I'm beginning to believe that its not possible so I guess this is my last attempt. I would love to test this solution as it will solve a big problem for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Stuck process when using sed command

$BIN/sql.py "select * from reporting.V_AMSNB_OPT_PB_LOAD" | sed -n "2,$ p" | sed "s/ /,/g" > $DATADIR/OPT_PB_LOAD_AMSNB.csv if The processes that get created from the above code gets stuck...as in the processes get created but then never completes. (or at least we assume it never completes... (1 Reply)
Discussion started by: mimiyj
1 Replies

2. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

3. Shell Programming and Scripting

Totally new to shell scripting

lllll (34 Replies)
Discussion started by: risarose87
34 Replies

4. Shell Programming and Scripting

If else statement in sed

Hello Guys, I am new here and this is my first post, hope someone can help me I am writing a script that is supposed to go in 9 different directories and edit a given file in each of the directories. I am using sed to edit the file as sed -i 'line# s/#to be changed/#to be replaced with/... (5 Replies)
Discussion started by: Madiouma Ndiaye
5 Replies

5. Shell Programming and Scripting

stuck on first script with sed issue

Hi, I'm developing my first bash script and have made good progress but stuck at this point. I've run sed on a text file to extract some data and have saved it into a variable e.g. $blah the variable contains emails as follows e.g. <a@b.com> <b@c.com> I'm now trying to edit... (2 Replies)
Discussion started by: newb1000
2 Replies

6. UNIX for Dummies Questions & Answers

Totally brand new to CRON

hi there i am very new to the world of CRON. i would like to know from below the ground up how i can learn abouot cron. where the best tutorials lie. i have an assignment that requires i set up a cron job to download a price file. am i in control of where the price file is downloaded to? (eg to... (2 Replies)
Discussion started by: buzzby
2 Replies

7. AIX

Totally messed up

I have moved the etc/passwd file and we are now unable to get in to Unix - any suggestions? (2 Replies)
Discussion started by: vbagwell
2 Replies

8. Shell Programming and Scripting

if and sed statement

this is my output for my crawler. /about.html /ads/ /advanced_search?hl=en froogle.google.com/frghp?hl=en&tab=wf&ie=UTF-8 groups.google.com/grphp?hl=en&tab=wg&ie=UTF-8 /imghp?hl=en&tab=wi&ie=UTF-8 /intl/en/options/ /language_tools?hl=en /maphp?hl=en&tab=wl&ie=UTF-8... (3 Replies)
Discussion started by: chris1234
3 Replies

9. UNIX for Dummies Questions & Answers

totally new to unix

I'm trying to write a script, named "worfo" which will read a file in and return, on the screen, the number of words in the file. Also, I am looking to do several extras with this script. I need to allow it to accept the option "-n" which will use all non-alpha characters to delimit words. Also,... (2 Replies)
Discussion started by: adawg1283
2 Replies

10. UNIX for Dummies Questions & Answers

Totally new unix user

Hi, ive just been given a HP UX c180 to play with, along with a few external hd`s, can anyone recommend me a good place to start learning unix please as i havent got a clue what im doing when it comes to unix. Either web links or book recomendations would be great. I would like to set it up with... (1 Reply)
Discussion started by: Leviathan40
1 Replies
Login or Register to Ask a Question