ページ 1 / 1
文字列の中から1文字のものを除去or空白文字に置き換える考え
Posted: 2012年12月20日(木) 00:48
by blade
先ほどここで質問した者です。
要求が変わって、またわからなくなったので質問させていただきます。
あるテキストデータがあるとします。例えば以下の様なものなのですが
コード:
zoom v 3 3 @ ~ + 3 1 02059445 02060133 01947577
zoom_along v 1 1 ! @ 1 0 02059445
zoom_in v 1 1 @ 1 0 02157683
この中から各々の行において1文字だけで構成されている箇所を消したいのですがいい方法はありますでしょうか。
単語、数字間はスペースで開けたいです。
つまり、出力結果は
コード:
zoom 02059445 02060133 01947577
zoom_along 02059445
zoom_in 02157683
のようにしたいと考えています。
・単語 1文字群 8桁の数字 の順番は統一されています。
・“8桁の数字”というのは統一されています。
・その数は単語毎にバラバラです(1~8個ほど)。
・1文字で表されるものはアルファベット、数字、記号と様々です。
・その数は単語毎にバラバラです(3~10個ほど)。
・1行毎のはじめにある英単語は1つだけとなっています。
Re: 文字列の中から1文字のものを除去or空白文字に置き換える考え
Posted: 2012年12月20日(木) 07:27
by みけCAT
適材適所です。
コード:
#!/usr/bin/perl
while(<STDIN>) {
chomp($_);
$data=$_;
$data =~ s/ / /g;
$data=" $data ";
$data =~ s/ \S //g;
$data =~ s/^ //;
$data =~ s/ $//;
$data =~ s/ / /g;
$data =~ s/ / /; # 最初の単語の後のスペースを2個にする
print "$data\n";
}
Re: 文字列の中から1文字のものを除去or空白文字に置き換える考え
Posted: 2012年12月20日(木) 08:58
by non
すべての語句で改行して良いなら、
while (fscanf(fp,"%s",onaji) != EOF) {
に変更するだけ。
しかし、改行を考慮するなら、もう一工夫必要。
Re: 文字列の中から1文字のものを除去or空白文字に置き換える考え
Posted: 2012年12月20日(木) 12:28
by box
仕様を満たしているかどうか、保証の限りではありません。
コード:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
char str[128]; // 大きさは適切に確保すること
FILE *fp;
fp = fopen("test.txt", "r");
if (fp == NULL) {
fprintf(stderr, "file open error.\n");
exit(1);
}
while (fgets(str, sizeof(str), fp) != NULL) {
char *p, *dlm = " ";
str[strlen(str)-1] = '\0';
p = strtok(str, dlm);
while (p != NULL) {
if (strlen(p) != 1) {
printf("%s%s ", p, (isalpha(*p) ? " " : ""));
}
p = strtok(NULL, dlm);
}
putchar('\n');
}
fclose(fp);
return 0;
}
Re: 文字列の中から1文字のものを除去or空白文字に置き換える考え
Posted: 2012年12月20日(木) 12:57
by blade
みけCAT さんが書きました:適材適所です。
コード:
#!/usr/bin/perl
while(<STDIN>) {
chomp($_);
$data=$_;
$data =~ s/ / /g;
$data=" $data ";
$data =~ s/ \S //g;
$data =~ s/^ //;
$data =~ s/ $//;
$data =~ s/ / /g;
$data =~ s/ / /; # 最初の単語の後のスペースを2個にする
print "$data\n";
}
回答ありがとうございます。
perlはほとんど触ったことがなく、ネットで調べながらみけCATさんのコードをいじったのですがうまく出力されません(以下のコード)。
ファイルから一行ずつ読み込み、同じ処理を行おうと思ったのですが、空白行が出力されただけになってしまいました。
どこが悪いのか教えてください、よろしくお願いします。
※sample.txtは初めの質問に書いたzoomから始まる3行のテキストです。
コード:
#!/usr/bin/perl
open(DATAFILE, "sample.txt") or die("error :$!");
while (my $line = <DATAFILE>){
chomp($line);
$data=$line;
$data =~ s/ / /g;
$data=" $data ";
$data =~ s/ \S //g;
$data =~ s/^ //;
$data =~ s/ $//;
$data =~ s/ / /g;
$data =~ s/ / /; # 最初の単語の後のスペースを2個にする
}
close(DATAFILE);
Re: 文字列の中から1文字のものを除去or空白文字に置き換える考え
Posted: 2012年12月20日(木) 13:50
by かずま
Linux か、Windows の cygwin なら、sed が使えます。
コード:
$ cat test.txt
zoom v 3 3 @ ~ + 3 1 02059445 02060133 01947577
zoom_along v 1 1 ! @ 1 0 02059445
zoom_in v 1 1 @ 1 0 02157683
$ sed ':a;s/ [^ ] / /;ta;s/ / /' test.txt
zoom 02059445 02060133 01947577
zoom_along 02059445
zoom_in 02157683
$
Re: 文字列の中から1文字のものを除去or空白文字に置き換える考え
Posted: 2012年12月20日(木) 13:53
by みけCAT
bladeさんのコードだとデータを出力していません。
コード:
$data =~ s/ / /; # 最初の単語の後のスペースを2個にする
の後に
を追加してください。
Re: 文字列の中から1文字のものを除去or空白文字に置き換える考え
Posted: 2012年12月20日(木) 14:05
by blade
みけCAT さんが書きました:bladeさんのコードだとデータを出力していません。
コード:
$data =~ s/ / /; # 最初の単語の後のスペースを2個にする
の後に
を追加してください。
なぜかいじったときに消してしまっていたようです(汗)
単純な質問でしたのに答えていただきありがとうございました。
追加したらうまくいきました。
Re: 文字列の中から1文字のものを除去or空白文字に置き換える考え
Posted: 2012年12月21日(金) 02:16
by かずま
scanf と printf だけで書いてみました。変数は w 1個だけ。
コード:
#include <stdio.h>
int main(void)
{
char w[256];
while (scanf("%255s", w) == 1) {
printf("%s ", w);
while (scanf("%*[ ]"), scanf("%255[^ \n]", w) == 1)
if (w[1]) printf(" %s", w);
if (scanf("%c", w) == 1) printf("\n");
}
return 0;
}
Re: 文字列の中から1文字のものを除去or空白文字に置き換える考え
Posted: 2012年12月22日(土) 03:25
by かずま
sed の代わりに awk を使うと、
コード:
$ cat test.txt
zoom v 3 3 @ ~ + 3 1 02059445 02060133 01947577
zoom_along v 1 1 ! @ 1 0 02059445
zoom_in v 1 1 @ 1 0 02157683
$ awk '{sub(/( [^ ])+ /," ");print}' test.txt
zoom 02059445 02060133 01947577
zoom_along 02059445
zoom_in 02157683
$
Re: 文字列の中から1文字のものを除去or空白文字に置き換える考え
Posted: 2012年12月22日(土) 03:58
by かずま
awk の代わりに perl を使うと、
コード:
$ cat test.txt
zoom v 3 3 @ ~ + 3 1 02059445 02060133 01947577
zoom_along v 1 1 ! @ 1 0 02059445
zoom_in v 1 1 @ 1 0 02157683
$ perl -p -e 's/( \S)+ / /' test.txt
zoom 02059445 02060133 01947577
zoom_along 02059445
zoom_in 02157683
$
Re: 文字列の中から1文字のものを除去or空白文字に置き換える考え
Posted: 2012年12月23日(日) 20:05
by かずま
getchar() と putchar() だけで書いてみました。
コード:
#include <stdio.h>
int sep(int c) { return c == ' ' || c == '\n' || c == EOF; }
int skip(void) { int c; while ((c = getchar()) == ' ') ; return c; }
int word(void) { int c; while (!sep(c = getchar())) putchar(c); return c; }
int main(void) {
int c, c2;
for ( ; (c = word()) != EOF; putchar('\n'))
for (putchar(' '); c != '\n' && c != EOF; )
if ((c = skip()) != '\n' && !sep(c2 = getchar()))
putchar(' '), putchar(c), putchar(c2), c = word();
return 0;
}
Re: 文字列の中から1文字のものを除去or空白文字に置き換える考え
Posted: 2012年12月24日(月) 12:29
by blade
他にこんなにいろいろな方法を示していただき、ありがとうございました。