[二星]格式化输出,欢迎各种脚本或者语言

除了美化之外,还可以来尝试挑战一下任务
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: ★★格式化输出,欢迎各种脚本或者语言

#46

帖子 eexpress » 2009-09-24 12:04

其实我早发过了。就那opera通讯录拨号的帖子。

效果嘛。有opera的,试试就知道了。
● 鸣学
pom
帖子: 14
注册时间: 2009-09-24 9:34

Re: ★★格式化输出,欢迎各种脚本或者语言

#47

帖子 pom » 2009-09-26 16:31

cnkilior 写了:

代码: 全选

#!/bin/sh

#ALL Variables 
cleaner(){
     ID=
     NAME=
    .
    .
    .   #需要补全,我偷个懒好吧!
}
writer(){
echo ${NAME},$PHONE >>whereyouneed
}
while read LINE;do 
    case "$LINE" in
         "")
             continue
             ;;
          \#CONTACT)
                writer
                cleaner
                ;;
            *)
                 eval $LINE
                 ;;
        esac
done

#remove the head.
sed -i '^,$/d' whereyouneed

不错
wenjianhn
帖子: 583
注册时间: 2008-10-15 10:49
来自: CS

Re: ★★格式化输出,欢迎各种脚本或者语言

#48

帖子 wenjianhn » 2009-09-30 21:12

lilydjwg 写了:

代码: 全选

#!/usr/bin/env python3
# fileencoding=utf-8

name = phone = None
with open('data.txt') as f:
  for i in f:
    if i.find('#CONTACT') != -1:
      another = True
      name = phone = None
    if i.startswith('NAME'):
      name = i[5:-1]
    if i.startswith('PHONE'):
      phone = i[6:-1]
    if another and name and phone:
      print(name, phone, sep=',')
      another = False

学习了

代码: 全选

 _____________
< 呜呜buntu >
 -------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
头像
lyh
帖子: 113
注册时间: 2007-05-26 20:57
联系:

Re: [二星]格式化输出,欢迎各种脚本或者语言

#49

帖子 lyh » 2009-10-01 15:50

代码: 全选

cat $1 |             # $1 输入文件
  awk 'BEGIN { RS = "#CONTACT"; FS = "\n"; OFS = ":" } {$1 = $1; print }' |
    sed -n -e 's/:.*:NAME=\([^/@:]*\):.*:PHONE=\(1[0-9]\{10\}\):.*/\1 : \2/p'
我也发一个练练手 :em02
头像
一无所有
帖子: 21
注册时间: 2009-02-28 20:55
系统: Debian
联系:

Re: [二星]格式化输出,欢迎各种脚本或者语言

#50

帖子 一无所有 » 2009-11-04 19:28

我用C写了一个:

代码: 全选

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
        FILE *input_file;
        input_file = fopen("input.txt", "rt");
        if (input_file == NULL) {
                printf("input.txt文件不存在!");
                return 1;
        }

        int string_length_limit;
        printf("请输入每一行字符个数的上限:");
        scanf("%d", &string_length_limit);
        string_length_limit++;

        char person_tag[] = "#CONTACT";
        char name_tag[] = "NAME=";
        char phone_tag[] = "PHONE=";
        char one_line[string_length_limit];
        char name[string_length_limit];
        char phone[string_length_limit];
        int is_phone_effective = 0, is_name_effective = 0;

        while (fgets(one_line, string_length_limit, input_file) != NULL) {
                if (strcmp(person_tag, one_line) == 0) {
                        if (is_phone_effective == 1 && is_name_effective == 1) {
                                printf("%s,%s\n", name, phone);
                        }
                        is_name_effective = 0, is_phone_effective = 0;
                } else if (strncmp(name_tag, one_line, 5) == 0) {
                        is_name_effective = 1;
                        strcpy(name, one_line + 5);
                        name[strlen(name) - 1] = '\0';
                } else if (strncmp(phone_tag, one_line, 6) == 0) {
                        is_phone_effective = 1;
                        strcpy(phone, one_line + 6);
                        phone[strlen(phone) - 1] = '\0';
                }
        }
        if (is_phone_effective == 1 && is_name_effective == 1) {
                printf("%s,%s\n", name, phone);
        }

        fclose(input_file);
        return 0;
}
wind2
帖子: 9
注册时间: 2009-11-23 22:47

Re: [二星]格式化输出,欢迎各种脚本或者语言

#51

帖子 wind2 » 2009-11-24 12:00

那我来java好了 C++的就省略了 前面有C的了
脚本语言不会 好想学 很好很强大

代码: 全选

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class Case {

	public static void main(String[] args) {
		System.out.println(System.getProperty("user.dir"));
		String rfilename = System.getProperty("user.dir") + "/import.txt";
		String wfilename = System.getProperty("user.dir") + "/output.txt";
		File rfile = new File(rfilename);
		File wfile = new File(wfilename);
		if (!rfile.exists()) {
			System.err.println("Can't Find " + rfilename);
			return;
		}
		BufferedReader br;
		BufferedWriter bw;
		String linestr, mail = "", phone = "";
		String[] splitstr;
		try {
			br = new BufferedReader(new FileReader(rfile));
			bw = new BufferedWriter(new FileWriter(wfile, false));
			linestr = br.readLine();
			while (linestr != null) {
				if (linestr.equals("#CONTACT")) {
					mail = "";
					phone = "";
				}
				splitstr = linestr.split("=");
				if (splitstr[0].equals("MAIL"))
					mail = splitstr[1];
				if (splitstr[0].equals("PHONE"))
					phone = splitstr[1];
				if (!mail.equals("") && !phone.equals("")) {
					System.out.println(mail + "," + phone);
					bw.append(mail + "," + phone);
					bw.newLine();
					bw.flush();
					mail = "";
					phone = "";
				}
				linestr = br.readLine();
			}
			bw.close();
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
2fish
帖子: 26
注册时间: 2008-11-22 19:28

Re: [二星]格式化输出,欢迎各种脚本或者语言

#52

帖子 2fish » 2009-12-02 16:59

代码: 全选

#/usr/bin/python

data = open("task01-data.txt",'r')
name=''
phone=''
for lines in data.readlines():
    if 'NAME' in lines and 'SHORT' not in lines:
        name=lines.split('=')[1].strip()
    if 'PHONE' in lines:
        phone=lines.split('=')[1]
        if name!='' and phone!='':
            print "%s:%s"%(name,phone)
        name=''
        phone=''
Full Circle中文杂志 |Ubuntu社区独立发行的电子月刊
http://sites.google.com/site/fullcirclezh/
water_snow
帖子: 6
注册时间: 2009-11-17 19:37

Re: [二星]格式化输出,欢迎各种脚本或者语言

#53

帖子 water_snow » 2010-01-07 18:05

代码: 全选

awk 'BEGIN {RS="\n\n"}/\nNAME.*PHONE/' filename 
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

Re: ★★格式化输出,欢迎各种脚本或者语言

#54

帖子 tusooa » 2010-06-30 15:46

eexpress 写了:多谢各位研究。

残局结束了。谢谢各位让我知道,我选择了最正确的perl。

4行有效代码。

代码: 全选

● cat t.pl
#!/usr/bin/perl

while(<>){
chomp;
$name=$',next if /NAME=/;
print $name.",".$'."\n" if /PHONE=/ && $';
}
:em11
(忍不住了,挖一个坟,哈哈)
代码是可能出错误的。

代码: 全选

[tlcr: 0] [五月十九日 15:08:08] [tusooa@tusooa-laptop] [~]
>> cat > /tmp/testfile << 'EOF'
#CONTACT
ID=454
NAME=Fred Weinhaus
CREATED=1251770526
MAIL=fmw@alink.net
ICON=Contact0

#CONTACT
ID=455
NAME=binsos@googlegroups.com
CREATED=1253364946
MAIL=binsos@googlegroups.com
DESCRIPTION=
SHORT NAME=
PHONE=1xxxxx999901
ICON=Contact0

heredoc> #CONTACT
heredoc> ID=x
heredoc> NAME=y
heredoc> 
heredoc> #CONTACT
heredoc> ID=x
heredoc> PHONE=z
heredoc> EOF
[tlcr: 0] [五月十九日 15:09:10] [tusooa@tusooa-laptop] [~]
>> perl /tmp/perl-test /tmp/testfile 
,1xxxxx999901
y,z
上面的NAME和下面的PHONE居然对应了。

现在爽了,4行:

代码: 全选

[tlcr: 0] [五月十九日 17:32:52] [tusooa@tusooa-laptop] [~]
>> cat 应用/脚本/GetValue
#!/usr/bin/env perl

use 5.010;

while(chomp($_ = <>))
{
    if (/^(#CONTACT)$/) { say "$NAME,$PHONE" if $NAME and $PHONE; undef $NAME; undef $PHONE;}
    for $a qw(NAME PHONE) { ${$a} = $' if /^$a=/; }
}
say "$NAME,$PHONE" if $NAME and $PHONE;

代码: 全选

] ls -ld //
头像
linxiaoyu
帖子: 39
注册时间: 2009-05-03 21:09

Re: [二星]格式化输出,欢迎各种脚本或者语言

#55

帖子 linxiaoyu » 2010-12-21 13:35

最近在学脚本,来论坛找任务做一下,我的脚本,写的比较烂,高手莫笑

代码: 全选

test.awk
BEGIN{RS="#";FS="\n";ORS="\n"}
{for (i=1;i<=NF;i++){if ($i ~ /^NAME/){printf("%s",substr($i,6))};if ($i ~/^PHONE/){printf(",%s",substr($i,7))}};printf("\n")}
nawk -f test.awk file | nawk '/,/{print}' 
GONE WITH THE WIND ~~~
pengqian
帖子: 120
注册时间: 2010-12-23 10:23

Re: ★★格式化输出,欢迎各种脚本或者语言

#56

帖子 pengqian » 2011-01-20 5:57

roylez 写了:死臭屁,烂大括号恶心的。

代码: 全选

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
for l in open(sys.argv[1]).readlines():
    if l.strip().startswith('NAME'): name = l.strip().split('=')[1]
    if l.strip().startswith('PHONE'):   print l.strip().split('=')[1] +','+name
最近在学python, 这个好懂哈~ 原来string 还有 startwith() 这种method 啊
头像
liu滔
帖子: 7212
注册时间: 2010-10-09 23:01

Re: [二星]格式化输出,欢迎各种脚本或者语言

#57

帖子 liu滔 » 2011-01-20 10:34

51楼前面所有的import应该可以换成一句``import java.io.*;''的吧…… :em06
头像
lilydjwg
论坛版主
帖子: 4249
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: ★★格式化输出,欢迎各种脚本或者语言

#58

帖子 lilydjwg » 2011-01-20 13:27

pengqian 写了: 最近在学python, 这个好懂哈~ 原来string 还有 startwith() 这种method 啊
你英文没学好啊,是 startswith()
头像
liu滔
帖子: 7212
注册时间: 2010-10-09 23:01

Re: [二星]格式化输出,欢迎各种脚本或者语言

#59

帖子 liu滔 » 2011-01-20 17:01

最近在学Perl和Java,不过Java学得不怎么样,虽然说Perl也学得不怎么样,不过还是来献一下丑,献上这里最长的Perl代码 :em06
[perl]
#! /usr/bin/perl -w
while (<>) {
if (/^(NAME|PHONE)=/) {
chomp($_);
push(@list, $_);
}
}
for ($i = 0; $i < @list; $i++) {
if ($list[$i] =~ /^NAME/) {
@name = split(/=/, $list[$i]);
if ($list[$i + 1] =~ /^PHONE/) {
@phone = split(/=/, $list[$i + 1]);
if (defined($phone[1])) {
print("$name[1], $phone[1]\n");
}
}
}
}
[/perl]
各位别笑话我哦~ :em03
经过本人测试,绝对可用,并且不会将两个不同的联系人的NAME项和PHONE项混在一起…… :em02
头像
Yume
帖子: 1015
注册时间: 2010-05-24 12:16

Re: [二星]格式化输出,欢迎各种脚本或者语言

#60

帖子 Yume » 2011-01-21 7:01

膜拜各种高手,新人还在学习中...
回复