Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Move a line in a file

  1. #11
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Move a line in a file

    I believe this ed should do it. Note that it edits the file in-place.
    Code:
    printf "%s\n" "/^build/d" "/^source/x" "w" | ed -s file.txt
    It sends three commands to ed.
    Code:
    /^build/d  - find the first line that mathces "^build" and delete it (The line's content is stored in the cut buffer)
    
    /^source/x - find the first line that matches "^source" and paste the contents of the cut buffer below it.
    
    w          - write the changes

  2. #12
    Join Date
    Nov 2006
    Location
    There and back again
    Beans
    1,097

    Re: Move a line in a file

    Quote Originally Posted by ghostdog74 View Post
    Code:
    awk '/build/{
        # if build is found
        f=0    #remove flag
        print $0 # print the current line
        for(i=1;i<=d;i++){ print a[i]  } #print the array of lines after source  and before build...
    Not sure how the array understands to the source line, but thanks for the explanation. I'm getting to like how awk works.

    Quote Originally Posted by benj1 View Post
    how did you call it ?, it should be
    Code:
    thisscriptfile.awk foofile.txt
    ...
    Yeah, I called this from my script path which is a hidden folder. I'm thinking your script is interpretting the . as a regexp variable???

    Quote Originally Posted by geirha View Post
    I believe this ed should do it. Note that it edits the file in-place.
    Code:
    printf "%s\n" "/^build/d" "/^source/x" "w" | ed -s file.txt
    It sends three commands to ed...[/code]
    Yeah, I originally wrote the thread trying to remember this command. I had seen 'ed' before but didn't remember it. +1 for helping me remember this and for ease of use.

    I've run into a bit of a problem though. At times, (I've discovered) the source and build lines expand over more lines. For example:

    Code:
    source=('78905905f5a4ed82160c327f3fd34cba'
            '5277a9164001a4276837b59dade26af2'
            '3f8b60b6fbb993c18442b62ea661aa6b')
    
    build=('a-file-name'
           'another-file-name')
    I've tried using the ed command and defining a range:

    Code:
    printf "%s\n" "/^build/,/)/d" "/^source/,/)/x" "w" | ed -s file.txt
    with no luck. Can I do this with 'ed'??? Or anyone know a way to get awk to do it??

  3. #13
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Move a line in a file

    Quote Originally Posted by Gen2ly View Post
    Code:
    source=('78905905f5a4ed82160c327f3fd34cba'
            '5277a9164001a4276837b59dade26af2'
            '3f8b60b6fbb993c18442b62ea661aa6b')
    
    build=('a-file-name'
           'another-file-name')
    ... Or anyone know a way to get awk to do it??
    you will need extra check, since your build ends with ), you can check whether it spans another line.
    Code:
    /build/ && !/\)$/ {
      # if build found but does not end in )
      s=$0
      getline #get the next line
      s=s$0 # join them
      # if you are sure those statements don't span more than 2 lines
      print s
    }
    /build/ && /\)$/{
      .......
    }

Page 2 of 2 FirstFirst 12

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
  •