LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-20-2004, 05:47 AM   #1
therealbxp
LQ Newbie
 
Registered: Nov 2004
Posts: 5

Rep: Reputation: 0
newbe bash question ( grep processing)


so first of all hello everybody this is my first post
i am also new to linux so if possible be very clear and thx in advance

the problem
i have writen a bash script that gets a html file from a server(this is done with wget, no probs so far) . now i want to process this file( there is always a part(script) i want to delete).
the part i want to delete is a javascript( placed between <script> </script)
how can i do this
i know u can search the file and get the linenumbers with grep( problem here after the linenumbers theres also the line with text)

i think if i can solve the above problem so i only can get the numbers i would be able to write the rest of the program

quick version
how can i get only the first part( just the number198) of this line
198:<script>blblblablana</script>

thx in advance
bxp
 
Old 11-20-2004, 07:34 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
Quote:
quick version
how can i get only the first part( just the number198) of this line
198:<script>blblblablana</script>
You can do that like this:
Code:
#!/bin/bash

LINE="198:<script>blblblablana</script>"
echo "${LINE%%:*}"
If you can rely on both the "<script>" and "</script>" tags to be on the same line, and also you don't need to preserve text before "<script>" and after "</script>", then you can do it easier in one go with grep:
Code:
grep -vi '<script>.*</script>' dummy.html
if the "<script>" and "</script>" tags can possibly occur on different lines, use 'sed' instead of 'grep'. then you can also preserve text before "<script>" and after "</script>".

Say, some html file ("dummy.html") looks like this:
Code:
<html>
blalbla
zzz<script>blblblablana</script>444
qweqw
<script>blblbla
asas
asas
blana</script>
123
</html>
Then this script:
Code:
#!/bin/bash

sed \
-e '/<script>.*<\/script>/{' \
-e 's/^\(.*\)<script>.*<\/script>\(.*\)$/\1 \2/' \
-e 'n' \
-e '}' \
-e 's/^\(.*\)<script>/\1/' \
-e 's/<\/script>\(.*\)$/\1/' \
dummy.html
...outputs:
Code:
<html>
blalbla
zzz 444
qweqw
blblbla
asas
asas
blana
123
</html>
 
Old 11-20-2004, 07:40 AM   #3
therealbxp
LQ Newbie
 
Registered: Nov 2004
Posts: 5

Original Poster
Rep: Reputation: 0
thx for the excellent answer, sorry for the stupid question but everybody has to learn

greetings bxp
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
processing command args in bash slzckboy Programming 16 10-18-2017 03:12 AM
processing a ksh shell in bash environment ntoughe Programming 9 09-12-2005 05:09 AM
Bash script question (grep and awk) hamish Linux - Software 6 04-06-2005 03:14 PM
bash script and grep syros Programming 4 01-13-2005 03:04 PM
bash command: grep mAineAc Programming 6 11-18-2004 11:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration