निम्नलिखित एक-लाइनर:
perl -ne '$_ =~ s|^(( )+)|" " x (length($1)/4)|eg; print $_' < test.txt
2-अंतरिक्ष इंडेंट के साथ 4-स्थान इंडेंट की जगह।
(आप उत्पन्न पैटर्न को देखने के " "साथ प्रतिस्थापित करके सत्यापित कर सकते "-+"हैं)
अब, हम एक bash फ़ाइल बना सकते हैं, इसे कॉल करते हैं indent-changer.sh:
#!/bin/bash
while read filename; do
if ! [[ -r "$filename" ]]; then
echo "Skipping '$filename' because not readable"
continue
fi
tempfile="$(mktemp)"
if perl -ne '$_ =~ s|^(( )+)|" " x (length($1)/4)|eg; print $_' < "$filename" > "$tempfile"; then
mv "$filename" "$filename".orig
mv "$tempfile" "$filename"
echo "Success processing '$filename'"
else
echo "Failure processing '$filename'"
fi
done < "$1"
फ़ाइलों की सूची को फ़ाइल में संसाधित करने के लिए डंप करें, और उपरोक्त स्क्रिप्ट निष्पादित करें। मूल फ़ाइल अभी भी प्रत्यय के साथ मौजूद .origहै। इसलिए, उदाहरण के लिए:
find . -type f -iname "*.txt" > files-to-process.lst
# Verify or edit the .lst file as needed
./indent-changer.sh files-to-process.lst > processing.log
आप आसानी से कर सकते हैं egrep -v '^Success' processing.log।
पुनश्च: मैंने अपने सिग्विन इंस्टॉलेशन पर वन-लाइनर (लेकिन बैश स्क्रिप्ट नहीं) का परीक्षण किया; मुझे याद नहीं है कि क्या perlमूल स्थापना का हिस्सा है, या बाद में जोड़ा गया है। लेकिन मुझे लगता है कि यह मूल स्थापना का हिस्सा है।
"-+"निम्न फ़ाइल के साथ पैटर्न का परीक्षण :
THis is a test file
With indentation
more indentation
plus internal spaces
outdent
indent again
another internal space example
two spaces after two indents
end
end
end
का परिणाम:
THis is a test file
-+With indentation
-+-+more indentation
-+-+plus internal spaces
-+outdent
-+-+indent again
-+-+another internal space example
-+-+ two spaces after two indents
-+-+end
-+end
end
संपादित करें 2: यहां पर्ल वन-लाइनर का अधिक सामान्य संस्करण है:
perl -ne '$f=" ";$t=" ";$_=~s|^(($f)+)|$t x (length($1)/length($f))|eg; print $_' < test.txt
इस संस्करण के साथ, बस के लिए परिभाषाओं को संपादित $fऔर $tजरूरत के रूप में।