1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Ksh mystery: how are newlines represented in files?

  1. Dec 19, 2003 #1
    Please take a look at these lines which demonstrate my problem in a korn shell terminal:

    First I defined var with a newline embedded in it. I printed it and, as expected, got
    abc
    def.
    I printed the same thing to file1.

    Next, I edited it using the pattern operator ${var//\\n\} to remove the newline, printed it and again got what I expected. Now var is
    abcdef

    Then, I replaced var with the contents of file1: var=$(<file1)
    Printed it, and again there's
    abc
    def

    So far, so good.
    Now, I try to edit it again with exactly the same command var=${var//\\n/}
    But it has no effect. Var still prints as
    abc
    def

    What's going on here? Is the newline represented differently in file1? How? How can I edit it out?

    One other observation:
    I noticed that in the FIRST instance, after defining var="abc\ndef", if I entered
    print $var
    I got
    abc def
    Only by entering
    print "$var"
    would I get
    abc
    def

    But after reading it back in from the file, it prints as
    abc
    def
    whether I enter
    print "$var"
    or
    print $var
     
  2. jcsd
  3. Dec 19, 2003 #2
    Well, I found a solution using the cat -E option that gives me a way to edit out the mystery newline character without knowing exactly what it is. For example:

    $ var="abc\ndef"
    $ print "$var"
    abc
    def
    $ print "$var" > file1
    $ cat file1
    abc
    def
    $ var=$(cat -E file1)
    $ print "$var"
    abc$
    def$
    $ var=${var//\$?/}
    $ var=${var//\$*/}
    $ print "$var"
    abcdef

    But if anybody knows what that character is & how to edit it out of a variable without going through this merry-go-round procedure, please let me know.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Ksh mystery: how are newlines represented in files?
  1. How to view .he5 file? (Replies: 3)

  2. How to read a gz file? (Replies: 5)

  3. How to open .py file? (Replies: 9)

Loading...