공백을 밑줄과 소문자로 어떻게 변경하려면 무엇입니까?
문제
다음과 같은 텍스트 파일이 있습니다.
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 문서를 더 많이 살펴보고 의견에서 조언을 따릅니다. 다음 명령은 작동해야합니다.
sed -r {filehere} -e 's/[A-Z]/\L&/g;s/ /_/g' -i
다른 팁
다른 Perl 메소드 :
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
Bash 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과 함께 :
perl -ne 's/ +/_/g;print lc' file
파이썬으로 :
>>> f=open("file")
>>> for line in f:
... print '_'.join(line.split()).lower()
>>> f.close()
제휴하지 않습니다 StackOverflow