Find the answer to your Linux question:
Results 1 to 4 of 4
hi there, i need a help with replacing, let's say, all 'L' to 'C' in a large file (project.dat), but only for certain line numbers. let's say we have an ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    11

    search and replace for certain lines

    hi there,

    i need a help with replacing, let's say, all 'L' to 'C' in a large file (project.dat), but only for certain line numbers. let's say we have an index file with the line numbers we are interested to be changed (project.idx):
    2
    18
    ...

    then the project.dat file looks like follows:
    L 1 2 3 4 5 6
    L 1 2 3 4 5 6
    L 1 2 3 4 5 6
    ....

    i need to replace 'L' to 'C' in lines 2, 18, etc. what i can do by myself:
    Code:
    while read line
    do
        sed "$line{s/L/C/}" project.dat > project.tmp
        mv project.tmp project.dat
    done < project.idx
    
    rm -f project.tmp
    but the problem is that this script is too slow for large files *.idx and *.dat
    is there an more optimal way to do it? thank you.

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    See post # 7 at sed in a for loop - The UNIX and Linux Forums for a procedure that creates a script that will then perform a single pass over the data file ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Just Joined!
    Join Date
    Dec 2008
    Posts
    11
    drl, thank you so much. it worked!

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi, mors123456.

    You're welcome, good to hear that you got it working ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

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