I create a simple batch to replace a specific string in a file and dump the output to a new file:  set oldstring=SERVER_IP set newstring=10.10.10.1  setlocal enabledelayedexpansion for /f "tokens=*" %%i in (test.txt) do (      set str=%%i      set str=!str:%oldstring%=%newstring%!      echo !str!&gt;&gt;newfile  )   If I run it on a file containing caracter 'a' a line feed, then a white space and a line feed I get the wrong output.  a[lf]  [lf]   OUPUT:  a SERVER_IP=10.10.10.1   Why do I get this the string I'm trying to replace in the output whenever I have a line full of white spaces?  EDIT  Expected output  a
94748
How you can read a file (text or binary) from a batch file? There is a way to read it in a binary mode or text mode?
49040
for i in $(xrandr); do echo  "$i" ; done for i in "$(xrandr)"; do echo "$i"; done for i in "$(xrandr)"; do echo $i; done   I understand why 1 differs from 2. But why does 3 give a different output from 2? Please explain the output too. How do quotes work on newlines?
32406
Technically, any odd number of backslashes, as described in .  &gt;&gt;&gt; r'\'   File "&lt;stdin&gt;", line 1     r'\'        ^ SyntaxError: EOL while scanning string literal &gt;&gt;&gt; r'\\' '\\\\' &gt;&gt;&gt; r'\\\'   File "&lt;stdin&gt;", line 1     r'\\\'          ^ SyntaxError: EOL while scanning string literal   It seems like the parser could just treat backslashes in raw strings as regular characters (isn't that what raw strings are all about?), but I'm probably missing something obvious.
3334
I am writing a batch file script using Windows command-line environment and want to change each occurrence of some text in a file (ex. "FOO") with another (ex. "BAR"). What is the simplest way to do that? Any built in functions?
2158
This works as expected:  $ echo a b c | xargs --replace="{}" echo x "{}" y x a b c y   This does too:  $ echo a b c | xargs --max-args=1 echo x x a x b x c   But this doesn't work as expected:  $ echo a b c | xargs --max-args=1 --replace="{}" echo x "{}" y x a b c y   And neither does this:  $ echo a b c | xargs --delimiter=' ' --max-args=1 --replace="{}" echo x "{}" y x a y x b y x c  y   I expected this output:  x a y x b y x c y   As a workaround, I am using printf and two xargs, but that is ugly:  $ echo a b c | xargs printf '%s\0' | \ &gt; xargs --null --max-args=1 --replace="{}" echo x "{}" y x a y x b y x c y   Any idea why this is happening?
11519
I want to assign the result of an expression (i.e., the output from a command) to a variable and then manipulate it – for example, concatenate it with a string, then echo it.  Here's what I've got: #!/bin/bash cd ~/Desktop; thefile= ls -t -U | grep -m 1 &quot;Screen Shot&quot;; echo &quot;Most recent screenshot is: &quot;$thefile;  But that outputs: Screen Shot 2011-07-03 at 1.55.43 PM.png Most recent screenshot is:   So, it looks like that isn't getting assigned to $thefile, and is being printed as it's executed. What am I missing?
106661
I have an input file, say, file1, which appears as follows:  WRITE=2 START=1 SMEAR=0 MIN=6 MAX=100   My problem is to search for the string SMEAR and if the string exists replace the entire line with SMEAR=-5. However, if the SMEAR does not exist, then I need to insert a new line SMEAR=-5. Ultimately the line SMEAR=-5 should appear only once. I can replace the string using sed:   sed -i '/SMEAR/c\SMEAR=-5' file1   Or I can insert the string as a new line    sed -i '10i SMEAR=-5' file1   But how can I combine them based on the existence of the string in the file?  EDIT: Also it would be helpful if I can pass the value of SMEAR as a variable.
4470
As per the following example, and as in  In bash, where has the trailing newline char gone?, I want to know "why" it happens    x="$(echo -ne "a\nb\n")" ; echo -n "$x" | xxd -p  # Output is: 610a62  # The trailing newline from the 'echo' command #   has been "deleted" by Command Substitution   I assume there must be some very significant reason for a shell action, namely Command Substitution, to actually delete some data from the command output it is substituting... but I can't get my head around this one, as it seems to be the antithesis of what it is supposed to do.. ie. to pass the output of a command back into the script process... Holding back one character seems weird to me, but I suppose there is a sensible reason for it...  I'm keen to find out what that reason is...
66135
I need to take the multi-line output of a program, match a string, and return the previous line of all matches.  An example of the output of the program:  $ jack_lsp -p firewire_pcm:analog-1_out         properties: input,physical,terminal, firewire_pcm:analog-2_out         properties: input,physical,terminal, firewire_pcm:analog-1_in         properties: output,physical,terminal, firewire_pcm:analog-2_in         properties: output,physical,terminal, $   I need to match, for example, 'input', and return the previous line of all matches. So in the example, the expected output would be:  firewire_pcm:analog-1_out firewire_pcm:analog-2_out   Here is what I have, but it only returns the first match:  $ jack_lsp -p | grep -B1 input | head -1 firewire_pcm:analog-1_out $   What am I doing wrong?
413
The following code best describes the situation.  Why is the last line not outputting the trailing newline char?  Each line's output is shown in the comment.  I'm using GNU bash, version 4.1.5          echo -n $'a\nb\n'                  | xxd -p  # 610a620a              x=$'a\nb\n'   ; echo -n "$x" | xxd -p  # 610a620a      echo -ne "a\nb\n"                  | xxd -p  # 610a620a x="$(echo -ne "a\nb\n")" ; echo -n "$x" | xxd -p  # 610a62
114623
Up until recently I was under the impression that Unix-y programs were unaware of where their output went, be it to standard out, redirected to a file, or into a pipe. However, an unrelated U&amp;L question on this site brought to my attention the following example:  % echo "hello world" &gt; file  % grep --color=auto lo file hello world  % grep --color=auto lo file | cat hello world   Obviously you can't see it here, but the first command shows 'lo' highlighted, as expected. However, the second call, which goes through a pipe, shows no colour. This suggests that grep was aware of its output being directed to a pipe and avoided outputting colour. How is this done?
122931
I have a file named /tmp/urlFile where each line represents a url. I am trying to read from the file as follows:  cat "/tmp/urlFile" | while read url do     echo $url done   If the last line doesn't end with a newline character, that line won't be read. I was wondering why?   Is it possible to read all the lines, regardless if they are ended with a new line or not?
111082
I am working with batch files in Windows, using both Notepad, and Notepad++. When I run the batch files, which all start with @echo off, I see the first line (when running on two separate machines) reading ´╗┐@echo off, and then all the REM lines below it appear as well.  I have tried changing the encoding in Notepad++, but it claims they are already at UTF-8 encoding, which appears to be correct.  What do I need to do to get these files to run properly?
9051
Check out the below program.   try {     for (String data : Files.readAllLines(Paths.get("D:/sample.txt"))){         String[] de = data.split(";");         System.out.println("Length = " + de.length);     } } catch (IOException e) {     e.printStackTrace(); }   Sample.txt:   1;2;3;4 A;B;; a;b;c;   Output:   Length = 4 Length = 2 Length = 3   Why second and third output is giving 2 and 3 instead of 4. In sample.txt file, condition  for 2nd and 3rd line is should give newline(\n or enter) immediately after giving delimiter for the third field. Can anyone help me how to get length as 4 for 2nd and 3rd line without changing the condition of the sample.txt file and how to print the values of de[2] (throws  ArrayIndexOutOfBoundsException)?
76187
How can I test whether a command's output is an empty string while not   interfering with buffering?  Example:  command | ??? if [ $? = 0 ] &amp;&amp; [ -n "$output_from_command"]; then    ...   I want the output from the code above to be the same as when running command.
13420
What's the difference between \n and \r (I know it has something to do with OS), and what's the best way to echo a line break that will work cross platform?  EDIT: In response to Jarod, I'll be using ths to echo a line break in a .txt log file, though I'm sure I'll be using it in the future for things such as echoing HTML makup onto a page.
5744
I think I may be overlooking a relatively fundamental point regarding shell. Output from the ls command by default separates output with newlines, but the shell displays the output on a single line.  Can anyone explain this to me? I had always presumed that the output was simply separated by spaces, but now that I see the output separated by newlines, I would expect the output to be displaying on separate lines.  Example:  cpoweradm@debian:~/lpi103-4$ ls text* text1  text2  text3   od shows that the output is separated by newlines:  cpoweradm@debian:~/lpi103-4$ ls text* | od -c 0000000   t   e   x   t   1  \n   t   e   x   t   2  \n   t   e   x   t 0000020   3  \n 0000022   If newlines are present, then why doesn't the output display as:  text1  text2 text3