Nowe posty

Autor Wątek: polecenie tr  (Przeczytany 3134 razy)

filon92

  • Gość
polecenie tr
« dnia: 2013-07-01, 17:25:21 »
Witma serdecznie,

Mam następujące pytanie, otóż czy da się użyć polecenia tr dla wczytanego przez użytkownika argumentu w bashu aby zamienić wszystkie litery w pliku podanym przez niego słowie z dużych na małe?
Dajmy na wczytuję argument a
Kod:
read a
tr $a "A-Z" "a-z"

Wiem, że tak nie mogę zrobić, istnieje jakiś sposób, żeby zrobić to tym poleceniem? a jeśli nie tym to jakimś innym?

Offline ultr

  • Users
  • Guru
  • *****
  • Wiadomości: 1177
    • Zobacz profil
polecenie tr
« Odpowiedź #1 dnia: 2013-07-01, 18:03:32 »
read a
tr 'A-Z' 'a-z' < "$a"

filon92

  • Gość
polecenie tr
« Odpowiedź #2 dnia: 2013-07-01, 18:34:18 »
ultr zrobiłem jak radziłeś lecz wyskakuje błąd "no such file or directory" :/

alvaro

  • Gość
polecenie tr
« Odpowiedź #3 dnia: 2013-07-02, 04:14:13 »
Zapewne chcesz czegoś takiego:
# tr '[:upper:]' '[:lower:]' <<<"Ala ma kota Filemona."
ala ma kota filemona.
Od wersji czwartej bash oferuje coś takiego:
bash -c 'foo="Filemon"; echo ${foo,,}'
Cytując za manualem:
Cytuj
${parameter^pattern}
${parameter^^pattern}
${parameter,pattern}
${parameter,,pattern}
Case modification. This expansion modifies the case of alphabetic characters in parameter. The pattern is expanded to produce a pattern just as in pathname expansion. The ^ operator converts lowercase letters matching pattern to uppercase; the , operator converts matching uppercase letters to lowercase. The ^^ and ,, expansions convert each matched character in the expanded value; the ^ and , expansions match and convert only the first character in the expanded value.. If pattern is omitted, it is treated like a ?, which matches every character. If parameter is @ or *, the case modification operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the case modification operation is applied to each member of the array in turn, and the expansion is the resultant list.
Pełen kod:
#!/usr/bin/env bash

a=
read -r a
#echo "$a"
a=$(tr '[:upper:]' '[:lower:]' <<<"$a")
echo "$a"

# bash 4
b=
read -r b
#echo "$b"
b=${b,,}
echo "$b"
wklejka

Alternatywy:

zsh
zsh -c 'foo="Filemon"; echo ${(L)foo}'
python
# python
Python 2.6.6 (r266:84292, Feb 21 2013, 19:26:11)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print unicode.lower("Filemon".decode('utf-8'))
filemon

filon92

  • Gość
polecenie tr
« Odpowiedź #4 dnia: 2013-07-02, 10:10:57 »
Dziala! Dzieki Wielkie! :)
Mam juz ostatnie pytanie.
Otoz w jaki sposob moge
if grep -x "$s" /usr/share/dict/words > /dev/null
W tym fragmencie kodu zrobic tak, aby uzytkonik przy pomocy parametru mogl zmieniac ta sciezke do pliku /usr/share/dict/words na dowolna inna? Czy jest to mozliwe?

Offline Lord Darius

  • Guru
  • *****
  • Wiadomości: 1162
    • Zobacz profil
polecenie tr
« Odpowiedź #5 dnia: 2013-07-02, 15:31:08 »
@filon92
Kolejne pytanie = nowy wątek na forum.

Ten zamykam.
Spójrz bez strachu na rzecz budzącą strach, a straszność sama zniknie.
cat /etc/debian_version