Find the answer to your Linux question:
Results 1 to 5 of 5
hi, I have 4 files like: file1 (contains texts) aaaaa bbbbb ccccc file2 (contains texts) ccccccc rrrrrrrrrrr dddddd file3 (contains number of lines) 1 3 file4 (contains number of lines) ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2012
    Posts
    56

    replace text using number of lines


    hi,

    I have 4 files like:

    file1 (contains texts)
    aaaaa
    bbbbb
    ccccc

    file2 (contains texts)
    ccccccc
    rrrrrrrrrrr
    dddddd

    file3 (contains number of lines)
    1
    3

    file4 (contains number of lines)
    2
    3

    I want to replace the 1st (the 1st line of file3) line of the file 2 with the 2nd line of the file1. Then the 3rd (the sedond line of file4) line of the file 2 with the 3nd line of the file1 and so on.
    Could you help me?
    Thanks in advance.

  2. #2
    Just Joined! mrbruno's Avatar
    Join Date
    Jan 2013
    Location
    /MilkyWay/Sol/Earth/USA/NC/Raleigh
    Posts
    66
    Let me make sure I understand what you want:

    • Lines from file2 will be replaced lines from file1
    • Lines from file1 are never replaced with lines from file2
    • Files 3 and 4 will have the same number of lines


    Is my understanding correct? Let me know if you have different requirements.

    How do you want the result? Do you want file2 replaced with the updates? Do you want the new file to be dumped to stdout?

  3. #3
    Just Joined!
    Join Date
    Apr 2012
    Posts
    56
    hi,

    yes, your understanding is good. I don't have problem with the output.
    Thanks!

  4. #4
    Just Joined! mrbruno's Avatar
    Join Date
    Jan 2013
    Location
    /MilkyWay/Sol/Earth/USA/NC/Raleigh
    Posts
    66
    How about something like:

    Code:
    $ cat replacer
    awk "$@" '
    function load(varname, filename, container) {
      if (filename == "") {
        system("echo " varname " is not set >&2")
        exit(1)
      }
      if (system("test -f " filename) != 0) {
        system("echo " filename " \\(" varname "\\) not found >&2")
        exit(1)
      }
      container[0] = 0
      while (getline line < filename) {
        container[++container[0]] = line
      }
      close(filename)
    }
    
    BEGIN {
      load("old_lines", old_lines, OLD_LINES)
      load("new_lines", new_lines, NEW_LINES)
      load("old_linenums", old_linenums, OLD_LINENUMS)
      load("new_linenums", new_linenums, NEW_LINENUMS)
    }
    
    END {
      for (i=1; i<=OLD_LINENUMS[0]; i++)
        OLD_LINES[OLD_LINENUMS[i]] = NEW_LINES[NEW_LINENUMS[i]] 
    
      for (i=1; i<=OLD_LINES[0]; i++)
        print OLD_LINES[i]
    }' /dev/null
    $ head file*
    ==> file1 <==
    aaaaaa
    bbbbbb
    cccccc
    
    ==> file2 <==
    ccccccccc
    rrrrrrrrr
    ddddddddd
    
    ==> file3 <==
    1
    3
    
    ==> file4 <==
    2
    3
    $ ./replacer -v new_lines=file2 -v old_lines=file1 -v old_linenums=file3 -v new_linenums=file4
    rrrrrrrrr
    bbbbbb
    ddddddddd
    $

  5. #5
    Just Joined!
    Join Date
    Apr 2012
    Posts
    56
    Thank you! It works perfectly!

Posting Permissions

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