쉘 정렬질문입니다

asdf132456의 이미지

prac1.txt
1234 3 kim
3232 2 choi
3222 8 kap
4123 7 soo

prac2.txt
1211 3 lee
1212 6 smith
1338 4 paa
1337 5 james

를 세로로 붙인다음 정렬하여 숫자4개와 숫자1개를 출력하여 저장하려고합니다
#!/bin/bash
rm abc.txt
rm sum.txt
rm aaa.txt
join -a 1 -a 2 -1 1 prac1.txt prac2.txt > abc.txt
i=0

sort abc.txt > abc1.txt
while read line
do

#array[$i]=$line|cut -d ' ' -f1
echo "count =" $i
echo $line|cut -d ' ' -f1 -f2|tee echo $1 : $2 >>aaa.txt
#echo ${array[$i]}

((i++))
done < abc1.txt

이렇게 코드를 짜긴했는데 aaa.txt의 출력은
1211 3
1212 6
1234 3
1337 5
1338 4
3222 8
3232 2
4123 7
이렇게나오네요 어떻게해야 :이 나올수있을지 질문드립니다

ymir의 이미지

$ cat prac1_sorted.txt
1234 3 kim
3222 8 kap
3232 2 choi
4123 7 soo
$ cat prac2_sorted.txt
1211 3 lee
1212 6 smith
1337 5 james
1338 4 paa
$ join -a 1 -a 2 -1 1 prac1_sorted.txt prac2_sorted.txt | sort | \
> sed 's/ /:/g' | cut -d':' -f1-2
1211:3
1212:6
1234:3
1337:5
1338:4
3222:8
3232:2
4123:7
$ join -a 1 -a 2 -1 1 prac1_sorted.txt prac2_sorted.txt | sort | \
> while read -r line; do set -a $line; echo $1:$2; done
1211:3
1212:6
1234:3
1337:5
1338:4
3222:8
3232:2
4123:7
$ join -a 1 -a 2 -1 1 prac1_sorted.txt prac2_sorted.txt | sort | \
> while read -r line; do declare -a arr=($line); echo ${arr[0]}:${arr[1]}; done
1211:3
1212:6
1234:3
1337:5
1338:4
3222:8
3232:2
4123:7

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

김정균의 이미지

[root@host ~]$ cat prac*.txt | sort -V | awk '{ print $1 ":" $2 }'
1211:3
1212:6
1234:3
1337:5
1338:4
3222:8
3232:2
4123:7
bushi의 이미지

$ cat prac1.txt prac2.txt | while read num4 num1 etc; do echo $num4:$num1; done | sort -t: -k1
1211:3
1212:6
1234:3
1337:5
1338:4
3222:8
3232:2
4123:7
파이썬3의 이미지

# -*- coding: utf-8 -*-
 
f1 = open("prac1.txt", "r")
f2 = open("prac2.txt", "r")
 
d1 = f1.read(); f1.close()
d2 = f2.read(); f2.close()
 
d11 = d1.splitlines(False)
d22 = d2.splitlines(False)
 
d111 = [":".join(x.split()[:-1]) for x in d11]
d222 = [":".join(x.split()[:-1]) for x in d22]
 
data = sorted(d111+d222)
 
for line in data: print(line)
파이썬3 의 이미지

# -*- coding: utf-8 -*-
 
f1 = open("prac1.txt", "r")
f2 = open("prac2.txt", "r")
 
d1 = f1.read().strip(); f1.close()
d2 = f2.read().strip(); f2.close()
 
d11 = d1.splitlines(False)
d22 = d2.splitlines(False)
 
data = sorted(d11+d22)
 
data = [":".join(x.split()[:-1]) for x in data]
 
for line in data: print(line)
황병희의 이미지

# -*- coding: utf-8 -*-
 
flists = [
    "prac1.txt",
    "prac2.txt",
    "prac3.txt",
]
tlines = []
 
def asdf(x):
    f = open(x, "r")
    data = f.read().strip(); f.close()
    data = data.splitlines(False)
    data = [":".join(k.split()[:-1]) for k in data]
 
    for k in data:
        tlines.append(k)
 
if __name__ == "__main__":
    for file in flists: asdf(file)
    for line in sorted(tlines): print(line)

--
^고맙습니다 감사합니다_^))//

익명 사용자의 이미지

파이선 나왔는데 루비가 빠지면 섭하죠.. ㅋㅋ

~$ cat prac1.txt
1234 3 kim
3232 2 choi
3222 8 kap
4123 7 soo
 
~$ cat prac2.txt
1211 3 lee
1212 6 smith
1338 4 paa
1337 5 james
 
~$ cat sort.rb

#!/usr/bin/ruby
 
a = IO.readlines("prac1.txt")
b = IO.readlines("prac2.txt")
c = (a + b).sort!
 
c.each do |line|
  d = line.split
  puts d[0] + ":" + d[1]
end

~$ ruby sort.rb 
1211:3
1212:6
1234:3
1337:5
1338:4
3222:8
3232:2
4123:7
파이썬3의 이미지

루비 코드가 군더더기 없이 간결하고 아름답네여~ ^^^

t2space의 이미지

$ sed 's/\(^[0-9]\+\)[[:blank:]]\+\([0-9]\)\+.*$/\1:\2/' prac1.txt prac2.txt | sort
파이썬3의 이미지

음... cat 으로 시작하는 한줄 권법이 참 무섭네여,,, 포스를 느끼고 갑니다,,, 좋은 코드 보여주셔서 감사드립니다^^^

[우분투 18.04 파여폭스 나비에서 작성했습니다]

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.