margin: 30px;
padding: 30px 20px 10px;
#!/bin/bash
#
css_file="$1"
# Iterate through each line of the CSS file
while IFS= read -r line; do
if [[ $line == *"border"* || $line == *"max-width"* ]]; then
continue
else
# Use regular expressions to find pixel values (e.g., "10px", "20px", etc.)
px_values=$(echo "$line" | grep -oE "[0-9]+px")
echo "'px_values:' $px_values"
# Iterate through each found pixel value
for px_value in $px_values; do
# Extract the numeric value from the pixel value
numeric_value=$(echo "$px_value" | grep -oE "[0-9]+")
# Convert the pixel value to rem and divide by 10
rem_value=$(awk "BEGIN { printf \"%.2f\", $numeric_value / 10 }")
# Replace the pixel value with the calculated rem value
new_line=$(echo "$line" | sed "s/$px_value/${rem_value}rem/g")
# sed -i "s/$line/$new_line/" $css_file
done
# Print the modified line
# echo "$line"
fi
done < "$css_file"
#!/bin/bash
css_file="$1"
while read -r line; do
if [[ $line == *"border"* || $line == *"max-width"* ]]; then
echo "$line"
continue
else
# Use regular expressions to find pixel values (e.g., "10px", "20px", etc.)
px_values=$(echo "$line" | grep -oE "[0-9]+px")
new_line="$line"
# Iterate through each found pixel value
for px_value in $px_values; do
# Extract the numeric value from the pixel value
numeric_value=$(echo "$px_value" | grep -oE "[0-9]+")
# Convert the pixel value to rem and divide by 10
rem_value=$(awk "BEGIN { printf \"%.2f\", $numeric_value / 10 }")
# Replace the pixel value with the calculated rem value
new_line=$(echo "$new_line" | sed "s/$px_value/${rem_value}rem/g")
# sed -i "s/$line/$new_line/" $css_file
done
# Print the modified line
echo "$new_line"
fi
done < "$css_file"
awk -F: '
/\{|\}|border|max-width|^$/ { print $0 }
!/\{|\}|border|max-width|^$/ {
split( substr($2, 1, length($2)-1) , a," ");
res = "";
for (i in a) {
ind = match(a[i], /[0-9]+px/)
if (ind != 0) {
a[i] = sprintf ("%.2frem", a[i]/10);
}
res=a[i] " " res;
}
res = substr(res, 1, length(res)-1);
print $1 ": " res ";"
}
' some.css
Arrays in awk are different—they are associative. This means that each array is a collection of pairs—an index and its corresponding array element value...
The pairs are shown in jumbled order because their order is irrelevant.
awk -F: '{
if ($0 ~ /\{|\}|border|max-width|^$/ ) { print $0 }
else {
res = "";
split( substr($2, 1, length($2)-1) , a, " ");
for (i=1; i<= length(a);i++) res = res " " ((match(a[i], /[0-9]+px/) != 0)? a[i]/10"rem" : a[i]);
print $1 ": " res";"
}
}' some.css