
나는 사용하려고
nl -ba -s '\t' full_media > full_media2
그러나 탭 문자를 추가하는 것이 아니라 실제로 줄 번호 뒤에 "\t" 텍스트를 추가합니다. 제가 편집하고 있는 파일은 CSV 파일이고 여기에서는 처음에 ID가 있는 새 열을 추가하려고 합니다.
답변1
bash 또는 zsh와 같은 최신 셸을 사용하는 경우 $
셸을 사용하여 \t
이를 평가하고 실제 탭으로 바꿉니다.
nl -ba -s $'\t' full_media > full_media2
그럼에도 불구하고 출력을 확인하면 기본 구분 기호가 탭임을 알 수 있습니다.
$ nl -ba -s $'\t' ~/at.sh | od -c
0000000 1 \t n o h u p s g
$ nl -ba ~/at.sh | od -c
0000000 1 \t n o h u p s g
실제로 POSIX에 따르면 기본 구분 기호는 탭 문자입니다. ~에서man nl
:
-s sep
Specify the characters used in separating the line number and
the corresponding text line. The default sep shall be a <tab>.
CSV에 열을 추가하려면 Python을 사용해 보세요.
#! /usr/bin/env python2
from sys import stdin, stdout
import csv
csvin = csv.reader(stdin, delimiter='\t')
csvout= csv.writer(stdout, delimiter='\t')
count = 1
for row in csvin:
csvout.writerow ([count] + row)
count = count + 1
이를 스크립트(예: nl.py
)로 저장하고 다음을 실행합니다.
python2 nl.py < full_media > full_media2