GREP buscar y sustituir en multiples archivos
Publicado por Manuel Garcia (2 intervenciones) el 21/08/2016 11:52:18
Hola,
tengo un script que se encarga de:
1. dividir un archivo CSS en tantos archivos como estilos existan en el CSS.
2. Buscar entre los archivos divididos si coinciden los nombres de los estilos con un listado.
3. En caso de que coincidan, el script va a eliminar las lineas que contengan 3 propiedades de CSS (margin-left, margin-right y text-indent).
4. En su lugar va a añadir 3 propiedades con valores nuevos (margin-left, margin-right y text-indent). en dependencia del nombre del estilo.
Este escript divide todo correctamente, pero justamente creo que cuando llega al Grep no sabe reconocer el numero del bucle y por eso no funciona.
Tras ejecutarlo veréis que en la carpeta, aparecen varios archivos como:
idGeneratedStyles.css
idGeneratedStyles0
idGeneratedStyles1
idGeneratedStyles2
idGeneratedStyles3
Aqui esta el script:
y aqui esta el archivo CSS, llamado idGeneratedStyles.css:
tengo un script que se encarga de:
1. dividir un archivo CSS en tantos archivos como estilos existan en el CSS.
2. Buscar entre los archivos divididos si coinciden los nombres de los estilos con un listado.
3. En caso de que coincidan, el script va a eliminar las lineas que contengan 3 propiedades de CSS (margin-left, margin-right y text-indent).
4. En su lugar va a añadir 3 propiedades con valores nuevos (margin-left, margin-right y text-indent). en dependencia del nombre del estilo.
Este escript divide todo correctamente, pero justamente creo que cuando llega al Grep no sabe reconocer el numero del bucle y por eso no funciona.
Tras ejecutarlo veréis que en la carpeta, aparecen varios archivos como:
idGeneratedStyles.css
idGeneratedStyles0
idGeneratedStyles1
idGeneratedStyles2
idGeneratedStyles3
Aqui esta el script:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
# Split all the CSS styles from the Adobe Indesign into a separate files
cat ./ePub/OEBPS/css/idGeneratedStyles.css | (
I=0;
echo "">./ePub/OEBPS/css/idGeneratedStyles0;
while read line;
do
echo $line >> ./ePub/OEBPS/css/idGeneratedStyles$I;
if [ "$line" == '}' ];
then I=$[I+1];
echo "" > ./ePub/OEBPS/css/idGeneratedStyles$I;
fi;
############
if grep -qr L0-R0-I4 "./ePub/OEBPS/css/idGeneratedStyles$I"; then perl -i -ne'/\text-indent/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
#add the right bledding
perl -pi -w -e 's#}#margin-left:0;margin-right:0;text-indent:1em;}#g;' ./ePub/OEBPS/css/idGeneratedStyles$I;
fi;
echo "working on css file $I %";
done;
)
############
if grep -qr L0-R0-I5 "./ePub/OEBPS/css/idGeneratedStyles$I"; then
#delete the bad bleeding
perl -i -ne'/\margin-left/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
perl -i -ne'/\margin-right/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
perl -i -ne'/\text-indent/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
#add the right bledding
perl -pi -w -e 's#}#margin-left:0;margin-right:0;text-indent:1em;}#g;' ./ePub/OEBPS/css/idGeneratedStyles$I;
fi;
echo "working on css file $I %";
done;
)
############
if grep -qr L0-R0-I3 "./ePub/OEBPS/css/idGeneratedStyles$I"; then
#delete the bad bleeding
perl -i -ne'/\margin-left/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
perl -i -ne'/\margin-right/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
perl -i -ne'/\text-indent/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
#add the right bledding
perl -pi -w -e 's#}#margin-left:0;margin-right:0;text-indent:1em;}#g;' ./ePub/OEBPS/css/idGeneratedStyles$I;
fi;
echo "working on css file $I %";
done;
)
############
if grep -qr L3-R0-I-3 "./ePub/OEBPS/css/idGeneratedStyles$I"; then
#delete the bad bleeding
perl -i -ne'/\margin-left/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
perl -i -ne'/\margin-right/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
perl -i -ne'/\text-indent/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
#add the right bledding
perl -pi -w -e 's#}#margin-left:1em;margin-right:0;text-indent:-1em;}#g;' ./ePub/OEBPS/css/idGeneratedStyles$I;
fi;
echo "working on css file $I %";
done;
)
y aqui esta el archivo CSS, llamado idGeneratedStyles.css:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
p.CHAPTER_eBook_03-Biblio-entry---2-lines-space-below---L0-R0-I4---EBOOK {
-epub-hyphens:none;
color:#000000;
font-family:"Palatino LT Std", serif;
font-size:0.833em;
font-style:normal;
font-variant:normal;
font-weight:normal;
line-height:1.2;
margin-bottom:0.84em;
margin-left:14px;
margin-right:0;
margin-top:0.24em;
orphans:1;
page-break-after:auto;
page-break-before:auto;
text-align:left;
text-decoration:none;
text-indent:-14px;
text-transform:none;
widows:1;
}
p.CHAPTER_eBook_03---text---2nd-para-full-space-below---L0-R0-I5---EBOOK {
-epub-text-align-last:left;
color:#000000;
font-family:"Palatino LT Std", serif;
font-size:0.833em;
font-style:normal;
font-variant:normal;
font-weight:normal;
line-height:1.2;
margin-bottom:1.44em;
margin-left:0;
margin-right:0;
margin-top:0;
orphans:1;
page-break-after:auto;
page-break-before:auto;
text-align:justify;
text-decoration:none;
text-indent:1em;
text-transform:none;
widows:1;
}
p.CHAPTER_eBook_03---text---2nd-para-space-below---L0-R0-I3---EBOOK {
-epub-text-align-last:left;
color:#000000;
font-family:"Palatino LT Std", serif;
font-size:0.833em;
font-style:normal;
font-variant:normal;
font-weight:normal;
line-height:1.2;
margin-bottom:0.72em;
margin-left:0;
margin-right:0;
margin-top:0;
orphans:1;
page-break-after:auto;
page-break-before:auto;
text-align:justify;
text-decoration:none;
text-indent:8px;
text-transform:none;
widows:1;
}
p.CHAPTER_eBook_03---text-L3-R0-I-3--EBOOK {
-epub-hyphens:none;
-epub-text-align-last:left;
color:#000000;
font-family:"Palatino LT Std", serif;
font-size:0.833em;
font-style:normal;
font-variant:normal;
font-weight:normal;
line-height:1.2;
margin-bottom:0;
margin-left:0;
margin-right:0;
margin-top:0;
orphans:1;
page-break-after:auto;
page-break-before:auto;
text-align:justify;
text-decoration:none;
text-indent:0;
text-transform:none;
widows:1;
}
p.CHAPTER_eBook_03---text---full-space-below---EBOOK {
-epub-text-align-last:left;
color:#000000;
font-family:"Palatino LT Std", serif;
font-size:0.833em;
font-style:normal;
font-variant:normal;
font-weight:normal;
line-height:1.2;
margin-bottom:1.44em;
margin-left:0;
margin-right:0;
margin-top:0;
orphans:1;
page-break-after:auto;
page-break-before:auto;
text-align:justify;
text-decoration:none;
text-indent:0;
text-transform:none;
widows:1;
}
p.CHAPTER_eBook_03---text---space-below-L3-R0-I-3--EBOOK {
-epub-hyphens:none;
-epub-text-align-last:left;
color:#000000;
font-family:"Palatino LT Std", serif;
font-size:0.833em;
font-style:normal;
font-variant:normal;
font-weight:normal;
line-height:1.2;
margin-bottom:0.72em;
margin-left:0;
margin-right:0;
margin-top:0;
orphans:1;
page-break-after:auto;
page-break-before:auto;
text-align:justify;
text-decoration:none;
text-indent:0;
text-transform:none;
widows:1;
}
Valora esta pregunta


0