Find the answer to your Linux question:
Results 1 to 2 of 2
I need to replace a line in a file, example test.file: Code: cat test.file DBAN 196157 5419883235 364173045 932 43933467 4915 256 21538970 4291 288 12197694 2809 1044 14041695 2782 ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    10

    replace a line in a file

    I need to replace a line in a file, example test.file:

    Code:
    cat test.file
    
    DBAN 196157 5419883235 364173045 932 43933467 4915 256 21538970 4291 288 12197694 2809 1044 14041695 2782 95 4526567 488 0 0 0 75 7120403 1678 43
    CDALA 196157 5419883235 364173045 932 43933467 4915 256 21538970 4291 288 12197694 2809 1044 14041695 2782 95 4526567 488 0 0 0 75 7120403 1678 43
    CSI 196157 5419883235 364173045 932 43933467 4915 256 21538970 4291 288 12197694 2809 1044 14041695 2782 95 4526567 488 0 0 0 75 7120403 1678 43
    I'd have to replace always the second number from a specific line in a file, like:

    Code:
    grep CDALA test.file | awk '{print $3}
    and then replace it with for example 2425212475 so the file would be:

    Code:
    cat test.file
    
    DBAN 196157 5419883235 364173045 932 43933467 4915 256 21538970 4291 288 12197694 2809 1044 14041695 2782 95 4526567 488 0 0 0 75 7120403 1678 43
    CDALA 196157 2425212475 364173045 932 43933467 4915 256 21538970 4291 288 12197694 2809 1044 14041695 2782 95 4526567 488 0 0 0 75 7120403 1678 43
    CSI 196157 5419883235 364173045 932 43933467 4915 256 21538970 4291 288 12197694 2809 1044 14041695 2782 95 4526567 488 0 0 0 75 7120403 1678 43

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    try this:

    grep CDALA <filename> | awk '{sub ($3, "2425212475");print}'

    man awk
    the sun is new every day (heraclitus)

Posting Permissions

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