sed

What are the differences between Perl, Python, AWK and sed?

Hello, just want to know what are the main differences among them? and the power of each language (where it's better to use it). Edit: it's not "vs." like topic, just information. ...

How to do multiline search and replace with a script?

I'm trying to replace every multiline import inside a Python source file.. So, the source goes like from XXX import ( AAA, BBB, ) from YYY import ( CCC, DDD, EEE, ... ) ...other instructions... and I'd like to get something like from XXX import AAA, BBB from YYY import CCC, DDD, EEE, ... ...other instructions... ...

How to increment a number in several files if the number is not always the same ?

Hi, I have several files containing this line Release: X I want to increment X in all the files. If X was constant between the files, I could have a bash script looping around the files and doing ($1 containing the former release number and $2 the new one, ie. $1 + 1) : sed 's/Release: '$1'/Release: '$2'/' <$file >$file.new Now,...

[bash] Fill placeholders in file in single pass

I have a skeleton text file with placeholder strings: blah blah blah blah $PLACEHOLDER_1$ blah $PLACEHOLDER_2$ and so on. Specific "form" of placeholders does not matter -- I may change them to whatever most comfortable for specific implementation. I have a bash script where I know values for placeholders, and I need to generate a ne...

[bash] Escape a string for sed search pattern

In my bash script I have an external (received from user) string, which I should use in sed pattern. REPLACE="<funny characters here>" sed "s/KEYWORD/$REPLACE/g" How can I escape the $REPLACE string so it would be safely accepted by sed as a literal replacement? NOTE: The KEYWORD is a dumb substring with no matches etc. It is not sup...

sed in Vista - how to delete all symbols between?

Hello, I have a bat file that I should use to delete a part of one file and save into another one. I need to delete all the symbols between text "[aaa bbb]" and "[ccc ddd]". That is if I have the text: [aaa bbb] 1 2 3 [ccc ddd] I should have as output: [aaa bbb] [ccc ddd] Thank you EDIT: I would like to clarify the question. I sho...

Windows Command to detect and remove text in a file

I have an ascii file and in there somewhere is the line: BEGIN and later on the line: END I'd like to be able to remove those two lines and everything in between from a command line call in windows. This needs to be completely automated. EDIT: See http://stackoverflow.com/questions/425864/sed-in-vista-how-to-delete-all-symbols-between...

maildir headers problem

Hello, I have the followign bash script to update mtimes for maildir files: #!/bin/bash for i in /test/emailfile do date=$(sed -n '/Received: from/ { :a; n; /;/ {s/.*; //p;q}; b a }' "$i") newdate=$(date -d "$date" +'%Y%m%d%H%M.%S') touch -t "$newdate" "$i" done This script has always worked fine, with standard headers ...

Linux command line global search and replace

I'm trying to search and replace a string in all files matched by grep on a linux machine. I've got some pieces of what I want to do, but I'm unsure how best to string them all together. grep -n 'foo' * will give me output in the form: [filename]:[line number]:[text] For each file returned by grep, I'd like replace "foo" with "bar" ...

How do I properly match Regular Expressions?

I have a list of objects output from ldapsearch as follows: dn: cn=HPOTTER,ou=STUDENTS,ou=HOGWARTS,o=SCHOOL dn: cn=HGRANGER,ou=STUDENTS,ou=HOGWARTS,o=SCHOOL dn: cn=RWEASLEY,ou=STUDENTS,ou=HOGWARTS,o=SCHOOL dn: cn=DMALFOY,ou=STUDENTS,ou=HOGWARTS,o=SCHOOL dn: cn=SSNAPE,ou=FACULTY,ou=HOGWARTS,o=SCHOOL dn: cn=ADUMBLED,ou=FACULTY,ou=HOGWARTS...

How can I add a line to a file in a shell script?

I want to add a row of headers to an existing CSV file, editing in place. How can I do this? echo 'one, two, three' > testfile.csv and I want to end up with column1, column2, column3 one, two, three (nb changing the initial CSV output is out of my hands) EDIT: I originally had this as 'using sed' but any standard command w...

sed/awk: DOS to UNIX path substitution within a file

I have a file that contains this kind of paths: C:\bad\foo.c C:\good\foo.c C:\good\bar\foo.c C:\good\bar\[variable subdir count]\foo.c And I would like to get the following file: C:\bad\foo.c C:/good/foo.c C:/good/bar/foo.c C:/good/bar/[variable subdir count]/foo.c Note that the non matching path should not be modified. I know how...

Search and replace in Shell

I am writing a shell (bash) script and I'm trying to figure out an easy way to accomplish a simple task. I have some string in a variable. I don't know if this is relevant, but it can contain spaces, newlines, because actually this string is the content of a whole text file. I want to replace the last occurence of a certain substring w...

Using sed, how do you print the first 'N' characters of a line?

With sed, what is a one liner to print the first n characters. I am doing the following. grep -G 'defn -test.*' OctaneFullTest.clj | sed .... ...

How can I make changes to only the first line of a file?

I would like to know which pattern can I use in sed to make changes in the first line of huge files (~2 GB). The preference for sed is only because I assume it must be faster than a Python or Perl script. The files have the following structure: field 1, field 2, ... field n data and, given the likelihood of having spaces in the ident...

format html with sed

How can I insert newlines in an html file before each table related tag using sed? ...

Is sed blocking?

I had the impression sed wasn't blocking, because when I do say: iostat | sed sed processes the data as it arrives, but when I do iostat | sed | netcat Then sed blocks netcat. Am I right? ...

sed scripting - environment variable substitution

Hi all, I have sed related question: If I run these command from a scrip: #my.sh PWD=bla sed 's/xxx/'$PWD'/' ... $ ./my.sh xxx bla Which is fine. But, if i run: #my.sh sed 's/xxx/'$PWD'/' ... $ ./my.sh $ sed: -e expression #1, char 8: Unknown option to `s' I read in tutorials that to substitute env. variables from shell you need...

How do you use sed from Perl?

Hi, I know how to use sed with grep, but within Perl the below fails. How can one get sed to work within a Perl program? chomp (my @lineNumbers=`grep -n "textToFind" $fileToProcess | sed -n 's/^\([0-9]*\)[:].*/\1/p'`) ...

Using a sed equivalent in a C program

I have a string input to my program of the form: con*.cc I want this to represent the regular expression, con.*.cc. How can I do this inside a C program? Is there something like sed that I can use? ...