Announcement

Collapse
No announcement yet.

Text Replace Help - Change apple to "apple", ??

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Text Replace Help - Change apple to "apple", ??

    Text Replace Help - Change apple to "apple", ??

    Hi,

    My team and I are working on a new HTML5/JS word game now.
    We need help in formatting the dictionary files for use in JS.

    In Kubuntu 14.04 L.T.S. 64Bit, how can we scan a dictionary text file
    and change the words from example:
    apple
    to
    "apple",
    for each line in the dictionary text file?

    Dictionary text file contains all words for a specific letter.
    (one word per line)

    Thanks!

    JeZxLee

    #2
    The sed command has a "replace in-place" function. Assume your dictionary file is dict.txt;

    sed -i 's/apple/"apple",/' dict.txt

    will do it. sed will also make a backup file if you specify an extension thusly:

    sed -i.bak 's/apple/"apple",/' dict.txt

    If you file has one entry per line, you just need a do loop to read each line.

    Please Read Me

    Comment


      #3
      Originally posted by oshunluvr View Post
      The sed command has a "replace in-place" function. Assume your dictionary file is dict.txt;

      sed -i 's/apple/"apple",/' dict.txt

      will do it. sed will also make a backup file if you specify an extension thusly:

      sed -i.bak 's/apple/"apple",/' dict.txt

      If you file has one entry per line, you just need a do loop to read each line.
      Hi,

      Thanks for the information.
      Can you write an example that reads and changes each line of a text file?
      We are a little confused as to how to do it properly.
      Thanks!

      JeZxLee

      Comment


        #4
        I got this to work;

        while read p; do r="$p",; sed -i "s/$p/$r/" dict.txt; done <dict.txt

        Here's my test fie and result from the above command;
        Code:
        stuart@office:~$ cat dict.txt
        apple
        banana
        pear
        stuart@office:~$ while read p; do r=\"$p\",; sed -i "s/$p/$r/" dict.txt; done <dict.txt
        stuart@office:~$ cat dict.txt
        "apple",
        "banana",
        "pear",
        stuart@office:~$
        To explain a bit about the above command: I couldn't get it to work exactly as I had in my first post because sed won't read variables between single quotes ('). This meant I had to use double quotes for the sed command (") but that confuses everything because the OP wanted double quotes in the output. So by using two variables - $p for the input line and $r for the output line - I could use double quotes in the sed command to get the desired results.

        The backslash in the $r variable tells bash not to interpret the next character, just insert it (like you would to use a space in a bash command). This produces the desired result.

        NOTE: This should effect every line in the file so be careful if you want to do otherwise.
        Last edited by oshunluvr; Jan 14, 2015, 12:47 PM.

        Please Read Me

        Comment


          #5
          I'd make a backup first...

          Please Read Me

          Comment


            #6
            Originally posted by oshunluvr View Post
            I got this to work;
            while read p; do r="$p",; sed -i "s/$p/$r/" dict.txt; done <dict.txt
            That's not good. Aside from a few errors, (it adds a comma, doesn't actually quote anything, and fails if one word is contained in another),1 it is, ahem, prohibitively inefficient, and might not work, depending on how your shell buffers stuff. I let it run for a few minutes on a copy of words.txt, a file used by lots of online games such as "words for friends", and it got as far as "abdominal", thrashing the disc mercilessly.

            If the task is to put quotes around all the words in a file, that has one word per line,
            Code:
            sed -i 's/.*/"&"/' dict.txt
            runs in a few seconds (on words.txt).

            1I suppose the intent was something like
            while read p;do r=\"\$p\";sed -i "s/^$p$/$r" dict.txt;done <dict.txt
            Regards, John Little

            Comment


              #7
              To " oshunluvr" and " jlittle",

              Thank you for your help!
              I used LibreOffice Calc to change the words.

              I added you both to the staff screen of the game.
              You can see your names by visiting the following web page URL link:
              http://lettersfall.com
              (click on [About] button on main title screen)

              Thanks!

              JeZxLee

              Last edited by JeZ-l-Lee; Jan 25, 2015, 01:31 PM.

              Comment


                #8
                Very interesting thread, a short question, is the loop that is being discussed similar to the ANCIENT..."for-next" loop of the old BASIC? For ussens of a previous generation. lol

                woodsmoke

                Comment


                  #9
                  Jlittle: Mine does add the quotes and the comma as intended and requested by the OP, but, I see I didn't account for "a" appearing as a word in a list and thus causing a mess - which is repeated every time the matching word "a" is found, and so on - as you pointed out. So it works as long as the list is unique and thus isn't very useful. I didn't even think about getting a larger file to test on as I should have. Obviously, I'm still new to sed, awk, cut, etc., but they an be quite fun and useful.

                  Yours works great, except of course you left out the comma that was asked for. I didn't know about the .* insert which makes all the difference in the world.

                  I did some more playing about and found this works too:

                  sed -i -e 's/^/"/' -e 's/$/",/' dict.txt

                  simply adding a quote to the beginning and a quote+comma to the end of each line. Yours worked a few milliseconds faster on the words.txt file.

                  For more fun, I tested both commands on a much larger file - 1688004 lines - and at that size your single operation took two seconds less than my two operation command.

                  Please Read Me

                  Comment


                    #10
                    Originally posted by oshunluvr View Post
                    Jlittle: Mine does add the quotes and the comma as intended and requested by the OP,
                    Sorry, I missed the comma requirement, I read it as the grammar of the message. I should have looked more closely at the example.

                    Looking more closely at your post #4, in the bold type on the second line you don't escape the quotes, but you do in the code box, I didn't see that.

                    But what moved me to comment was the "quadratic" behaviour of processing the whole file for each word in the file. Might work on a small example, but doesn't scale. I was also worried about changing the file that the while loop was reading, been bitten by that, but a close look at the man page for sed -i indicates the edited version is renamed over the old, which leaves the old file existing as a ghost (not appearing in any directory) until it is closed so that's not a problem.

                    However, text editors do this kind of thing as their bread and butter, and I should have advised the OP so; the OP's solution was akin to that, though LibreOffice Writer would have been simpler and faster I imagine. But what text editor to suggest? Nano hung when I gave it this task. I use vim myself, but I'm not sure vim would suit the OP. It took me half an hour to learn to drive kate to do this global search and replace, and even then I had to be careful to stop it processing every file in the same directory!

                    Anyway, OP, if you are doing this sort of thing even a little, learning a text editor will pay dividends in not very long.
                    Regards, John Little

                    Comment

                    Working...
                    X