Find the answer to your Linux question:
Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By histrungalot
Hi to all, I've searched the forums and the web and come up with only close-but-no-cigar answers. I am trying to replace every kth line in my file with: echo ...
  1. #1
    Just Joined!
    Join Date
    Jul 2009
    Posts
    4

    sed every nth line replace

    Hi to all,

    I've searched the forums and the web and come up with only close-but-no-cigar answers.

    I am trying to replace every kth line in my file with:
    echo -n ##
    where the ## is not the line number but a multiple of say 5.

    So my file looks like this (but with say 40 lines):
    Code:
    exiftool -shuttercount 607.JPG | cut -c 35-38 >> res.txt
    exiftool -shuttercount 615a.JPG | cut -c 35-38 >> res.txt
    exiftool -shuttercount 623.JPG | cut -c 35-38 >> res.txt
    exiftool -shuttercount 625.JPG | cut -c 35-38 >> res.txt
    exiftool -shuttercount 626.JPG | cut -c 35-38 >> res.txt
    and I want it to look like:
    Code:
    exiftool -shuttercount 615a.JPG | cut -c 35-38 >> res.txt
    exiftool -shuttercount 623.JPG | cut -c 35-38 >> res.txt
    echo -n 5...
    exiftool -shuttercount 625.JPG | cut -c 35-38 >> res.txt
    exiftool -shuttercount 624.JPG | cut -c 35-38 >> res.txt
    exiftool -shuttercount 622.JPG | cut -c 35-38 >> res.txt
    exiftool -shuttercount 623.JPG | cut -c 35-38 >> res.txt
    exiftool -shuttercount 628.JPG | cut -c 35-38 >> res.txt
    echo -n 10...
    exiftool -shuttercount 725.JPG | cut -c 35-38 >> res.txt
    exiftool -shuttercount 726.JPG | cut -c 35-38 >> res.txt
    exiftool -shuttercount 722.JPG | cut -c 35-38 >> res.txt
    exiftool -shuttercount 723.JPG | cut -c 35-38 >> res.txt
    exiftool -shuttercount 728.JPG | cut -c 35-38 >> res.txt
    echo -n 15...
    I managed to get it close but with 5, 10, 15... respectively 6, 12, 18... because the lines of the numbers are being counted and diverge from what they should be. I'm also missing both the echo part and the numbering together though I imagine I can handle that.

    Alternatively I tried putting the echo part in first (via sed every Kth line) and then appending the number, but again same issue. The echo takes up a line number.

    I do not want to use a loop, but would kinda like a few lines of sed or awk (fewer the better).

    Alternatively I'm trying to run the command:
    Code:
    exiftool -shuttercount IMAGE.JPG | cut -c 35-38
    On every file in an arbitrary directory which would give me a 4 digit number and then find the maximum of all of them.
    I have manged to do this but it's slow and the first problem generates an on-screen real-time counter. (Incidentally I have solved it using +s instead of numbers, but I'd like to know without having to multiply or count.) Is there a better way? Should I be using arrays? Right now I've been using temp files and shuffling instructions back and forth to create a batch file that runs.

    Any help is appreciated.

  2. #2
    Just Joined!
    Join Date
    Aug 2011
    Posts
    48
    I think I can do it with awk, if that helps. Where ## is 5 but you could make it anything:
    Code:
    awk 'BEGIN{x=1}{if ( (FNR%5)==0) print $0"\necho -n "x"...";else print $0}{x=x+1}' yourFile
    Last edited by histrungalot; 02-01-2012 at 02:24 PM.
    mellofellow likes this.

  3. #3
    Just Joined!
    Join Date
    Jul 2009
    Posts
    4
    Not, you think you can do...you DID!

    Thank you for your immense help and rapid response.

    Very appreciated.

Posting Permissions

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