Linux/Unix Shell Scripting - GREP buscar y sustituir en multiples archivos

 
Vista:

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:
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
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

SOLUCIONADO: SUSTITUIR ESTILOS AUTOMATICAMENTE

Publicado por Manuel Garcia (2 intervenciones) el 26/08/2016 11:28:32
Solucionado!
concretamente este script puedes usarlo cuando exportes desde indesign, pues Indesign tras la exportación los eBooks no se ven correctamente.
Ten encuenta:

1º lo he testeado desde el terminal de Mac
2º el ebook cuando lo descomprimes tiene la siguiente roota:
./ePub/OEBPS/css/idGeneratedStyles.css

3º el archivo css original tras exportarlo de indesign se llama:
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
rm ePub.epub
cp ePubcopia.epub ePub.epub
sudo chown -Rv root ./ePub.epub
chmod -R 777 ./ePub.epub
 
#####################################################################################
###START### MODIFY ALL BLEEDING STYLES FOR NICE APPEARANCE ON EPUB AND MOBI #########
#####################################################################################
# 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/GeneratedStyles0;
while read line;
do
echo $line >> ./ePub/OEBPS/css/GeneratedStyles$I;
if [ "$line" == '}' ];
then I=$[I+1];
echo "" > ./ePub/OEBPS/css/GeneratedStyles$I;
fi
done;
)
 
sudo chown -Rv root ./ePub/OEBPS/css/*
chmod -R 777 ./ePub/OEBPS/css/*
for f in `ls ./ePub/OEBPS/css/GeneratedStyles*` ; do
############
if grep -qr L0-R0-I4 $f; then 
		
perl -i -ne'/margin-left/ or print' $f;
perl -i -ne'/margin-right/ or print' $f;
perl -i -ne'/text-indent/ or print' $f;

#add the right bledding
perl -pi -w -e 's#}#margin-left:0;margin-right:0;text-indent:1em;\}#g;' $f
echo "working on css file $I %";
fi

############
if grep -qr L0-R0-I5 $f; then
#delete the bad bleeding
perl -i -ne'/margin-left/ or print' $f;
perl -i -ne'/margin-right/ or print' $f;
perl -i -ne'/text-indent/ or print' $f;

#add the right bledding
perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g' $f
echo "working on css file $f %";
fi;

############
if grep -qr L0-R0-I3 $f; then
#delete the bad bleeding
perl -i -ne'/margin-left/ or print' $f;
perl -i -ne'/margin-right/ or print' $f;
perl -i -ne'/text-indent/ or print' $f;

#add the right bledding
perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g;' $f
echo "working on css file $f %";
fi;
############
if grep -qr L0-R0-I5 $f; then
#delete the bad bleeding
perl -i -ne'/margin-left/ or print' $f;
perl -i -ne'/margin-right/ or print' $f;
perl -i -ne'/text-indent/ or print' $f;

#add the right bledding
perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g' $f
echo "working on css file $f %";
fi;

############
if grep -qr L6-R0-I-4 $f; then
#delete the bad bleeding
perl -i -ne'/margin-left/ or print' $f;
perl -i -ne'/margin-right/ or print' $f;
perl -i -ne'/text-indent/ or print' $f;

#add the right bledding
perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g;' $f
echo "working on css file $f %";
fi;
############
if grep -qr L6-R6-I0 $f; then
#delete the bad bleeding
perl -i -ne'/margin-left/ or print' $f;
perl -i -ne'/margin-right/ or print' $f;
perl -i -ne'/text-indent/ or print' $f;

#add the right bledding
perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g' $f
echo "working on css file $f %";
fi;

############
if grep -qr L6-R6-I3 $f; then
#delete the bad bleeding
perl -i -ne'/margin-left/ or print' $f;
perl -i -ne'/margin-right/ or print' $f;
perl -i -ne'/text-indent/ or print' $f;

#add the right bledding
perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g;' $f
echo "working on css file $f %";
fi;
############
if grep -qr L14-R4-I-5 $f; then
#delete the bad bleeding
perl -i -ne'/margin-left/ or print' $f;
perl -i -ne'/margin-right/ or print' $f;
perl -i -ne'/text-indent/ or print' $f;

#add the right bledding
perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g;' $f
echo "working on css file $f %";
fi;
############
if grep -qr CHAPTER_eBook_03-Biblio-entry---2-lines-List-1-to-9---EBOOK $f; then
#delete the bad bleeding
perl -i -ne'/margin-left/ or print' $f;
perl -i -ne'/margin-right/ or print' $f;
perl -i -ne'/text-indent/ or print' $f;

#add the right bledding
perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g' $f
echo "working on css file $f %";
fi;

############
if grep -qr CHAPTER_eBook_03-Biblio-entry---subtitle-List-1-to-9---EBOOK $f; then
#delete the bad bleeding
perl -i -ne'/margin-left/ or print' $f;
perl -i -ne'/margin-right/ or print' $f;
perl -i -ne'/text-indent/ or print' $f;

#add the right bledding
perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g;' $f
echo "working on css file $f %";
fi;
done;






###END### MODIFY ALL BLEEDING STYLES FOR NICE APPEARANCE ON EPUB AND MOBI #########
#####################################################################################

Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar