Results 1 to 5 of 5

Thread: changing word's position in file

  1. #1
    Join Date
    Aug 2008
    Beans
    31

    changing word's position in file

    Hi, people.

    I have a large txt file composed of quotes and their authors, but I need to change the position of the author's name.

    So, every quote is in this format:
    (tab)Nobody likes me
    Author: Tolstoy , Leon (4 spaces)
    And I need them to be like this:
    Nobody likes me.
    Leon Tolstoy
    The various quotes are placed just one after another, with just a simple carriage return between them (ie, no blank line, nothing).
    I need to:
    . remove the TAB before the quote
    . put a period after the quote
    . remove the "Author: "
    . change the names' position
    . remove the 4 spaces from after the name

    I apologize if I don't know how to do this, but I've already tryied to learn SED but it's beyond my brain capacity...

    Thanks a lot!

  2. #2
    Join Date
    Apr 2007
    Beans
    122
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: changing word's position in file

    Here's how you can do it in perl (you might have to adapt a little bit if the spacing is off):

    Code:
    my $in_file = "test.txt";
    my $out_file = "test_out.txt";
    
    my $str;
    open (INPUT, $in_file) or die "can't open $in_file: $!";
    while (<INPUT>) {
    	$str = $str . $_;
    }
    close (INPUT);
    
    $str =~ s/\t(.+?\s+?)Author:\s(.\w+?)\s,\s(\w+?)\s{4}/$1\n$3 $2/sg;
    
    open (OUTPUT, ">$out_file") or die "can't open $out_file: $!";
    print OUTPUT $str;
    close (OUTPUT);

  3. #3
    Join Date
    Dec 2006
    Beans
    7,349

    Re: changing word's position in file

    Hi,

    I can't quite get it with a purely sed usage:

    Code:
    cat file | sed -e 's/$/\./g' -e 's/^\t//g' \
    -e 's/ \{4\}\.$//g' -e 's/Author: //g' \
    -e  's/ ,//g'
    But this does not transpose the final names and the business with the full stops is a bit ugly . I would love to see a purely sed version that reverses these names. Anyone?

    Andrew
    You think that's air you're breathing now?

  4. #4
    Join Date
    Dec 2006
    Beans
    7,349

    Re: changing word's position in file

    Hi,

    OK so I got a little direction from comp.unix.shell concerning the ttransformation of names and the final purely sed solution is as follows:

    Code:
       cat in_file | \
       sed -e 's/$/\./g' -e 's/^\t//g' \
           -e 's/ \{4\}\.$//g' \
           -e 's/Author: \([a-zA-Z]*\) , \([a-zA-Z]*\)/\2 \1/g' \
           > out_file
    I love sed when it works so well .

    Andrew
    You think that's air you're breathing now?

  5. #5
    Join Date
    Jun 2007
    Location
    UK
    Beans
    1,386
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: changing word's position in file

    You might find it easier to use awk rather than sed. Try the following:
    Code:
    awk '/^\t/ {sub("\t",""); print $0 "."}
         /Author:/ {print $4 " " $2}' input.txt > output.txt

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •