Posted August 30, 2008 at 11:08pm in
Linux
A couple days ago a friend sent an email to a mailing list I am on wondering how to replace the first 99 characters of a line and extracting characters 100-106 inclusive.
cat file | sed 's/^.\{99\}\(.\{7\}\).*/\1/g'
I will let you look up the regex to know what each part is doing. You would need to do this on a per line basis if you wanted to get 100-106 from each line.
I realize that this is somewhat poor design since I don’t need to cat the file to sed, sed will handle reading from the file. I was just showing that the output needed to be sent to sed for the purpose my friend was looking for. In a real app that needed the functionality you would process the file line by line and pass that line to the sed command to grab the 7 characters.
Posted August 26, 2008 at 09:08pm in
Linux
This is just a quick example I whipped up, it isn’t an actual situation where you might use it and wget might even take stdin for urls.
Lets say you have a html file you want to download all of the mp3s from so you run the following command to get the urls
[21:41:33][user@host ~]$ cat index.html | sed -e 's/"/\n/g' | egrep '^http.*\.mp3$'
http://www.greatestsiteever.com/song.mp3
http://www.goodmusic.com/goody.mp3
http://www.badmusic.com/bad.mp3
...
The thing is that you don’t want to write those urls to a file on the drive because you just don’t want the clutter.
Enter /dev/stdin
[21:45:48][user@host ~]$ cat index.html | sed -e 's/"/\n/g' | egrep '^http.*\.mp3$' | wget -i /dev/stdin
--21:45:49-- http://www.greatestsiteever.com/song.mp3
Resolving www.greatestsiteever.com... 10.10.10.1
Connecting to www.greatestsiteever.com|10.10.10.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: `song.mp3'
[ <=========================================================> ] 3,443,012 1832.5K/s in 2.2s
21:45:51 (1832.5 KB/s) - `song.mp3' saved [43012]
FINISHED --21:45:51--
Downloaded: 1 files, 3.28M in 2.2s (1832.5 KB/s)
......
This technique can be very handy depending on the type of work you are doing. If you are needing to pass a configuration file to an application and the filesystem is read only or you don’t want to actually write the config to the filesystem or any situation where a command needs an input file and you don’t want to create any files.