따라서 내 file.txt의 내용은 다음과 같습니다.
Symbol Name Sector Market Cap, $K Last Links
AAPL
Apple Inc
Computers and Technology
2,006,722,560
118.03
AMGN
Amgen Inc
Medical
132,594,808
227.76
AXP
American Express Company
Finance
91,986,280
114.24
다음을 달성하려면 이것을 사용해야 합니다.
Symbol,Name,Sector,Market Cap $K,Last
AAPL,Apple Inc,Computers and Technology,2006722560,118.03
AMGN,Amgen Inc,Medical,132594808,227.76
AXP,American Express Company,Finance,91986280,114.24
나는 이런 것을 시도했다
sed 's/, / /g' table1.txt | tr "\t" " " | cut -d " " -f 1-6 | tr "\n" ","
출력 포함
Symbol Name Sector Market Cap $K Last, AAPL,Apple Inc,Computers and Technology,2,006,722,560,118.03, AMGN,Amgen Inc,Medical,132,594,808,227.76, AXP,American Express Company,Finance,91,986,280,114.24,
하지만 이것은 내가 기대했던 것과 다르며 어떻게 진행해야 할지 모르겠습니다.
답변1
열 헤더 문자열 사이에 탭이 있다고 가정합니다.
$ cat tst.awk
BEGIN { FS="\t"; OFS="," }
{ gsub(OFS,"") }
NR==1 {
gsub(/[[:space:]]+[^[:space:]]+$/,"")
numLines = NF
$1 = $1
print
next
}
{
lineNr = (NR-2) % numLines + 1
gsub(/^[[:space:]]+|[[:space:]]+$/,"")
rec = (lineNr == 1 ? "" : rec OFS) $0
if ( lineNr == numLines ) {
print rec
}
}
$ awk -f tst.awk file
Symbol,Name,Sector,Market Cap $K,Last
AAPL,Apple Inc,Computers and Technology,2006722560,118.03
AMGN,Amgen Inc,Medical,132594808,227.76
AXP,American Express Company,Finance,91986280,114.24
답변2
한 가지 방법은 다음과 같습니다.
$ awk '{
if(NR==1){
sub(/,/,"");
gsub(/ */,",");
print
}
else{
if(NR%5==2){
if(NR>2){print ""}
printf "%s,",$0
}
else{
printf "%s,",$0
}
}
}
END{print ""}' file
Symbol,Name,Sector,Market Cap $K,Last,Links
AAPL,Apple Inc,Computers and Technology,2,006,722,560,118.03,
AMGN,Amgen Inc,Medical,132,594,808,227.76,
AXP,American Express Company,Finance,91,986,280,114.24,
,
후행 및 선행 공백을 제거하기 위해 일부 후처리를 추가할 수 있습니다 .
$ awk '{ if(NR==1){sub(/,/,""); gsub(/ */,","); print}else{ if(NR%5==2 ){ if(NR>2){print ""}printf "%s,",$0}else{printf "%s,",$0}}}END{print ""}' file | sed 's/^ *//; s/,$//'
Symbol,Name,Sector,Market Cap $K,Last,Links
AAPL,Apple Inc,Computers and Technology,2,006,722,560,118.03
AMGN,Amgen Inc,Medical,132,594,808,227.76
AXP,American Express Company,Finance,91,986,280,114.24
답변3
GNU 사용 sed
:
sed -z '
s/,//g;
# remove all commas
s/\n\([^[[:blank:]]\)/,\1/g;
# replace "\n" +a non-Tab/Space char with a comma and revert back char itself
s/[[:blank:]][[:blank:]]\+/,/g;
# replace repeated Tabs/Spaces with a comma
' infile
주석 처리되지 않은 명령 및 선행 공백 제거:
sed -z '
s/,//g;
s/\n\([^[[:blank:]]\)/,\1/g;
s/[[:blank:]][[:blank:]]\+/,/g; s/\n[[:blank:]]\+/\n/g;
' infile