In very lot of answers, mainly about text-processing commands, I saw commands such as sed, awk, grep, among other, being used with STDIN and the simple open of a file  e.g.  $ sed -e 's|foo|bar|g' file # open file $ sed -e 's|foo|bar|g' &lt;file # open STDIN   or  $ grep 'PATTERN' file # open file $ grep 'PATTERN' &lt;file # open STDIN   In a personal, I use the open file method always, but I want to know when and when not to use them, also what's the difference.
77702
I was working through a tutorial and saw use of both cat myfile.txt and cat &lt; myfile.txt. Is there a difference between these two sequences of commands? It seems both print the contents of a file to the shell.
44404
While we use * to denote zero or more previous characters in grep, we use *.c to find all C files when we use it with the ls command like ls *.c. Could someone tell how the use of * differs in these two cases?
46078
On occasion process substitution will not work as expected. Here is an example:  Input:  gcc &lt;(echo 'int main(){return 0;}')   Output:  /dev/fd/63: file not recognized: Illegal seek collect2: error: ld returned 1 exit status   Input:  But it works as expected when used with a different command:  grep main &lt;(echo 'int main(){return 0;}')   Output:  int main(){return 0;}   I have noticed similar failures with other commands (i.e. the command expecting the file from the process substitution can't use /dev/fd/63 or similar). This failure with gcc is just the most recent.  Is there some general rule that I should be aware of to determine when process substitution will fail in this way and should not be used?  I am using this BASH version on Ubuntu 12.04 (I've also seen this in arch and debian): GNU bash, version 4.3.11(1)-release (i686-pc-linux-gnu)
65627
It looks like 'find', 'bash' and 'sed' in some cases does not work as one expects.  The following example should first create file 'sample.txt', then find the file and finally process it by '-exec' command. The executed command prints found filename, test specimens, and modified filename. The 'sed' command itself is used to replace 'txt' to 'TXT'.  touch sample.txt find ./ -maxdepth 1 -name "*.txt" -exec echo {} $(echo Specimen_before.txt {} Specimen_after.txt |sed -e "s/txt/TXT/g") \;   The expected output is:     ./sample.txt Specimen_before.TXT ./sample.TXT Specimen_after.TXT   Instead it produces:     ./sample.txt Specimen_before.TXT ./sample.txt Specimen_after.TXT   (the example has been tested also with old-school command substitution through backquotes '`' with the same result)  What am I doing wrong?
37773
I wrote a bash script that processes a list of filenames (using glob expansion, as in for f in *) and then outputs a subset of this list to a file. Subsequently I read the content of this file into an array and perform a plain binary search for specific filenames using the obvious &lt; and &gt; operators for comparing strings.  Considering that I would like the script to work on as many different environments like Linux, MacOS, MinGW, ... (even though it uses things like [[ and stat which are less portable), my questions are:   Do I need to sort the file content (either with sort or additional bash code) or is the glob expansion always sorted - in every environment? Do the conditional operators use the same "sorting" as the expansion (or after sort)?  Would expansion or sort return file10.txt after file2.txt (in what cases?) but using conditional operators file10.txt would be before file2.txt ? What sort option would I  use to fix this? Are there any caveats if some of my filenames are in Unicode?   Are there any issues using specific versions of bash?   Does LC_COLLATE affect any of the above?     I obviously need the file content to match the sorting "method" of the operators in order for the binary search to work as expected...
5499
I often want to search through a man page, however I am forced to either do:  man &lt;package&gt; | grep &lt;search&gt;   Or:  man &lt;package&gt; &gt; file.txt   But there must be an easier way than this to do this, so how can I search through a man page while in it instead of having to send its contents to a file, or use grep to print the necessary matching contents to standard output?
33296
I heard about "useless use of cat" and found some suggestions, but the following outputs nothing in my bash shell.  &lt; filename   Using cat works as expected though.  cat filename   I'm using Fedora Core 18 and GNU bash, version 4.2.45(1).  EDIT: Using it in front of a pipe doesn't work either.  &lt; filename | grep pattern   Whereas using cat works as expected.  cat filename | grep pattern   EDIT2: To clarify, I know that I can use this  grep pattern &lt; filename   but I read here  that I can also use it in front of the command. It does not work in front of the command though.
29805
The purpose of the command should be replacing any IP in file myfile.txt with the local ip of the server automatically  example:  sed -i -e 's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/LOCALIP/g' myfile.txt   i want to know if it possible to use command for getting local ip address inside sed   basically i want to replace LOCALIP in my example with this command   /sbin/ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1
48567
I've been trying to use bash to read a file character by character.  After much trial and error, I have discovered that this works:  exec 4&lt;file.txt  declare -i n while read -r ch &lt;&amp;4;       n=0      while [ ! $n -eq ${#ch} ]            do  echo -n "${ch:$n:1}"                (( n++ ))           done      echo ""       done   I.e., I can read it line by line and then loop through each line char by char.  Before doing this, I had tried: exec 4&lt;file.txt &amp;&amp;  while read -r -n1 ch &lt;&amp;4; do; echo -n "$ch";  done but it would skip all whitespaces in the file.  Could you please explain why? Is there a way to make the second strategy (i.e. reading char by char with bash's read) work?
116749
I have been told it was possible, but without a single working example, that sed can read from a string variable without need for an input file. I have yet to get it to work.  For general safety, I’m writing the $PATH variable to another variable, as I mess with this, because I don’t need other problems to arise until I know exactly how to do this.  Consider the following:  ~$x=$PATH ~$sed -i 's/:/ /g' $x   this fails with: No such file or directory.  Here are some others I have tried:  ~$ sed -i 's/:/ /g' | (read x)  sed: no input files ~$ sed -i 's/:/ /g' | (read $x)  bash: read: `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games': not a valid identifier sed: no input files ~$ sed -i 's/:/ /g' | $(read x)  sed: no input files ~$ sed -i 's/:/ /g' | $(read $x)  bash: read: `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games': not a valid identifier sed: no input files ~$ sed -i 's/:/ /g' &lt; $(read $x)  bash: read: `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games': not a valid identifier bash: $(read $x): ambiguous redirect ~$ sed -i 's/:/ /g' &lt; read $x  bash: read: No such file or directory ~$ sed -i 's/:/ /g' &lt; (read x)  bash: syntax error near unexpected token `(' ~$ sed -i 's/:/ /g' &lt; $((read x))  bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: syntax error: operand expected (error token is "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games") ~$ sed -i 's/:/ /g' &lt; $((read $x))  bash: read /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: division by 0 (error token is "usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games") ~$ sed -i 's/:/ /g' &lt;&lt; $((read $x))  &gt; ;^C ~$ sed -i 's/:/ /g' &lt;&lt; $(read $x) &gt; ;^C ~$ sed -i 's/:/ /g' $x sed: can't read /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: No such file or directory ~$ sed -i 's/:/ /g' &lt; $x bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: No such file or directory ~$ sed -i 's/:/ /g' &lt; echo $x bash: echo: No such file or directory ~$ sed -i 's/:/ /g' |  echo $x /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games sed: no input files   Can this even work? I would prefer not to need to write files I don't need just so I can use sed. For this particular example, if ~$x=$PATH ~$sed -i 's/:/ /g' $x actually worked the way I would have hoped, I would get:   /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /usr/games /usr/local/games   which I could then assign to a variable, and use in future commands, like ls $x
71762
From this :  $ diff &lt;(wget -q -O - URL1) &lt;(wget -q -O - URL2)   Seems like multiple standard inputs but---if I remember my Linux Porgramming course correctly---this can't be it. I thought that, by definition, standard input was one stream.  Perhaps this has zilch to do with stdin? Can someone explain this to me; maybe provide links to documentation.  Bonus: How would a script handle these multiple streams? If someone could provide a Python or Perl example, that would be very helpful.
87030
I am doing grep and sed and I get 2 lines of a file that I am interested in. How can I get these lines in a single line ending with the new line character? Now I am getting:    pattern1   pattern2     I would like to get pattern1 pattern2 \n
46153
I want to find all lines in several files that match one of two patterns. I tried to find the patterns I'm looking for by typing   grep (foo|bar) *.txt   but the shell interprets the | as a pipe and complains when bar isn't an executable.  How can I grep for multiple patterns in the same set of files?
105512
I can't find any documentation about the sed -e switch, for simple replace, do I need it?  e.g.  sed 's/foo/bar/'   VS  sed -e 's/foo/bar/'
120458
I have recently discovered that if we press Ctrl+X Ctrl+E, bash opens the current command in an editor (set in $VISUAL or $EDITOR) and executes it when the editor is closed. But it doesen't seem to be documented in the man pages.
45267
I would like to use the same pipe for different applications, like in:  cat my_file | {   cmd1   cmd2   cmd3 }   Cmd1 should consume part of the input. Cmd2 should consume another part and so on.  However, each cmd eats more of the input then it read really needs due buffering.  For example:  yes | nl | {    head -n 10 &gt; /dev/null   cat  } | head -n 10   Outputs from line 912 instead of line 11.  Tee is not a good option, because each command is supposed to consume part of the stdin.   Is there a simple way to get this working?
61269
Is there a way to modify a file without writing the contents to another file, without sed and awk?  For example:  $ cat test.txt aaa aaa bbb ccc ddd   Replacing using sed with -i option, sed -i 's/aaa/NNN/g' test.txt will produce the following:  NNN NNN bbb ccc ddd   How to do that without awk and sed?