c쉘에서 문자를 숫자로 취급하는 방법?

ytt123의 이미지

C쉘 스크립트 만드는 중인데요,

파일에서 받아들인 문자를 숫자로 취급할 수 있나요?

set aa = `cut -c 1-3 file.list`
@ bb = $aa + 5
echo $bb

하면 30 으로 나오네요 ^^;; 36 (=31+5) 으로 나와야 하는데 말이죠.
아마도 031을 31이란 숫자로 인식하지 않고, 문자로 인식해서 생기는 문제 같습니다.
031을 숫자 31로 인식하게 하는, 방법이 있을까요?

cat file.list
031-234-2345
131-234-2345
231-234-2345
..

raymundo의 이미지

C쉘은 모릅니다만 30이 나온 이유는 문자라기보다는 0으로 시작하는 숫자라서 8진수 031, 즉 10진수 25로 인식해서일 것 같네요. 그걸 고려해서 덧셈 전에 $aa 에서
앞의 0을 제거하는 방법을 찾아보는 것도 해결책이 될 수 있겠네요.

좋은 하루 되세요!

ytt123의 이미지

문자로 인식된 aa를 아래처럼 하니 숫자로(앞에 0이 제거된) 인식이 되네요.
set bb = `echo | awk -v temp="${aa}" '{printf("%g", temp);}'`

p.s. 6년전 제가 올린 질문이었는데 시간(세월...)이 흐른 후 이제 답글을 달게 되네요.ㅎㅎ

김정균의 이미지

bc 나 expr 을 이용하시면 됩니다.

aa="031"
 
c=$(echo "${aa}" | sed -r 's/^0+//g')
let "b = ${c} + 5"
 
b=$(bc <<< "${aa} + 5")
echo $b
 
b=$(echo "${aa} + 5" | bc)
echo $b
 
b=$(expr ${aa} + 5)
echo $b
ytt123의 이미지

깔끔하게 만들 수 있네요.

파이썬3의 이미지

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
 
# 참고문헌: <a href="https://kldp.org/node/136970
#" rel="nofollow">https://kldp.org/node/136970
#</a> 각 줄의 앞문자열 3자를 문자형에서 정수형으로...
# 그리고 각 항목에다 5 를 더한후 출력...
"""
$ cat file.list
031-234-2345
131-234-2345
231-234-2345
331-234-2345
441-234-2345
$
"""
 
f = open("file.list", "r")
lines = f.readlines()
 
aa = []
for x in lines:
    aa.append(x[0:3])
 
for bb in aa:
    print(int(bb)+5) 
 
f.close()
 
"""
테스트: 우분투 18.04, 파이썬 3.6.7
 
(bionic)soyeomul@localhost:~/test$ ./11.py
36
136
236
336
446
(bionic)soyeomul@localhost:~/test$ 
"""
bushi의 이미지

bash 입니다.

IFS=$'-'
while read a b c;
do
  let a=${a/#0/}+5
  echo $a
done < file.list

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.