[질문] 죄송하지만 코드 좀. 부탁드립니다.

leolo의 이미지

특정 파일의 문자열을 찾아서 치환하는 코드 좀 부탁드립니다.
예를 들어.

a.txt 라는 파일을 읽어서
SECTION의 값을 123으로 치환하는 함수입니다.

파일 a.txt의 내용이 아래와 같다면..
#
#
TYPE=2
SECTION=3
#


#
#
TYPE=2
SECTION=123
#
으로 변경하는 코드입니다.

C로 부탁드립니다.

Fe.head의 이미지

cat a.txt | sed 's/^SECTION=[0-9]*/SECTION=123/' > a1.txt

하면 될것 같은데요.

물론 테스트는 안해 봤습니다.

고작 블로킹 하나, 고작 25점 중에 1점, 고작 부활동
"만약 그 순간이 온다면 그때가 네가 배구에 빠지는 순간이야"

lacovnk의 이미지

<?
$input = file('a.txt');
$output = array();
foreach($input as $cur_line)
{
     if('SECTION' == substr($cur_line,0,6)){$cur_line = 'SECTION=123';}
     array_push($output,$cur_line);     
}
fwrite('a.txt',implode('\n',$output);
?>

물론 테스트는 안해 봤습니다.

tinywolf의 이미지

하핫.. c빼고 다 올라올 것같은 느낌이.. 짖굳으셔라~

ㅡ_ㅡ;

eminency의 이미지

tinywolf wrote:
하핫.. c빼고 다 올라올 것같은 느낌이.. 짖굳으셔라~

전례가 몇 번 있지요, 아마...? ㅡㅡ

노루가 사냥꾼의 손에서 벗어나는 것 같이, 새가 그물치는 자의 손에서 벗어나는 것 같이 스스로 구원하라 -잠언 6:5

cppig1995의 이미지

fehead wrote:
cat a.txt | sed 's/^SECTION=[0-9]*/SECTION=123/' > a1.txt

전 더 짖궃습니다.
드디어 고대하던 C 버전!

#include <stdlib.h>

int main()
{
   return system("cat a.txt | sed 's/^SECTION=[0-9]*/SECTION=123/' > a1.txt");
}

Real programmers /* don't */ comment their code.
If it was hard to write, it should be /* hard to */ read.

wfellow의 이미지

# cd school
# cd report
# vi a.txt

-----[꼬릿말 절취선 시작]-----
삽질전에 먼저 구글신께 기도하자.
-----[꼬릿말 절취선 끝]-----

익명 사용자의 이미지

#!/usr/bin/perl

my %FETCH;
my %REPLACE = ('SECTION' => '123');

$^I = ".bak";


while(<>)
{
	chomp;
	$FETCH{$`} = $REPLACE{$'} || $'  if(/=/);
	print $`,"=",$FETCH{$`},"\n";
}
		
	
luscent의 이미지

#!/usr/bin/perl

my %FETCH;
my %REPLACE = ('SECTION' => '123');

$^I = ".bak";


while(<>)
{
	chomp;
	$FETCH{$`} = $REPLACE{$`} || $'  if(/=/);
	print $`,"=",$FETCH{$`},"\n";
}
		
	

실행은 perl this.file 데이터 파일[/code]

luscent의 이미지

#!/usr/bin/perl 

my %FETCH; 
my %REPLACE = ('SECTION' => '123'); 

$^I = ".bak"; 


while(<>) 
{ 
   chomp; 
   if(/=/)
   {
   		$FETCH{$`} = $REPLACE{$`} || $'  ; 
   		print $`,"=",$FETCH{$`},"\n"; 
   		next;
   }
   print $_,"\n";
   	
   
} 

앗 실수

$`가 전역 변수라서

결과는
#
TYPE=2
SECTION=123
#

퇴근전 심심해서 뭐 할 거 없나 했는데. 잘됐네요.[/code]

kane의 이미지

open Str

val _ = let
  fun change f = let 
      val l = input_line f
      val re = regexp "\(^SECTION=\).*"
      val s = if string_match re l 0 
                then (replace_first re "\1" l)^"123" 
                else l
    in
      s::(change f)
    end 
    handle End_of_file => nil

  fun write f (s::sl) = output_string f (s^"\n") ; write f sl
    | write f nil = ()

  val fin = open_in "a.txt"
  val a = change fin
  val fout = open_out "a.txt"
  val b = write fout a
in
  ()
end

nML 버전입니다.
요새 요놈으로 숙제 중이라..
lovethecorners의 이미지

저도 C 는 아닙니다....그래도 한번

perl -pi -e "s/SECTION=3/SECTION=123/g" a.txt

꽤 지났네요....머쓱

익명 사용자의 이미지

박장대소!

댓글 달기

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