I have two files: a.patterns contains patterns in a one per line way  "`" "^" "&lt;" "&lt;(" "&lt;)" "&lt;[" "=" "&gt;" "_;|^+*+?@"   and b.data contains comma separated values.  1,1,0,"*",1 2,1,1,"*",1 3,0,0,"&lt;)",0   The purpose is to substitute take each pattern matched in the csv file with that pattern's index (i.e., its line number).   The problem is that I load each pattern into a variable, and use sed to evaluate/replace it but frequently the patterns contain special characters.   ... while read line; do   counter=`expr $counter + 1`   temp=`echo "$temp" | sed "s/$line/$counter/"` done &lt; a.patterns ...   My question: is there a way to tell sed to count the contents of the variable as literals? Ideally I would like to avoid inserting escape character for all special characters in the pattern file or alternative ways to achieve the same?  Thank you.
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?
40781
While appending some text after a matched pattern, I wanted to add an empty line with a comment to isolate my additions from the rest. However, I have noticed that for the very first \n, I need to escape the \ with an additional \ (makes it \\n) but this is apparently not required for rest of the appended text. Why is this so?  An example that I produced on a SUSE 11 server (bash v3.2.57(2) and GNU sed version 4.1.5) is:  edwdev@hdpedge1:/edw&gt; sed '/test/a \\nNot a test\nJust Playing' &lt;&lt;&lt; test test  Not a test Just Playing   If I do not escape the first \n (right after the a sed command, I get an n printed instead of a newline.  edwdev@hdpedge1:/edw&gt; sed '/test/a \nNot a test\nJust Playing' &lt;&lt;&lt; test test nNot a test Just Playing   Notice that I have intentionally added a space after a command so that it doesn't conflict accidentally with alternate syntax.  Same behavior is observed if I replace \n with \t.Number of spaces after a command do not seem to matter; the first backslash needs to be escaped to print the special character even after multiple spaces.    Same is the case on another environment, where I originally observed this; a Centos 7 x64 VM, bash version is 4.2.46(2) and sed version is GNU sed 4.2.2  sed -i '/\[mysqld\]/a \\n# &lt;&lt;INSERTED MANUALLY&gt;&gt;\ntransaction-isolation=READ-COMMITTED\ninnodb_flush_method=O_DIRECT\nmax_connections=550\n# &lt;&lt;END OF MANUAL INSERTIONS &gt;&gt;\n' /etc/my.cnf   Output:  ... [mysqld]  # &lt;&lt;INSERTED MANUALLY&gt;&gt; transaction-isolation=READ-COMMITTED innodb_flush_method=O_DIRECT max_connections=550 # &lt;&lt;END OF MANUAL INSERTIONS &gt;&gt;  ...   Output Without Escaping first \n:  [mysqld] n# &lt;&lt;INSERTED MANUALLY&gt;&gt; transaction-isolation=READ-COMMITTED innodb_flush_method=O_DIRECT max_connections=550 # &lt;&lt;END OF MANUAL INSERTIONS &gt;&gt;   Is there any particular reason for this behavior? I am unable to find any details while skimming through the docs (), through, admittedly, I haven't read the entire documentation thoroughly.
34120
I am looking for a way to replace a string in a file with a string that contains a slash by using sed.  connect="192.168.100.61/foo" srcText="foo.bar=XPLACEHOLDERX" echo $srcText | sed "s/XPLACEHOLDERX/$connect"   The result is:  sed: -e Expression #1, Character 32: Unknown option for `s'
34780
I want to know which files have the string $Id$.  grep \$Id\$  my_dir/mylist_of_files   returns 0 occurrences.  I discovered that I have to use  grep \$Id$ my_dir/mylist_of_files  Then I see that the $Id is colored in the output, i.e. it has been matched.  How could I match the second $ and why doesn't \$Id\$ work.  It doesn't matter if the second $ is the last character or not.  I use grep 2.9.    Before posting my question, I used google...       To search for a $ (dollar sign) in the file named test2, enter:      grep \\$ test2      The \\ (double backslash) characters are necessary in order   to force the shell to pass a \$ (single backslash, dollar sign) to the   grep command. The \ (single backslash) character tells the grep   command to treat the following character (in this example the $) as a   literal character rather than an expression character. Use the fgrep   command to avoid the necessity of using escape characters such as the   backslash.   but I don't understand why grep \$Id works and why grep \\$Id\\$ doesn't.  I'm a little bit confused...
44642
Is there an issue with sed and new line character? I have a file test.txt with the following contents    aaaaa   bbbbb   ccccc   ddddd     The following does not work: sed -r -i 's/\n/,/g' test.txt    I know that I can use tr for this but my question is why it seems not possible with sed.     If this is a side effect of processing the file line by line I would be interested in why this happens. I think grep removes new lines. Does sed do the same?
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.
40934
I have a couple of big text files and in the file UNIQS.txt I have a list of strings to grep from another file. The code I use is  grep -f UNIQS.txt EEP_VSL.uniqs.sam &gt; UNIQ_templates.sam   which does nothing - the file generated is empty. But when I do  grep -F -f UNIQS.txt EEP_VSL.uniqs.sam &gt; UNIQ_templates.sam   it works correctly. This confuses me because I didn't think grep would interpret the entries in UNIQS.txt as regexp patterns without quotes and slashes and so on being in the file (which there aren't). Is it the case in general that if you are getting the patterns from a file then it will automatically think that they are regexp patterns?  Edit: In the UNIQS.txt file, there are newline separated strings of the form  HWI-ST365:215:D0GH0ACXX:2:1101:10034:186783   (called template names) and the file EEP_VSL... tab separated columns, with about 14 columns and the first column is the template name, so basically I want to extract the line corresponding to each template in the file.
53214
I am attempting to write a filter using something like sed or awk to do the following:   If a given pattern does not exist in the input, copy the entire input to the output If the pattern exists in the input, copy only the lines after the first occurrence to the output   This happens to be for a "git clean" filter, but that's probably not important. The important aspect is this needs to be implemented as a filter, because the input is provided on stdin.  I know how to use sed to delete lines up to a pattern, eg. 1,/pattern/d but that deletes the entire input if /pattern/ is not matched anywhere.  I can imagine writing a whole shell script that creates a temporary file, does a grep -q or something, and then decides how to process the input. I'd prefer to do this without messing around creating a temporary file, if possible. This needs to be efficient because git might call it frequently.
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?
22924
How can I search and replace text that contains / with sed?  I currently use the following command which doesn't work  sed -i "s/queue_directory = /var/spool/postfix-secondary/queue_directory = /var/spool/postfix-$newnumber/g" /etc/postfix-$newnumber/main.cf
10427
I'm using sed to find and replace patterns within SAS files I have rather than changing them individually. The problem is I am trying to replace macro variables and when I use the ampersand it is not processing correctly.  Here's my code: sed -ie 's/user=&amp;uid./user=&amp;sysuserid./g' *_table_*.sas   whenever I run this command it seems to append and do all kinds of funky stuff with the original text.  Question: How do I replace text that contains ampersands with sed command?
3017
I wrote a regular expression which works well in a certain program (grep, sed, awk, perl, python, ruby, ksh, bash, zsh, find, emacs, vi, vim, gedit, …). But when I use it in a different program (or on a different unix variant), it stops matching. Why?
7069
Using a common command line tool like sed or awk, is it possible to join all lines that end with a given character, like a backslash?  For example, given the file:  foo bar \ bash \ baz dude \ happy   I would like to get this output:  foo bar bash baz dude happy
51359
IT seems that 'sed' and I will never be friends...  How can I find and replace a pattern within a range (within another pattern)?  In words: Find: AllowOverride None within the following range and replace it with AllowOverride All  ... &lt;Directory "/var/www/html"&gt; some text more text some comments AllowOverride None more text &lt;/Directory&gt; ...   Please note (if not already done): This is related to httpd.conf which contains a lot more "modules" all ending with &lt;/Directory&gt;  As this command is used within a yaml file, it would be aesome to achieve this with a one-liner (I am aware, that one-liners are getting ugly pretty fast...).   Also: I rely on using sed (requirement, dont ask me why - pls :))
94024
I am using this sed command to extract a json value for a key, say MyKey..  sedPattern='s/^.*"myKey":"\([^"]*\)".*$/\1/'   For a json string like,  {"myKey" : 1, "yourKey": 2}   It returns what i need,  1   But what does \1 action in the above sed command mean?
55783
I am trying to run grep against a list of a few hundred files:  $ head -n 3 &lt;(cat files.txt) admin.php ajax/accept.php ajax/add_note.php   However, even though I am grepping for a string that I know is found in the files, the following does not search the files:  $ grep -i 'foo' &lt;(cat files.txt)  $ grep -i 'foo' admin.php The foo was found   I am familiar with the -f flag which will read the patterns from a file. But how to read the input files?  I had considered the horrible workaround of copying the files to a temporary directory as cp seems to support the &lt;(cat files.txt) format, and from there grepping the files. Shirley there is a better way.
121845
inputs:  line1 with the PATTERN that contains ( )  line2 with the PATTERN that contains ( )  lineN with the PATTERN that contains ( )    outputs:  line1 with the PATTERN that contains ( ) ; line2 with the PATTERN that contains ( ) ; ... lineN with the PATTERN that contains ( ) ;   I tried this:  find . -name "test.txt" -print | xargs sed -i "/PATTERN/ s/$)/); /g"   but it didn't work.