كيف يمكنني تغيير المسافات ليؤكد وكل صغيرة؟
سؤال
ولدي ملف نصي الذي يحتوي على:
Cycle code
Cycle month
Cycle year
Event type ID
Event ID
Network start time
وأريد أن تغيير هذا النص بحيث أي وقت مضى عندما يكون هناك مسافة، أريد أن استبدله _
. وبعد ذلك، أريد الأحرف لخفض إلكتروني حالة مثل أدناه:
cycle_code
cycle_month
cycle_year
event_type_id
event_id
network_start_time
وكيف يمكن لي تحقيق ذلك؟
المحلول
وبالنظر إلى وثائق الحوار الاقتصادي الاستراتيجي بعض النصائح أكثر ويلي من التعليقات يجب أن تعمل الأمر التالي.
sed -r {filehere} -e 's/[A-Z]/\L&/g;s/ /_/g' -i
نصائح أخرى
وهناك طريقة أخرى بيرل:
perl -pe 'y/A-Z /a-z_/' file
وtr
يعمل وحده:
tr ' [:upper:]' '_[:lower:]' < file
وهناك علامة perl
في سؤالك كذلك. لذلك:
#!/usr/bin/perl
use strict; use warnings;
while (<DATA>) {
print join('_', split ' ', lc), "\n";
}
__DATA__
Cycle code
Cycle month
Cycle year
Event type ID
Event ID
Network start time
وأو:
perl -i.bak -wple '$_ = join('_', split ' ', lc)' test.txt
sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ /abcdefghijklmnopqrstuvwxyz_/" filename
ومجرد استخدام قذيفة الخاص بك، إذا كان لديك باش 4
while read -r line
do
line=${line,,} #change to lowercase
echo ${line// /_}
done < "file" > newfile
mv newfile file
البله :
awk '{$0=tolower($0);$1=$1}1' OFS="_" file
ومع بيرل:
perl -ne 's/ +/_/g;print lc' file
ومع بيثون:
>>> f=open("file")
>>> for line in f:
... print '_'.join(line.split()).lower()
>>> f.close()
لا تنتمي إلى StackOverflow