반응형
The easiest way to replace white spaces with (underscores) _ in bash [closed]
recently I had to write a little script that parsed VMs in XenServer and as the names of the VMs are mostly with white spaces in e.g Windows XP or Windows Server 2008, I had to trim those white spaces and replace them with underscores _ . I found a simple solution to do this using sed which is great tool when it comes to string manipulation.
echo "This is just a test" | sed -e 's/ /_/g'
returns
This_is_just_a_test
You can do it using only the shell, no need for tr
or sed
$ str="This is just a test"
$ echo ${str// /_}
This_is_just_a_test
This is borderline programming, but look into using tr:
$ echo "this is just a test" | tr -s ' ' | tr ' ' '_'
Should do it. The first invocation squeezes the spaces down, the second replaces with underscore. You probably need to add TABs and other whitespace characters, this is for spaces only.
반응형
'code' 카테고리의 다른 글
Append column to pandas dataframe (0) | 2020.09.21 |
---|---|
Function with same name but different signature in derived class (0) | 2020.09.21 |
? : PHP 5.3에서 무엇입니까? (0) | 2020.09.21 |
Apache를 다시 시작할 때 "make_sock : 주소 [::] : 443에 바인딩 할 수 없습니다"(trac 및 mod_wsgi 설치) (0) | 2020.09.20 |
현재 PowerShell 실행 파일을 얻으려면 어떻게해야합니까? (0) | 2020.09.20 |