Tag Info

Hot answers tagged

10

sed expects a basic regular expression (BRE). \s is not a standard special construct in a BRE (nor in an ERE, for that matter), this is an extension of some languages, in particular Perl (which many others imitate). In sed, depending on the implementation, \s either stands for the literal string \s or for the literal character s. In your implementation, it ...


8

You don't need to pipe a file thru grep, grep takes filename(s) as command line args. grep -v '^#' file1 file2 file3 will print all lines EXCEPT those that begin with a # char. you can change the comment char to whatever you wish. If you have more than one comment char (assuming its at the beginning of a line) egrep -v '^(;|#|//)' filelist


7

Here's a sed script solution (easier on the eyes than trying to get it into one line on the command line): /<TEXT1>/ { r File1 d } Running it: $ sed -f script.sed File2 /home/user1/ /home/user2/bin /home/user1/a/b/c <TEXT2>


7

egrep can save you the use of cat. In other words, create less processes (egrep vs cat+egrep) and use less buffers (pipe from cat to egrep vs no pipe). It is generally a good idea to limit the use of cat if you simply want to pass a file to a command that can read it on its own. With this said, the following command will remove comments, even if they are ...


6

Yes, it's possible, \& can be used in replace expression to represent the entire match, similarly \#& can be used to represent the entire match as number. More concretely: M-x query-replace-regexp \b[0-9]+\b RETURN \,(+ 3 \#&) And a quote from the documentation You can use Lisp expressions to calculate parts of the replacement string. To ...


6

I am not fluent in sed, but it is easy to do so in awk: awk '/bar/{getline;next} 1' foo.txt The awk script reads: for a line containing bar, get the next line (getline), then skip all subsequent processing (next). The 1 pattern at the end prints the remaining lines. Update As pointed out in the comment, the above solution did not work with consecutive ...


6

If you have GNU sed (so non-embedded Linux or Cygwin): sed '/bar/,+1 d' If you have bar on two consecutive lines, this will delete the second line without analyzing it. For example, if you have a 3-line file bar/bar/foo, the foo line will stay.


5

Try the following key sequence: c e Unix Esc (note the space character before "Unix") Here's a brief explanation: c: "change" command, similar to delete but ends in "insert" mode. e: from the cursor to the end of the following word. " Unix": the replacement text Esc: Return to command mode (always return to command mode!)


5

I'm quite certain this can't be done directly. However, I came up with a function for you. Put this in your ~/.vimrc: function! Toggle() s!^\(\s*/\?[^/\s]/\?\)!xxx//\1!e s!^\(\s*\)//!\1!e s!^xxx//!//!e endfunc (This will change any xxx// you already have at the beginning of a line into //, but I'd imagine this is a rare occurrence). You could ...


5

You will want to make use of sed's scripting capabilities to accomplish this. $ sed -e '/bar/ { N d }' sample1.txt Sample data: $ cat sample1.txt foo bar biz baz buz The "N" command appends the next line of input into the pattern space. This combined with the line from the pattern match (/bar/) will be the lines that you wish to delete. You can ...


5

If bar may occur on consecutive lines, you could do: awk '/bar/{n=2}; n {n--; next}; 1' < infile > outfile which can be adapted to delete more than 2 lines by changing the 2 above with the number of lines to delete including the matching one. If not, it's easily done in sed with @MichaelRollins' solution or: sed '/bar/,/^/d' < infile > ...


4

For the first question I would do: :s/a/b && The second is trickier, I don't know a way to do it automatically but you can make vim prompt you on each match like this: :s/a/b/gc Then you reply "no" to the first n matches and "yes" to the others.


4

UPDATE: This awk script may be more what you are looking for: awk -vRS='\r\n ' -vORS= 1 contacts.vcf (original post) This perl script works, though it is actually longer, even when sed is spaced out a bit; and it is quite obviously logically very similar to sed. Perhaps perl reads the file into memory faster(?), as it doesn't have the is it, or is it ...


4

Sed can do a lot of things beside simple search-and-replace, and that includes multiline support. Here's a blog post on how someone did this (he wrote a sedml script for "sed multiline": http://austinmatzko.com/2008/04/26/sed-multi-line-search-and-replace/ The basic idea is to copy the whole file to sed's "hold buffer", run the regex on that and then write ...


4

As mentioned, use other separator or escape the slashes. Your last try misses escape of last slash. And as pointed out by @StephaneChazelas, escape dot's as well. And, including @terdon if sed is not needed; grep -Fxv, where -F is fixed string, not regex, would be an option. -x makes sure it matches whole lines. -v inverts. A simple (very simple) ...


3

Because sed matches in a greedy manner, there is always the possibility that text past the end of the comment will be matched instead of the preceding real end-of-comment marker, eg. as in a quoted string which contains "*/". This cannot be handled by sed in a simple manner, but you can work around it. Here is one such method: using single-character ...


3

I suppose your intention is to remove an old internationalization system from your PHP scripts. perl -e 'undef$/;$s=<>;$s=~s/<\?php\s+(?:print|echo)\s+t\((['"'"'"])(.*?)\1\);\s+\?>/$2/gs;print$s' apasajja This has some improvements not asked in the question: Works for both print or echo. Works for both single and double quotes. Allows the ...


3

If you only want to remove the new lines in the string, you don't need to use sed. You can use just $ echo "$string" | tr '\n' ' ' as others had pointed. But if you want to convert new lines into spaces on a file using sed, then you can use: $ sed -i ':a;N;$!ba;s/\n/\t/g' file_with_line_breaks or even awk: $ awk '$1=$1' ORS=' ' file_with_line_breaks ...


3

cat $file | egrep -v '^;|^$' that will exclude lines that begin with the ';', and empty lines. in regex, ^ indicates the beginning of a line, and $ the end of a line, so ^$ specifies lines where the start of line character and the end of line character are right next to each other.


3

I would probably write some script to expand shell varaibles to generate output config file. Here are some hints: here-strings (<<<) gets expanded you can load variables by ". varaibles.conf" env -i script.sh will run script in clean environment (no extra varaibles) So script will have to construct a new temporary script which will source the ...


3

Another way to do this would be "_ddp "_dd deletes the current line to the null-buffer. This doesn't over write what you had just copied, which I have found helpful a lot of times! The p pastes the line you had copied earlier. Might not be shorter in terms of keystrokes, but knowing this is an option can be very helpful!


3

see http://stackoverflow.com/questions/5858200/sed-replace-every-nth-occurrence The solution uses awk rather than sed, but "use the right tool for the job". It may or may not be possible to do in sed but, even if it is, it will be a lot easier in a tool like awk or perl.


3

A simple text approach where you replace AnyFunction(arg1, &$arg2) by AnyFunction(arg1, $arg2) unless preceded by the word function, will work if your source code is reasonably formatted: no function declaration on the same line as a function call, no comments or newline between function and the function name, no comments containing unbalanced ...


3

Incremental search has this feature, but the replace functions don't. Fortunately, incremental search does have a way to switch to replace mode once you've selected a search term. So: Press C-s to switch to incremental search mode Press C-w to yank the current word into the search buffer You can keep pressing it to append multiple words, and you can also ...


3

You could do something like: eval "cat << __end_of_template__ $(sed 's/[\$`]/\\&/g;s/<%= @\([^ ]*\) %>/${\1}/g' < template) __end_of_template__" That is, have sed replace all the <%= @xxx %> with ${xxx} after having escaped all the $, \ and ` characters and let the shell do the expansion. Or if you can't guarantee that template ...


3

Search for a line that starts with projdir, and replace the whole line with a new one: sed -i 's/^projdir .*$/projdir PacMan/' .ignore ^ and $ are beginning/end-of-line markers, so the pattern will match the whole line; .* matches anything. The -i tells sed to write the changes directly to .ignore, instead of just outputting them


3

The slashes in the regex are messing up with sed's delimiters. But you can use different delimiters than the slash. For example: sed 's#@base_url = "http://dmstaffing-stage.herokuapp.com/"##' xx


2

I answer because the diff/patch method might be of interest in some cases. To define a substitution of lines contained in file blob1 by lines contained in blob2 use: diff -u blob1 blob2 > patch-file For example, if blob1 contains: hello you and blob2 contains: be welcome here the generated patch-file will be: --- blob1 2011-09-08 ...


2

This should work - awk -F"," -v OFS="," ' $NF=="()" {print $1,$2,"NONE";next} {gsub(/\(|\)/,"",$NF);print}' INPUT_FILE Explanation: We set the Field Separator to , and Output Field Separator to ,. Setting OFS gives us the liberty to avoid printing your desired field separators explicitly. We check for a pattern $NF=="()" (where $NF is the last field), ...


2

With sed: echo -e "192.168.0.25,Up,()\n1.2.3.4,Up,(host.domain.com)" | sed 's/()/None/g;s/[()]//g' 192.168.0.25,Up,None 1.2.3.4,Up,host.domain.com Here is an awk-program: BEGIN {FS=","} $3 ~ /\(\)/ { printf ($1","$2",None\n"); next } $3 ~ /\(.+\)/ { gsub ("\\(", "") gsub ("\\)", "") } ...



Only top voted, non community-wiki answers of a minimum length are eligible