I want to prepend the data of a file (random.txt) into another set of files (real.txt) in a folder.  Take the following example:  $ cat random some text  $ cat real.txt sample text   If I run the following command:  $ sed i.old '1s;^;some text\n;' real.txt  $ cat real.txt some text  sample text   This command prepends the text of file random into file real.txt, but the content of random is not fixed. Is it possible to redirect the content of the random into real.txt?  I tried the following command:  $ sed -i.old "1s;^;$(cat random)\n;" real.txt  $ cat real.txt   Which gives the output:  some text sample text   The problem occurs when i my random fil gets a txt starting with "/*". It shows following error:   $ cat real.txt /*new text  new line*/ $sed -i.old "1s;^;$(cat random)\n;" real.txt  sed: -e expression #1, char 16: unterminated `s' command
76092
I need to add PHP tags surrounding a file. It's easy to append them using  find . -exec echo "?&gt;" &gt;&gt; '{}' \;   but how can I prepend the tag &lt;?php?
74253
I have a bunch of .text files, most of which end with the standard nl.  A couple don't have any terminator at end.  The last physical byte is (generally) an alphameric character.  I was using cat *.text &gt;| /tmp/joined.text, but then noticed a couple of places in joined.text where the first line of a file appeared at the end of the last line of a previous file.  Inspecting the previous file, I saw there wasn't a line terminator -- concatenation explained.  That raised the question, what's the easiest way to concatenate, sticking in the missing newline? What about these options?   A solution that might effectively add a blank line to some input files.  For me, that's not a problem as the processing of joined.text can handle it. A solution that adds the cr/fl only to files that do not already end that way.
110915
&gt; can do it.  echo "text" &gt; file   tee can do it.  echo "test" | tee file    Can sed do it without using either of the above? Is it possible to save the output of a sed command to a file without using either &gt; or tee?
44727
I have a file named my_file.txt whose content is just the string Hello. How could I redirect its content to the command echo?  I know I have the commands less, cat, more... but I need to do it with echo.  I tried this:   $ cat my_file.txt | echo   and also this:  $ echo &lt; my_file.txt   But in both cases it appears only a blank in the stdout, not the content of my_file.txt.  How could I do that?
83894
I have a file containing a a large number of lines, each of which contains a bunch of numbers which are separated by spaces. I process this data in a pipe in some way, and then I want to collapse the multiple lines into a single line of all the numbers separated by spaces.  Is there a standard command-line utility I can use to do this? It seems like most line-by-line utilities won't mess with the newlines...
116673
For example, I have files named   A_1.txt A_t_1.txt A_ts_1.txt A_tsa_1.txt   in a directory ~/admin/packages/.  I am building a for loop to by globbing to A_1.txt   for f in ~/admin/packages/*_1.txt   It's obvious that this can potentially glob to any one of these files.  Specifically I would like to use an "exception" command (if it exists) to say glob to any file as long as it does not have a t, s, a before _1.txt.  EDIT: I forgot to mention that I have the path stored as a variable.  PROJECT="~/admin/packages/*_1.txt"  for f in ${PROJECT}...
104860
I am trying to delete two lines ( 7 and 8) from a .txt file. For this I am using following code:-  #!/bin/sh Column="7" sed '"$Column",8d' myfile.txt &gt; result.txt   on running this script, I am getting this error:-  sed: -e expression #1, char 1: unknown command: `"'   Please tell me how to use variable as a part of sed command.
44386
I have a directory filled with various files, differently named (i.e. not named with a specific pattern) and with different extensions, and I want to put each file in a newly created subdirectory named after each file; what's the best way to accomplish that from the command line under Linux? This sounds a bit confusing, let me make and example: let's call a random file in the directory ./filename.ext, I'd like to put it in a subdirectory called ./filename so that effectively the new path will be ./filename/filename.ext.  I did some trials with the following steps:   I've created some test files, from file-1.txt to file-9.txt, inside a test directory with for num in {1..9}; do touch file-$num.txt; done  I've created a directory named after each filename with for name in $(eval ls | cut -b 1-6); do mkdir $name; done I've moved the source files in their respective directories with for name in $(eval ls | grep -e ".txt" | cut -b 1-6); do mv $name.txt $name/$name.txt; done   As you can see, this solution works, but its huge drawback is that it works just in this case, with files of that extact filename lenght and with that particular extension. If I have some files with different names and extensions, this would be of no use at all. The use of cut command is not the ideal solution here, I suppose, but I haven't found anything better at the moment.  As a bonus, it would be cool to find a way to create the subdirs automatically when moving/renaming the files, without having to use mkdir previously (so when I ls to actually rename, I don't have to worry about excluding the newly created directories); but in the end this is a secondary problem.
91376
Is this possible? I read somewhere that the following command would do it:     sed -e [command] [file]   but it appeared to do the same thing as just     sed [command] [file]   (it did not save the changes). Is there any way to do this using sed?
5286
I have a script that reads a text stream and generates a file of sed commands that is later run with sed -f. The generated sed commands are like:  s/cid:image002\.gif@01CC3D46\.926E77E0/https:\/\/mysite.com\/files\/1922/g s/cid:image003\.gif@01CC3D46\.926E77E0/https:\/\/mysite.com\/files\/1923/g s/cid:image004\.jpg@01CC3D46\.926E77E0/https:\/\/mysite.com\/files\/1924/g   Assume the script which generates the sed commands is something like:  while read cid fileid do     cidpat="$(echo $cid | sed -e s/\\./\\\\./g)"     echo 's/'"$cidpat"'/https:\/\/mysite.com\/files\/'"$fileid"'/g' &gt;&gt; sedscr done   How can I improve the script to ensure all regex metacharacters in the cid string are escaped and interpolated properly?
119949
I was trying to remove all the lines of a file except the last line but the following command did not work, although file.txt is not empty.  $cat file.txt |tail -1 &gt; file.txt  $cat file.txt   Why is it so?
85371
I would like to create a simple program (in Java) which edits text files - particularly one which performs inserting arbitrary pieces of text at random positions in a text file. This feature is part of a larger program I am currently writing.  Reading the description about java.util.RandomAccessFile, it appears that any write operations performed in the middle of a file would actually overwrite the exiting content.   This is a side-effect which I would like to avoid (if possible).  Is there a simple way to achieve this?  Thanks in advance.
69617
I have created two files:  sample.txt and sample.txt  (the second file contains some hidden characters, e.g. a space). How can I remove the latest modified file? I'm using Linux.
48525
Am I blind or is there no option like --in-place for sort?  In order to save results to the input file, sed uses -i (--in-place).  Redirecting the output of sort to the input file  sort &lt; f &gt; f   results in making it empty. If there is no --in-place option - maybe there is some trick how to do this in handy way?  (The only thing that cames to my mind:   sort &lt; f &gt; /tmp/f$$ ; cat /tmp/f$$ &gt; f ; rm /tmp/f$$   Moving is not right choice, cause file permissions might be changed. That's why I overwrite with the contents of the temp file which I then remove.)
60802
I want to use cat with wildcards in bash in order to print multiple small files (every file is one sentence) to standard output. However, the separate file contents are not separated by a newline, which I'd like to for ease of reading.  How can I add a file delimiter of some sort to this command?
68162
doesn't seem to work  The next example would delete everything between "ONE" and "TWO:"   #!/bin/sh sed ' /ONE/ { # append a line     N # search for TWO on the second line      /\n.*TWO/ { # found it - now edit making one line         s/ONE.*\n.*TWO/ONE TWO/     } }' file   i was expecting to delete everything between ONE and TWO.i tried with file where file contains.  [user5@mailserver ~]$ cat file ONE hello TWO   but the output come like   [user5@mailserver ~]$ ./test ONE hello TWO   I was expecting ONE TWO I am trying to make it correct but not able to do.
84459
I was writing a bash script, and happened to updated the code (saved the script file to disk) while the script was waiting for some input in a while loop. After I returned to the terminal and continued with the previous invocation of the script, bash gave an error about the file syntax:  /home/aularon/bin/script: line 58: unexpected EOF while looking for matching `"' /home/aularon/bin/script: line 67: syntax error: unexpected end of file   So I tried doing the following:  1st: create a script, self-update.sh let's call it:  #!/bin/bash fname=$(mktemp) cat $0 | sed 's/BEFORE\./AFTER!./' &gt; $fname cp $fname $0 rm -f $fname echo 'String: BEFORE.';   What the script does is reading its code, changing the word 'BEFORE' into 'AFTER', then re-write itself with the new code.  2nd Run it:  chmod +x self-update.sh ./self-update.sh   3rd wonder...  aularon@aularon-laptop:~$ ./self-update.sh  String: AFTER!.   Now, I wouldn't have guessed that on the same invocation it would output AFTER!, on the second run for sure, but not on the first one.  So my question is: is it intentional (by design)? or it is because of the way bash runs the script? Line by line or command by command. Is there any good use of such behaviour? Any example of it?    Edit: I tried reformatting the file to put all command in one line, it does not work now:  #!/bin/bash fname=$(mktemp);cat $0 | sed 's/BEFORE\./AFTER!./' &gt; $fname;cp $fname $0;rm -f $fname;echo 'String: BEFORE.';   Output:   aularon@aularon-laptop:~$ ./self-update.sh #First invocation String: BEFORE. aularon@aularon-laptop:~$ ./self-update.sh #Second invocation String: AFTER!.   While when moving the echo string to the next line, separating it from the rewriting (cp) call:  #!/bin/bash fname=$(mktemp);cat $0 | sed 's/BEFORE\./AFTER!./' &gt; $fname;cp $fname $0;rm -f $fname; echo 'String: BEFORE.';   And now it works again:  aularon@aularon-laptop:~$ ./self-update.sh  String: AFTER!.