"Stupid" Unix/Linux problem plus Mac OS X

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    "Stupid" Unix/Linux problem plus Mac OS X

    I'm tearing my hair out over this one. It used to be the case that in Unix one could split a line of text up into words - one per line - very easily - possibly in more than one way.

    One way, which should work, is to use sed (the stream editor). It should be possible to issue a command (either verbatim, or similar to):

    sed "s/ /\n/g" <inputfile

    but this and many other similar commands steadfastly refuse to work in Mac OS 10.6.
    It's possible to get the spaces substituted, but the line feed metacharacter \n refuses to work. I've tried putting various flags on, as recommended on different sites. Zilch!

    I get the same result if I put the code into a shell script, or if I use source to run the script file. Changing the quotes doesn't do anything useful either - just in case any form of command substitution is taking place on the command line.

    I've also tried changing the text file data in the file inputfile to use the line endings for different systems Unix, Windows, Mac - but to no avail.

    Is this just Apple Mac OS being awkward? Is there a solution? What have Apple done to achieve this non-result?

    #2
    s/\ /\\n/g
    escape space + \n

    Comment


      #3
      Originally posted by Frances_iom View Post
      s/\ /\\n/g
      escape space + \n
      Francis

      Glad you're interested enough to respond. Thanks a lot.

      Unfortunately - this is where I'm at currently.....

      #! /usr/bin/sh
      #sed 's/ /\n/g'
      sed s/\ /\\\\n/g
      #sed ':escape: :space: + \n'

      Mac OS "Unix" claims to be POSIX compliant, though I'm not quite sure what the impact of that is. I think there are some matching features I'm not so aware of.

      With the command % echo "this is a test of " | source sed1 (where sed1 is the file shown above)
      the response is:

      this\nis\na\ntest\nof\n

      I get the same result if I redirect the output to a file.

      I can get the substitution of the spaces to occur, but not the output of a "real" newline character.

      \n just results in n being output. I've tried varying numbers of slashes \ but the results are either ns or \ns, which makes sense when you consider what they mean.

      I've been using Unix for many years, but never seen a problem like this one before.

      If anyone can get this working in a Mac OS X terminal I'd be very grateful to know how. I feel sure that in proper Unix or Linux it would be easy, and could be fixed in a few seconds.

      Comment


        #4
        1st s/i/e/ please
        I merely run linux + have a hatred of walled gardens - but double backlash n is needed as it appears that the sed string is being parsed - you could try quoting the whole sed command if what is being done is to pass it to shell to execute

        Comment


          #5
          sed 'y/ /\n/' may do what you want. No MAC to test it but it works in BSD.

          Comment


            #6
            Originally posted by OldTechie View Post
            sed 'y/ /\n/' may do what you want. No MAC to test it but it works in BSD.
            Does work inded, thank you.

            From the man pages ...


            [2addr]y/string1/string2/

            Replace all occurrences of characters in string1 in the pattern
            space with the corresponding characters from string2. Any char-
            acter other than a backslash or newline can be used instead of a
            slash to delimit the strings. Within string1 and string2, a
            backslash followed by an ``n'' is replaced by a newline charac-
            ter. A pair of backslashes is replaced by a literal backslash.
            Finally, a backslash followed by any other character (except a
            newline) is that literal character.
            Now I can do the kinds of things I was hoping to demonstrate -

            % source sedrun | sort | uniq

            where sedrun contains sed 'y/ /\n/'


            I wonder why they used 'y' for this operation. I was sure in the past 's' would have done. Heyho!

            Comment


              #7
              Looking at the text for s/BRE/replacement/flags it does not support \n meaning a newline. However you can do it. You have to escape a newline character:

              At shell level :

              # sed 's/ /\
              > /g' myfile.txt

              Or make a text file sedcmd:
              s/ /\
              /g

              then
              # sed -f sedcmd myfile.txt

              or your sedrun as
              sed -f sedcmd

              then your echo test sample works for me.

              P.S.

              Even simpler make sedrun as
              sed 's/ /\
              /g'

              That's it!

              Comment

              Working...
              X