Copy line in text file and add modified line to the end of line in same file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I would like to copy line in text file edit it on the fly to replace all / to | and paste in on the same line in the same line.



I have the first line in the text file:



http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg


Copy text between (name-never-changes) and .jpg



/aCcboeasdfdRD/asdfasdft21


Change to / to |



|aCcboeasdfdRD|asdfasdft21


And add to the end of the same line where URL is, with a separator
the separator can be text or number or special character



http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (separator) |aCcboeasdfdRD|asdfasdft21


And this should work with multiple text files with different URLs



I can't create new files and merge them this should be on the fly because the name of text files are different and I don't know filenames










share|improve this question























  • separator can be text or number or special character
    – unknown
    Sep 2 at 19:59










  • will the URL always have 4 fields?
    – msp9011
    Sep 2 at 20:03










  • url have always 4 fields
    – unknown
    Sep 2 at 20:04















up vote
0
down vote

favorite












I would like to copy line in text file edit it on the fly to replace all / to | and paste in on the same line in the same line.



I have the first line in the text file:



http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg


Copy text between (name-never-changes) and .jpg



/aCcboeasdfdRD/asdfasdft21


Change to / to |



|aCcboeasdfdRD|asdfasdft21


And add to the end of the same line where URL is, with a separator
the separator can be text or number or special character



http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (separator) |aCcboeasdfdRD|asdfasdft21


And this should work with multiple text files with different URLs



I can't create new files and merge them this should be on the fly because the name of text files are different and I don't know filenames










share|improve this question























  • separator can be text or number or special character
    – unknown
    Sep 2 at 19:59










  • will the URL always have 4 fields?
    – msp9011
    Sep 2 at 20:03










  • url have always 4 fields
    – unknown
    Sep 2 at 20:04













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I would like to copy line in text file edit it on the fly to replace all / to | and paste in on the same line in the same line.



I have the first line in the text file:



http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg


Copy text between (name-never-changes) and .jpg



/aCcboeasdfdRD/asdfasdft21


Change to / to |



|aCcboeasdfdRD|asdfasdft21


And add to the end of the same line where URL is, with a separator
the separator can be text or number or special character



http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (separator) |aCcboeasdfdRD|asdfasdft21


And this should work with multiple text files with different URLs



I can't create new files and merge them this should be on the fly because the name of text files are different and I don't know filenames










share|improve this question















I would like to copy line in text file edit it on the fly to replace all / to | and paste in on the same line in the same line.



I have the first line in the text file:



http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg


Copy text between (name-never-changes) and .jpg



/aCcboeasdfdRD/asdfasdft21


Change to / to |



|aCcboeasdfdRD|asdfasdft21


And add to the end of the same line where URL is, with a separator
the separator can be text or number or special character



http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (separator) |aCcboeasdfdRD|asdfasdft21


And this should work with multiple text files with different URLs



I can't create new files and merge them this should be on the fly because the name of text files are different and I don't know filenames







linux sed grep paste






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 2 at 21:55









msp9011

3,46643862




3,46643862










asked Sep 2 at 19:54









unknown

305




305











  • separator can be text or number or special character
    – unknown
    Sep 2 at 19:59










  • will the URL always have 4 fields?
    – msp9011
    Sep 2 at 20:03










  • url have always 4 fields
    – unknown
    Sep 2 at 20:04

















  • separator can be text or number or special character
    – unknown
    Sep 2 at 19:59










  • will the URL always have 4 fields?
    – msp9011
    Sep 2 at 20:03










  • url have always 4 fields
    – unknown
    Sep 2 at 20:04
















separator can be text or number or special character
– unknown
Sep 2 at 19:59




separator can be text or number or special character
– unknown
Sep 2 at 19:59












will the URL always have 4 fields?
– msp9011
Sep 2 at 20:03




will the URL always have 4 fields?
– msp9011
Sep 2 at 20:03












url have always 4 fields
– unknown
Sep 2 at 20:04





url have always 4 fields
– unknown
Sep 2 at 20:04











2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Try this,



awk -F '[/.]' '"$(NF-2)"' file

http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (saperatot) |aCcboeasdfdRD|asdfasdft21



  • /. two field separators


  • $0 to print the complete line


  • $(NF-2) $(NF-1) the third and second fields from the end

if you want to save in the file



Option 1: (if it has few lines)



echo "`awk -F '[/.]' '"$(NF-2)"' file`" > file


Option 2: (if it is a large file)



 awk -F '[/.]' '"$(NF-2)"' file > tempFile ; mv tempFile file


For multiple files in a folder:



cd /path/to/dir
for file in `ls`
do
echo "`awk -F '[/.]' '"$(NF-2)"' $file`" > $file
done





share|improve this answer






















  • use for loop.
    – msp9011
    Sep 2 at 20:46










  • What happens if file contains empty line at end? This script dont gonna print in one line only print at second line, how i can deleted second empty line and print in one line
    – unknown
    Sep 4 at 16:17










  • @unknown valid point!! we can validate by NF>2
    – msp9011
    Sep 4 at 16:19











  • source is one line with text which i wanna cpy at the end of same line second line is empty thats why this script print at second line not on same line do You know script to deleted that second line before use script? Im a noob and i dont know what is vaild point
    – unknown
    Sep 4 at 16:33










  • @unknown yes, try the same code with NF>1 like awk -F '[/.]' 'NF>1{print....
    – msp9011
    Sep 4 at 16:48

















up vote
0
down vote













Here’s a sed solution:



sed 'h; s@^[^/]*//[^/]*/[^/]*/@ (separator) |@; s@/@|@g; s/.jpg$//; x; G; s/n//'


Step by step:




  • h copies the current line from the “pattern space” to the “hold space”.


  • s@^[^/]*//[^/]*/[^/]*/@  (separator)  |@ is a substitute command
    using @ as the delimiter. 
    It replaces
    stuff//stuff/stuff/

    (i.e., http://example.com/(top-level-directory)/)
    with   (separator)  |.

    You could do
    s@^http//example.com/(top-level-directory)/@ (separator) |@
    if you wanted to.


  • s@/@|@g is also a substitute command using @ as the delimiter. 
    It replaces all remaining / characters in the line with | characters. 
    If you’re sure that there will be only one
    (between (second-level-directory) and (filename)),
    you can leave off the g.


  • s/.jpg$// obviously removes the .jpg extension from the end of the line. 
    (If you don’t know what the extension is,
    you could do something like s/.[^.]*$// instead.)


  • x exchanges the pattern space and the hold space,
    so the original line is now back in the pattern space, and the
      (separator) |(second-level-directory)|(filename)
    that we just built
    is in the hold space.


  • G gets the contents of the hold space
    and appends it to the pattern space (injecting a newline between them).


  • s/n// removes that newline.

This will pass blank lines through as blank lines. 
Other lines will be mangled.



If you want to edit file(s) in place, just pass sed the -i option
(or -i.bak, if desired or required on your system).






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466442%2fcopy-line-in-text-file-and-add-modified-line-to-the-end-of-line-in-same-file%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Try this,



    awk -F '[/.]' '"$(NF-2)"' file

    http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (saperatot) |aCcboeasdfdRD|asdfasdft21



    • /. two field separators


    • $0 to print the complete line


    • $(NF-2) $(NF-1) the third and second fields from the end

    if you want to save in the file



    Option 1: (if it has few lines)



    echo "`awk -F '[/.]' '"$(NF-2)"' file`" > file


    Option 2: (if it is a large file)



     awk -F '[/.]' '"$(NF-2)"' file > tempFile ; mv tempFile file


    For multiple files in a folder:



    cd /path/to/dir
    for file in `ls`
    do
    echo "`awk -F '[/.]' '"$(NF-2)"' $file`" > $file
    done





    share|improve this answer






















    • use for loop.
      – msp9011
      Sep 2 at 20:46










    • What happens if file contains empty line at end? This script dont gonna print in one line only print at second line, how i can deleted second empty line and print in one line
      – unknown
      Sep 4 at 16:17










    • @unknown valid point!! we can validate by NF>2
      – msp9011
      Sep 4 at 16:19











    • source is one line with text which i wanna cpy at the end of same line second line is empty thats why this script print at second line not on same line do You know script to deleted that second line before use script? Im a noob and i dont know what is vaild point
      – unknown
      Sep 4 at 16:33










    • @unknown yes, try the same code with NF>1 like awk -F '[/.]' 'NF>1{print....
      – msp9011
      Sep 4 at 16:48














    up vote
    1
    down vote



    accepted










    Try this,



    awk -F '[/.]' '"$(NF-2)"' file

    http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (saperatot) |aCcboeasdfdRD|asdfasdft21



    • /. two field separators


    • $0 to print the complete line


    • $(NF-2) $(NF-1) the third and second fields from the end

    if you want to save in the file



    Option 1: (if it has few lines)



    echo "`awk -F '[/.]' '"$(NF-2)"' file`" > file


    Option 2: (if it is a large file)



     awk -F '[/.]' '"$(NF-2)"' file > tempFile ; mv tempFile file


    For multiple files in a folder:



    cd /path/to/dir
    for file in `ls`
    do
    echo "`awk -F '[/.]' '"$(NF-2)"' $file`" > $file
    done





    share|improve this answer






















    • use for loop.
      – msp9011
      Sep 2 at 20:46










    • What happens if file contains empty line at end? This script dont gonna print in one line only print at second line, how i can deleted second empty line and print in one line
      – unknown
      Sep 4 at 16:17










    • @unknown valid point!! we can validate by NF>2
      – msp9011
      Sep 4 at 16:19











    • source is one line with text which i wanna cpy at the end of same line second line is empty thats why this script print at second line not on same line do You know script to deleted that second line before use script? Im a noob and i dont know what is vaild point
      – unknown
      Sep 4 at 16:33










    • @unknown yes, try the same code with NF>1 like awk -F '[/.]' 'NF>1{print....
      – msp9011
      Sep 4 at 16:48












    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    Try this,



    awk -F '[/.]' '"$(NF-2)"' file

    http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (saperatot) |aCcboeasdfdRD|asdfasdft21



    • /. two field separators


    • $0 to print the complete line


    • $(NF-2) $(NF-1) the third and second fields from the end

    if you want to save in the file



    Option 1: (if it has few lines)



    echo "`awk -F '[/.]' '"$(NF-2)"' file`" > file


    Option 2: (if it is a large file)



     awk -F '[/.]' '"$(NF-2)"' file > tempFile ; mv tempFile file


    For multiple files in a folder:



    cd /path/to/dir
    for file in `ls`
    do
    echo "`awk -F '[/.]' '"$(NF-2)"' $file`" > $file
    done





    share|improve this answer














    Try this,



    awk -F '[/.]' '"$(NF-2)"' file

    http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (saperatot) |aCcboeasdfdRD|asdfasdft21



    • /. two field separators


    • $0 to print the complete line


    • $(NF-2) $(NF-1) the third and second fields from the end

    if you want to save in the file



    Option 1: (if it has few lines)



    echo "`awk -F '[/.]' '"$(NF-2)"' file`" > file


    Option 2: (if it is a large file)



     awk -F '[/.]' '"$(NF-2)"' file > tempFile ; mv tempFile file


    For multiple files in a folder:



    cd /path/to/dir
    for file in `ls`
    do
    echo "`awk -F '[/.]' '"$(NF-2)"' $file`" > $file
    done






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 2 at 23:00

























    answered Sep 2 at 20:11









    msp9011

    3,46643862




    3,46643862











    • use for loop.
      – msp9011
      Sep 2 at 20:46










    • What happens if file contains empty line at end? This script dont gonna print in one line only print at second line, how i can deleted second empty line and print in one line
      – unknown
      Sep 4 at 16:17










    • @unknown valid point!! we can validate by NF>2
      – msp9011
      Sep 4 at 16:19











    • source is one line with text which i wanna cpy at the end of same line second line is empty thats why this script print at second line not on same line do You know script to deleted that second line before use script? Im a noob and i dont know what is vaild point
      – unknown
      Sep 4 at 16:33










    • @unknown yes, try the same code with NF>1 like awk -F '[/.]' 'NF>1{print....
      – msp9011
      Sep 4 at 16:48
















    • use for loop.
      – msp9011
      Sep 2 at 20:46










    • What happens if file contains empty line at end? This script dont gonna print in one line only print at second line, how i can deleted second empty line and print in one line
      – unknown
      Sep 4 at 16:17










    • @unknown valid point!! we can validate by NF>2
      – msp9011
      Sep 4 at 16:19











    • source is one line with text which i wanna cpy at the end of same line second line is empty thats why this script print at second line not on same line do You know script to deleted that second line before use script? Im a noob and i dont know what is vaild point
      – unknown
      Sep 4 at 16:33










    • @unknown yes, try the same code with NF>1 like awk -F '[/.]' 'NF>1{print....
      – msp9011
      Sep 4 at 16:48















    use for loop.
    – msp9011
    Sep 2 at 20:46




    use for loop.
    – msp9011
    Sep 2 at 20:46












    What happens if file contains empty line at end? This script dont gonna print in one line only print at second line, how i can deleted second empty line and print in one line
    – unknown
    Sep 4 at 16:17




    What happens if file contains empty line at end? This script dont gonna print in one line only print at second line, how i can deleted second empty line and print in one line
    – unknown
    Sep 4 at 16:17












    @unknown valid point!! we can validate by NF>2
    – msp9011
    Sep 4 at 16:19





    @unknown valid point!! we can validate by NF>2
    – msp9011
    Sep 4 at 16:19













    source is one line with text which i wanna cpy at the end of same line second line is empty thats why this script print at second line not on same line do You know script to deleted that second line before use script? Im a noob and i dont know what is vaild point
    – unknown
    Sep 4 at 16:33




    source is one line with text which i wanna cpy at the end of same line second line is empty thats why this script print at second line not on same line do You know script to deleted that second line before use script? Im a noob and i dont know what is vaild point
    – unknown
    Sep 4 at 16:33












    @unknown yes, try the same code with NF>1 like awk -F '[/.]' 'NF>1{print....
    – msp9011
    Sep 4 at 16:48




    @unknown yes, try the same code with NF>1 like awk -F '[/.]' 'NF>1{print....
    – msp9011
    Sep 4 at 16:48












    up vote
    0
    down vote













    Here’s a sed solution:



    sed 'h; s@^[^/]*//[^/]*/[^/]*/@ (separator) |@; s@/@|@g; s/.jpg$//; x; G; s/n//'


    Step by step:




    • h copies the current line from the “pattern space” to the “hold space”.


    • s@^[^/]*//[^/]*/[^/]*/@  (separator)  |@ is a substitute command
      using @ as the delimiter. 
      It replaces
      stuff//stuff/stuff/

      (i.e., http://example.com/(top-level-directory)/)
      with   (separator)  |.

      You could do
      s@^http//example.com/(top-level-directory)/@ (separator) |@
      if you wanted to.


    • s@/@|@g is also a substitute command using @ as the delimiter. 
      It replaces all remaining / characters in the line with | characters. 
      If you’re sure that there will be only one
      (between (second-level-directory) and (filename)),
      you can leave off the g.


    • s/.jpg$// obviously removes the .jpg extension from the end of the line. 
      (If you don’t know what the extension is,
      you could do something like s/.[^.]*$// instead.)


    • x exchanges the pattern space and the hold space,
      so the original line is now back in the pattern space, and the
        (separator) |(second-level-directory)|(filename)
      that we just built
      is in the hold space.


    • G gets the contents of the hold space
      and appends it to the pattern space (injecting a newline between them).


    • s/n// removes that newline.

    This will pass blank lines through as blank lines. 
    Other lines will be mangled.



    If you want to edit file(s) in place, just pass sed the -i option
    (or -i.bak, if desired or required on your system).






    share|improve this answer
























      up vote
      0
      down vote













      Here’s a sed solution:



      sed 'h; s@^[^/]*//[^/]*/[^/]*/@ (separator) |@; s@/@|@g; s/.jpg$//; x; G; s/n//'


      Step by step:




      • h copies the current line from the “pattern space” to the “hold space”.


      • s@^[^/]*//[^/]*/[^/]*/@  (separator)  |@ is a substitute command
        using @ as the delimiter. 
        It replaces
        stuff//stuff/stuff/

        (i.e., http://example.com/(top-level-directory)/)
        with   (separator)  |.

        You could do
        s@^http//example.com/(top-level-directory)/@ (separator) |@
        if you wanted to.


      • s@/@|@g is also a substitute command using @ as the delimiter. 
        It replaces all remaining / characters in the line with | characters. 
        If you’re sure that there will be only one
        (between (second-level-directory) and (filename)),
        you can leave off the g.


      • s/.jpg$// obviously removes the .jpg extension from the end of the line. 
        (If you don’t know what the extension is,
        you could do something like s/.[^.]*$// instead.)


      • x exchanges the pattern space and the hold space,
        so the original line is now back in the pattern space, and the
          (separator) |(second-level-directory)|(filename)
        that we just built
        is in the hold space.


      • G gets the contents of the hold space
        and appends it to the pattern space (injecting a newline between them).


      • s/n// removes that newline.

      This will pass blank lines through as blank lines. 
      Other lines will be mangled.



      If you want to edit file(s) in place, just pass sed the -i option
      (or -i.bak, if desired or required on your system).






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        Here’s a sed solution:



        sed 'h; s@^[^/]*//[^/]*/[^/]*/@ (separator) |@; s@/@|@g; s/.jpg$//; x; G; s/n//'


        Step by step:




        • h copies the current line from the “pattern space” to the “hold space”.


        • s@^[^/]*//[^/]*/[^/]*/@  (separator)  |@ is a substitute command
          using @ as the delimiter. 
          It replaces
          stuff//stuff/stuff/

          (i.e., http://example.com/(top-level-directory)/)
          with   (separator)  |.

          You could do
          s@^http//example.com/(top-level-directory)/@ (separator) |@
          if you wanted to.


        • s@/@|@g is also a substitute command using @ as the delimiter. 
          It replaces all remaining / characters in the line with | characters. 
          If you’re sure that there will be only one
          (between (second-level-directory) and (filename)),
          you can leave off the g.


        • s/.jpg$// obviously removes the .jpg extension from the end of the line. 
          (If you don’t know what the extension is,
          you could do something like s/.[^.]*$// instead.)


        • x exchanges the pattern space and the hold space,
          so the original line is now back in the pattern space, and the
            (separator) |(second-level-directory)|(filename)
          that we just built
          is in the hold space.


        • G gets the contents of the hold space
          and appends it to the pattern space (injecting a newline between them).


        • s/n// removes that newline.

        This will pass blank lines through as blank lines. 
        Other lines will be mangled.



        If you want to edit file(s) in place, just pass sed the -i option
        (or -i.bak, if desired or required on your system).






        share|improve this answer












        Here’s a sed solution:



        sed 'h; s@^[^/]*//[^/]*/[^/]*/@ (separator) |@; s@/@|@g; s/.jpg$//; x; G; s/n//'


        Step by step:




        • h copies the current line from the “pattern space” to the “hold space”.


        • s@^[^/]*//[^/]*/[^/]*/@  (separator)  |@ is a substitute command
          using @ as the delimiter. 
          It replaces
          stuff//stuff/stuff/

          (i.e., http://example.com/(top-level-directory)/)
          with   (separator)  |.

          You could do
          s@^http//example.com/(top-level-directory)/@ (separator) |@
          if you wanted to.


        • s@/@|@g is also a substitute command using @ as the delimiter. 
          It replaces all remaining / characters in the line with | characters. 
          If you’re sure that there will be only one
          (between (second-level-directory) and (filename)),
          you can leave off the g.


        • s/.jpg$// obviously removes the .jpg extension from the end of the line. 
          (If you don’t know what the extension is,
          you could do something like s/.[^.]*$// instead.)


        • x exchanges the pattern space and the hold space,
          so the original line is now back in the pattern space, and the
            (separator) |(second-level-directory)|(filename)
          that we just built
          is in the hold space.


        • G gets the contents of the hold space
          and appends it to the pattern space (injecting a newline between them).


        • s/n// removes that newline.

        This will pass blank lines through as blank lines. 
        Other lines will be mangled.



        If you want to edit file(s) in place, just pass sed the -i option
        (or -i.bak, if desired or required on your system).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 8 at 0:40









        G-Man

        11.8k92658




        11.8k92658



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466442%2fcopy-line-in-text-file-and-add-modified-line-to-the-end-of-line-in-same-file%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            The Forum (Inglewood, California)

            Palaiologos