Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] grep, sed, and cat - oh my!



Dave,

> Javascript/fileA.js
> Javascript/fileB.js
> Javascript/subdir1/fileC.js
> Javascript/subdir1/fileD.js
> Javascript/subdir2/fileE.js
> Javascript/subdir2/fileF.js
> 
> I came across a command which supposedly might do the trick:
> 
> ls -1R|grep :$|sed 's/:$//g'| awk '{print $1"/*"}'|xargs cat > all.js

You can make better:

find ./ -name "*.js"
-> gives you all the *.js files in the current directory

cat $( find ./ -name "*.js" ) > all.js
-> concatenates all these files into all.js

> For step 4, editing the HTML files, I think I need to run the following 
> command:
> 
> sed 's/.*<script type="text/javascript" charset="utf-8" 
> src="javascript/includes.js"></script>.*/<script type="text/javascript" 
> charset="utf-8" src="all-min.js"></script>/g'

You obviously want to use this substitution command:
  "s/something/something else/g"

But the '/' in your sunstritution strings are clashing with the '/' in
the substitution command. But, in fact, you can use any character in a
sed command, so let's use one not appearing in your substritution
strings. '%' for example.

This version may work:

sed 's%.*<script type="text/javascript" charset="utf-8" src="javascript/includes.js"></script>.*%<script type="text/javascript" charset="utf-8" src="all-min.js"></script>%g'

> ... and I thought I would need to use "grep" so that my line-replacing 
> would happen on multiple files... but does "sed" already act on multiple 
> files?

no. sed works line by line

> And in either case, can I tweak it to only act on files with a ".html" 
> extension?

Only give sed html files to work on, like

find ./ -name *.html | while read FILE; do sed -i 's%...%...%g' $FILE; done

-- 
Nicolas LIMARE
http://nicolas.limare.net/                         pgp:0xFA423F4F

Attachment: signature.asc
Description: Digital signature


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links