Find the answer to your Linux question:
Results 1 to 2 of 2
Hi all, I have two files, where the 1. file has special lines that need to be updated by lines from a 2. file: file1: foo foo foo 0.00 0.00 ...
  1. #1
    Just Joined!
    Join Date
    Jan 2010
    Posts
    6

    Replacing lines with pattern partialy by different file

    Hi all,

    I have two files, where the 1. file has special lines that need to be updated by lines from a 2. file:
    file1:
    foo
    foo
    foo 0.00 0.00 0.00 pattern
    foo
    foo 1.00 1.00 1.00 pattern
    foo
    file2:
    2.00 2.00 2.00
    3.00 3.00 3.00
    The last three columns of each line containing a"pattern" in file1 must be replaced by three columns of the corresponding line in file2. The result would be:
    file1:
    foo
    foo 2.00 2.00 2.00 keyword
    foo
    foo 3.00 3.00 3.00 keyword
    foo
    I found already how to delete a complete line with a "pattern":
    Code:
    sed "/pattern/d" file1
    But here I need to delete only the last three columns of a matched line. Is it possible to combine sed and awk to achieve this?

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Something like:
    Code:
    awk '/pattern/{getline s < "file2"; $0=$1 FS s FS k}1' k="keyword" file1 > newfile

Posting Permissions

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