Pages

Sunday, August 22, 2010

Create html file with bash script using awk

We will present a more special method of using the Linux command line.
This method comes in helping those who need to text or HTML content.
In fact, a combination of Linux commands to get an output.
Suppose we have a list of jpeg files in folder image.
They should exist into HTML to be displayed.
As we know an image has the form:
<img src="image/name_of_jpeg.jpg" alt="" /> 
The list of file is :
11.jpg,12.jpg,13.jpg,14.jpg,15.jpg
My method uses two commands: ll and awk.
First, I will display files:
$ ll | awk '/jpg/ {print $9}'
11.jpg
12.jpg
13.jpg
14.jpg
15.jpg 
I created one variable f1.
In these variables put the beginning of each line:
f1=‘<img src="image‘; 
We add two sed commands.
First, remove the space before the file name.
The second will add the rest to complete the link.
The xargs command is used with echo and print the first part of the link.
Finally we use <<< aaaa.html to output the HTML file.
The full script is here .