Rectangle 27 19

By default, the read builtin allows \ to escape characters. To turn off this behavior, use the -r option. It is not often you will find a case where you do not want to use -r.

string="I'm\nNed\nNederlander
I'm\nLucky\nDay
I'm\nDusty\nBottoms"

arr=()
while read -r line; do
   arr+=("$line")
done <<< "$string"

In order to do this in one-line (like you were attempting with read -a), actually requires mapfile in bash v4 or higher:

mapfile -t arr <<< "$string"

How to split a multi-line string containing the characters "\n" into a...

arrays string bash escaping
Rectangle 27 19

By default, the read builtin allows \ to escape characters. To turn off this behavior, use the -r option. It is not often you will find a case where you do not want to use -r.

string="I'm\nNed\nNederlander
I'm\nLucky\nDay
I'm\nDusty\nBottoms"

arr=()
while read -r line; do
   arr+=("$line")
done <<< "$string"

In order to do this in one-line (like you were attempting with read -a), actually requires mapfile in bash v4 or higher:

mapfile -t arr <<< "$string"

How to split a multi-line string containing the characters "\n" into a...

arrays string bash escaping
Rectangle 27 12

mapfile is more elegant, but it is possible to do this in one (ugly) line with read (useful if you're using a version of bash older than 4):

IFS=$'\n' read -d '' -r -a arr <<< "$string"

One caveat I did not know about when I posted this: the exit status will be non-zero, since a here string will never end with a null character like read -d '' will expect.

How to split a multi-line string containing the characters "\n" into a...

arrays string bash escaping
Rectangle 27 12

mapfile is more elegant, but it is possible to do this in one (ugly) line with read (useful if you're using a version of bash older than 4):

IFS=$'\n' read -d '' -r -a arr <<< "$string"

One caveat I did not know about when I posted this: the exit status will be non-zero, since a here string will never end with a null character like read -d '' will expect.

How to split a multi-line string containing the characters "\n" into a...

arrays string bash escaping
Rectangle 27 80

The -ansi flag is equivalent to -std=c89. As noted, it turns off some extensions of GCC. Adding -pedantic turns off more extensions and generates more warnings. For example, if you have a string literal longer than 509 characters, then -pedantic warns about that because it exceeds the minimum limit required by the C89 standard. That is, every C89 compiler must accept strings of length 509; they are permitted to accept longer, but if you are being pedantic, it is not portable to use longer strings, even though a compiler is permitted to accept longer strings and, without the pedantic warnings, GCC will accept them too.

-ansi turns off some extensions of GCC whereas -pedantic turns off more extensions. It seems like -ansi is first level rule then -pedantic is more restricted rule. Does it mean with this two options remove I can have my code more compatible to other compiler like Microsoft one?

@huahsin68: well, more or less. MSVC is a rather different compiler from most others, more adapted to its particular ecosystem and not available outside it. It does a lot of things its own way - which is not the same as the standard. Nevertheless, if you stay with the standard headers and so on, then MSVC and GCC are fairly similar. However, using -std=c89 -pedantic does mean you can move more easily between different compilers on other platforms. As soon as you start using <windows.h>, compatibility with other systems becomes problematic.

Why is this not defaulted to true? Why have to opt-in for it rather than opt-out?

@slf: Because like every other vendor (albeit that GNU doesn't sell their compiler for cash), they would like you to use their proprietary features? Or, more generally, because they consider the extensions useful and think they should be enabled by default.

For whatever it is worth, and JFTR, I've mostly stopped using -pedantic, but most of my code still compiles OK when I re-enable it (the one program that didn't was explicitly using __int128 types, which are pedantically incorrect). I think there was an intervening stage when GCC was too noisy (for my liking) with -pedantic. I just tested about 300 source files some library code, some commands, some SO test programs and there was just the one to-be-expected problem. Currently using GCC 4.8.2 on Mac OS X 10.9.2.

c++ - What is the purpose of using -pedantic in GCC/G++ compiler? - St...

c++ c gcc g++
Rectangle 27 80

The -ansi flag is equivalent to -std=c89. As noted, it turns off some extensions of GCC. Adding -pedantic turns off more extensions and generates more warnings. For example, if you have a string literal longer than 509 characters, then -pedantic warns about that because it exceeds the minimum limit required by the C89 standard. That is, every C89 compiler must accept strings of length 509; they are permitted to accept longer, but if you are being pedantic, it is not portable to use longer strings, even though a compiler is permitted to accept longer strings and, without the pedantic warnings, GCC will accept them too.

-ansi turns off some extensions of GCC whereas -pedantic turns off more extensions. It seems like -ansi is first level rule then -pedantic is more restricted rule. Does it mean with this two options remove I can have my code more compatible to other compiler like Microsoft one?

@huahsin68: well, more or less. MSVC is a rather different compiler from most others, more adapted to its particular ecosystem and not available outside it. It does a lot of things its own way - which is not the same as the standard. Nevertheless, if you stay with the standard headers and so on, then MSVC and GCC are fairly similar. However, using -std=c89 -pedantic does mean you can move more easily between different compilers on other platforms. As soon as you start using <windows.h>, compatibility with other systems becomes problematic.

Why is this not defaulted to true? Why have to opt-in for it rather than opt-out?

@slf: Because like every other vendor (albeit that GNU doesn't sell their compiler for cash), they would like you to use their proprietary features? Or, more generally, because they consider the extensions useful and think they should be enabled by default.

For whatever it is worth, and JFTR, I've mostly stopped using -pedantic, but most of my code still compiles OK when I re-enable it (the one program that didn't was explicitly using __int128 types, which are pedantically incorrect). I think there was an intervening stage when GCC was too noisy (for my liking) with -pedantic. I just tested about 300 source files some library code, some commands, some SO test programs and there was just the one to-be-expected problem. Currently using GCC 4.8.2 on Mac OS X 10.9.2.

c++ - What is the purpose of using -pedantic in GCC/G++ compiler? - St...

c++ c gcc g++
Rectangle 27 5

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

So use "\n" instead of '\n'

Also, instead of \n you can use PHP_EOL constant. In the Windows "\r\n" can be used as end of line, for this case you can make double replacement:$matches=explode("\n", str_replace("\r","\n",$matches));

php - How to explode a multi-line string? - Stack Overflow

php explode
Rectangle 27 5

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

So use "\n" instead of '\n'

Also, instead of \n you can use PHP_EOL constant. In the Windows "\r\n" can be used as end of line, for this case you can make double replacement:$matches=explode("\n", str_replace("\r","\n",$matches));

php - How to explode a multi-line string? - Stack Overflow

php explode
Rectangle 27 3

When using a multi-line string literal in C# with @", the correct escape sequence for a double-quote becomes "" instead of \".

string templateString = @"
    {0}
    ""{1}""
    {2}
    ";

.net - Putting \" in verbatim string with C# - Stack Overflow

c# .net stringtemplate verbatim-string
Rectangle 27 4

Part of the issue looks to be the use of double quotes which can be escaped with a backslash \, however to have a multi-line string in C#, you also need to append an @ symbol at the start like shown in this answer https://stackoverflow.com/a/1100265/2603735

c# - How to turn plain text json data into string? - Stack Overflow

c# string serialization json.net json-deserialization
Rectangle 27 171

In Eclipse if you turn on the option "Escape text when pasting into a string literal" (in Preferences > Java > Editor > Typing) and paste a multi-lined string whithin quotes, it will automatically add " and \n" + for all your lines.

String str = "paste your text here";

intelij also does this by default when you paste into ""s

Do you generally leave in the \rs that Eclipse puts in on Windows?

Java multiline string - Stack Overflow

java string multiline
Rectangle 27 170

In Eclipse if you turn on the option "Escape text when pasting into a string literal" (in Preferences > Java > Editor > Typing) and paste a multi-lined string whithin quotes, it will automatically add " and \n" + for all your lines.

String str = "paste your text here";

intelij also does this by default when you paste into ""s

Do you generally leave in the \rs that Eclipse puts in on Windows?

Java multiline string - Stack Overflow

java string multiline
Rectangle 27 170

In Eclipse if you turn on the option "Escape text when pasting into a string literal" (in Preferences > Java > Editor > Typing) and paste a multi-lined string whithin quotes, it will automatically add " and \n" + for all your lines.

String str = "paste your text here";

intelij also does this by default when you paste into ""s

Do you generally leave in the \rs that Eclipse puts in on Windows?

Java multiline string - Stack Overflow

java string multiline
Rectangle 27 2

a) how exactly does the presence of the backslash characters make it a "multi-line comment", and

A backslash as the last character on a line means that the compiler should disregard the backslash and the newline character - it tells the compiler to do this before it should check for comments. So it says that before removing comments it should effectively look at

//**********|ANSWER|************\//blah blah blah, answering the
//questions, etc etc

it now sees the // at the start and ignores the rest of the line

b) why would a multi-line comment be a problem anyway?

In your example it isn't since the second line is a comment anyway, but what if you had written something useful on the second line?

Well since you asked question "a" it's likely that you didn't realize that the compiler behaved this way, and if you don't realize that you've commented out a line of code, then it's quite nice of the compiler to warn you.

Another reason is that even if had known this is that normally an editor will not visibly show whitespace and it's therefore easy to miss that the backslash may or may not be the last character on the line. For example:

int i = 42;

// backslash+space: \ 
i++
// backslash and no space: \
i--
printf("%d\n", i);

Would result in 43 since the i-- is commented out, but i++ isn't (because the backslash is not the last character on the line, but a space is).

gcc - What is the meaning of multi-line comment warnings in C? - Stack...

c gcc comments warnings
Rectangle 27 2

a) how exactly does the presence of the backslash characters make it a "multi-line comment", and

A backslash as the last character on a line means that the compiler should disregard the backslash and the newline character - it tells the compiler to do this before it should check for comments. So it says that before removing comments it should effectively look at

//**********|ANSWER|************\//blah blah blah, answering the
//questions, etc etc

it now sees the // at the start and ignores the rest of the line

b) why would a multi-line comment be a problem anyway?

In your example it isn't since the second line is a comment anyway, but what if you had written something useful on the second line?

Well since you asked question "a" it's likely that you didn't realize that the compiler behaved this way, and if you don't realize that you've commented out a line of code, then it's quite nice of the compiler to warn you.

Another reason is that even if had known this is that normally an editor will not visibly show whitespace and it's therefore easy to miss that the backslash may or may not be the last character on the line. For example:

int i = 42;

// backslash+space: \ 
i++
// backslash and no space: \
i--
printf("%d\n", i);

Would result in 43 since the i-- is commented out, but i++ isn't (because the backslash is not the last character on the line, but a space is).

gcc - What is the meaning of multi-line comment warnings in C? - Stack...

c gcc comments warnings
Rectangle 27 10

#how to trim a multi line string or a file

s=""" line one
\tline two\t
line three """

#line1 starts with a space, #2 starts and ends with a tab, #3 ends with a space.

s1=s.splitlines()
print s1
[' line one', '\tline two\t', 'line three ']

print [i.strip() for i in s1]
['line one', 'line two', 'line three']




#more details:

#we could also have used a forloop from the begining:
for line in s.splitlines():
    line=line.strip()
    process(line)

#we could also be reading a file line by line.. e.g. my_file=open(filename), or with open(filename) as myfile:
for line in my_file:
    line=line.strip()
    process(line)

#moot point: note splitlines() removed the newline characters, we can keep them by passing True:
#although split() will then remove them anyway..
s2=s.splitlines(True)
print s2
[' line one\n', '\tline two\t\n', 'line three ']

string - How do I trim whitespace with Python? - Stack Overflow

python string whitespace trim strip
Rectangle 27 10

#how to trim a multi line string or a file

s=""" line one
\tline two\t
line three """

#line1 starts with a space, #2 starts and ends with a tab, #3 ends with a space.

s1=s.splitlines()
print s1
[' line one', '\tline two\t', 'line three ']

print [i.strip() for i in s1]
['line one', 'line two', 'line three']




#more details:

#we could also have used a forloop from the begining:
for line in s.splitlines():
    line=line.strip()
    process(line)

#we could also be reading a file line by line.. e.g. my_file=open(filename), or with open(filename) as myfile:
for line in my_file:
    line=line.strip()
    process(line)

#moot point: note splitlines() removed the newline characters, we can keep them by passing True:
#although split() will then remove them anyway..
s2=s.splitlines(True)
print s2
[' line one\n', '\tline two\t\n', 'line three ']

string - How do I trim whitespace with Python? - Stack Overflow

python string whitespace trim strip
Rectangle 27 10

#how to trim a multi line string or a file

s=""" line one
\tline two\t
line three """

#line1 starts with a space, #2 starts and ends with a tab, #3 ends with a space.

s1=s.splitlines()
print s1
[' line one', '\tline two\t', 'line three ']

print [i.strip() for i in s1]
['line one', 'line two', 'line three']




#more details:

#we could also have used a forloop from the begining:
for line in s.splitlines():
    line=line.strip()
    process(line)

#we could also be reading a file line by line.. e.g. my_file=open(filename), or with open(filename) as myfile:
for line in my_file:
    line=line.strip()
    process(line)

#moot point: note splitlines() removed the newline characters, we can keep them by passing True:
#although split() will then remove them anyway..
s2=s.splitlines(True)
print s2
[' line one\n', '\tline two\t\n', 'line three ']

string - How do I trim whitespace with Python? - Stack Overflow

python string whitespace trim strip
Rectangle 27 3

The issue is that you are including line anchors ^ and $ in your regex. The string you're trying to match is clearly a multi-line string, but Tcl's default matching treats newlines just like a regular character. Another complicating factor is that Tcl considers a newline to be \n but expect is using \r\n as the line terminator.

If you enable newline sensitive matching in the regular expression and take the carriage return into account, you may find this regular expression works: (?n)^this\sis\sa\sline((?:\s\d)+)\r?$

$ expect <<END
spawn sh -c {echo foo; echo "this is a line 2 4 5 6"; echo bar}
exp_internal 1
expect -re {(?n)^this\sis\sa\sline((?:\s\d)+)\r?$}
END
spawn sh -c echo foo; echo "this is a line 2 4 5 6"; echo bar
Gate keeper glob pattern for '(?n)^this\sis\sa\sline((?:\s\d)+)\r?$' is 'this?is?a?line*'. Activating booster.

expect: does "" (spawn_id exp4) match regular expression "(?n)^this\sis\sa\sline((?:\s\d)+)\r?$"? Gate "this?is?a?line*"? gate=no
foo

expect: does "foo\r\n" (spawn_id exp4) match regular expression "(?n)^this\sis\sa\sline((?:\s\d)+)\r?$"? Gate "this?is?a?line*"? gate=no
this is a line 2 4 5 6
bar

expect: does "foo\r\nthis is a line 2 4 5 6\r\nbar\r\n" (spawn_id exp4) match regular expression "(?n)^this\sis\sa\sline((?:\s\d)+)\r?$"? Gate "this?is?a?line*"? gate=yes re=yes
expect: set expect_out(0,string) "this is a line 2 4 5 6\r"
expect: set expect_out(1,string) " 2 4 5 6"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "foo\r\nthis is a line 2 4 5 6\r"
fconfigure

That worked perfectly for me. Thanks! What's a "Gate" in Expect?

I don't know about expect's "gate" -- it looks like it's trying to convert the regex into a glob pattern, but in this case that's not helpful at all, since you need the regex capturing parentheses.

And what's the (?n) at the beginning?

Follow the link I provided in the answer.

Accessing my capture group in regex pattern matching in Expect to a va...

regex pattern-matching expect capture-group
Rectangle 27 1

cmd /c can't take a multi-line string as an argument. If you want to execute multiple statements, you need to separate them with the command separator character &. However, what you're doing here is essentially trying to pass it a batch file in a string argument.

Place the contents of your here-string in a .bat file, then run cmd /c <path to batch file>. You don't even need Invoke-Expression. You can run cmd directly as a command from a PowerShell prompt.

thanks for chiming in, but in my first sentence i wrote I want to send a FTP request ... without pointing it to a saved script. I ended up doing what you suggested just to have a quick-n-dirty solution, but i am still looking for a way to do it without having to point to a file from within the POSH script.

Okay, I see that, but what I'm wondering is why do you want to invoke cmd from PowerShell in order to create a text file? Why not just run "user`npass`npwd`ncd DIR`nbinary`nget spec`nbye" > test.ftp at the PowerShell prompt? In fact, is there any reason you want to use cmd at all? ftp isn't a native cmd command, it's a standalone .exe bundled with Windows (in the system32 directory) and can be invoked directly from PowerShell. Or you might find (echo user pass pwd 'cd DIR' binary 'get spec' bye) -join "`n" more readable.

shell - save FTP request to string --> Invoke-Expression to DOS - Stac...

shell powershell ftp cmd