grep & sed question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep & sed question
# 1  
Old 11-20-2004
grep & sed question

I'm trying to write a bash script to perform a tedious task, but I have no experience and hardly any knowledge so I've been having a rough time with it.
I'm on Mac OS X, and I want a script to do the following:
I have a directory that has about 200 sudirectories. In each of these directories, there are a number of html files that each include a relative link to an image.
What I want to do is create one html file that contains all the img tags of all the html files in all the sudirectories. I would also like to turn all the relative links into absolute ones (meaning: I want each src attribute to contain the full path on my system of the html file -and image, they are in the same directory-).
I've been at it with sed, but one of the problems seems to be that some of the html files don't have regular UNIX line endings, so it's kind of hard for me to extract just those line with img files (I get the full files), let alone img tags.
What I've been toying with is the following (where you'll see that I try to put some line endings back where I think they'd be useful - though for some reason it doesn't work as expected):

#!/bin/bash

for pic in ./advanced/*/image??.html
do
grep img $pic | sed "s/img/img\
/" | cat >> images.html
done

This script doesn't address the absolute link problem yet.

Any help would be greatly appreciated.

Last edited by der Kopf; 11-20-2004 at 05:00 AM..
# 2  
Old 11-22-2004
Just a couple of ideas....

For the end of line problem, use tr or dos2unix (if applicable and installed) to replace the end of line characters with \n.

For getting the absolute path, have a look at "dirname".

Cheers
ZB
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep / awk /sed question

Hi All, I have a file with following sample data. I am also attaching the file. (Est) (Est) Jobs Sch Workstation Job Stream SchedTime State Pr Start Elapse # OK Lim POOL #ACR_BILLING 0005 08/15 ... (2 Replies)
Discussion started by: Kevin Tivoli
2 Replies

2. Shell Programming and Scripting

Grep & sed - Output

Hi All, Facing an issue with grep & sed I have logs as below: gsc_1_20121121.log:2012-11-21 10:09:13,143 INFO - fmsspace.1 ProcessNewOrderSingleRequest: Result - ProcessNewOrderSingleBatchResultDTO - success:true,newOrderSingleBatchResults:ProcessNewOrderSingleResultDTO -... (13 Replies)
Discussion started by: irfanmemon
13 Replies

3. Shell Programming and Scripting

help understanding regex with grep & sed

I have the following line of code that works wonders. I just don't completely understand it as I am just starting to learn regex. Can you help me understand exactly what is happening here? find . -type f | grep -v '^\.$' | sed 's!\.\/!!' (4 Replies)
Discussion started by: trogdortheburni
4 Replies

4. Shell Programming and Scripting

applescript & grep - sed command

I'm new using Unix commands in applescript. The following script you choose different folders with PDfs, get file count of PDfs on chosen folders, & write the results in text file. set target_folder to choose folder with prompt "Choose target folders containing only PDFs to count files" with... (0 Replies)
Discussion started by: nellbern
0 Replies

5. Shell Programming and Scripting

Question grep and sed

hi everybody i have this script and it work's if i use a single variable in grep, but when i put $searchterm_ the script stops work i have a problem too in sed, because i don't know how i can search and replace more than one item, in this case is >>> $searchterm/$replaceterm and... (4 Replies)
Discussion started by: felito
4 Replies

6. Shell Programming and Scripting

Help Needed with grep & sed

On one of my servers, it appears that a bunch of html files got the following code added to it... I was going to try to remove this line using grep & sed... as sample grep -lr -e 'apples' *.html | xargs sed -i 's/apples/oranges/g' I can get the grep portion to work... grep "<script... (7 Replies)
Discussion started by: djlane
7 Replies

7. UNIX for Dummies Questions & Answers

Grep char count & pipe to sed command

Hi I am having a 'grep' headache Here is the contents of my file: (PBZ,CP,(((ME,PBZ,BtM),ON),((ME,((PBZ,DG),(CW9,PG11))),CW9,TS2,RT1))) I would like to count out how many times 'PBZ' occurs and then place that number in the line above 3... (8 Replies)
Discussion started by: cavanac2
8 Replies

8. UNIX for Dummies Questions & Answers

sed/grep string replace question

Hi all, I know this question has probably been answered before, but I am struggling with this problem, even after googling a million pages. In a file named rdmt.conf I need a single character replaced, the number in the line below CUR_OC4J_ID=1 It will always appear after... (3 Replies)
Discussion started by: Mike AAA
3 Replies

9. Shell Programming and Scripting

question on sed grep awk from variable

i am still confusing on how to use sed, grep and awk if the input is not a file but a variable. such as: a="hello world" b="how are you" c="best wish to you" d="222,333,444" what if i want to check which variable $a,$b,$c,$d have contain "you" what if i want to replace the word "you"... (9 Replies)
Discussion started by: 3Gmobile
9 Replies

10. Shell Programming and Scripting

sed / grep question

Hello, I am new to shell scripting. I have a input file: Few lines of the input file has the following. /a0012/abcd12/abcd12 /a0003/xyzab1/lmno123 /a0006/pqrst1/abcde12 In my output file, I only need to get anything that is between the first and second '/' of each line: exampple:... (6 Replies)
Discussion started by: hemangjani
6 Replies
Login or Register to Ask a Question