ant: replace double empty line with single - ant

I am trying to set up an script that goes through each file and replace all consecutive empty lines with just one empty line. something like:
aaa
bbb
converted to
aaa
bbb
so far I have:
<replace file="web.xml" value="">
<replacefilter>
<replacetoken>\n\n</replacetoken>
<replacevalue>\n</replacevalue>
</replacefilter>
</replace>
but it isn't working at all
so far the only way is:
<target name="-remove-blank">
<replaceregexp file="${basedir}/temp/WEB-INF/web.xml"
match="(\r?\n)\s*\r?\n"
flags="g"
replace="\1"
/>
</target>
but this deletes all blank lines whereas I need it to only delete when there are 2 or more consecutive blank lines

<replaceregexp
file="some.file"
match="(${line.separator}){2}"
replace="${line.separator}"
flags="g"
/>
doesn't work.
-- EDIT --
given file
line1
line2
line3
line4
line5
line6
and using :
<replaceregexp
file="foo.txt"
match="^(${line.separator}){2,}"
replace="${line.separator}"
flags="mg"
/>
or when on windows (better use ${line.separator} and let ant do the work):
<replaceregexp
file="foo.txt"
match="^(\r\n){2,}"
replace="\r\n"
flags="mg"
/>
works :
line1
line2
line3
line4
line5
line6
If lines contain whitespaces you have to use :
match="^(\s*${line.separator}){2,}"
or
match="^(\s*\r\n){2,}"

Related

Ant delete all lines from text file after a certain keyword

Is there a possibility to delete all text lines using Ant in a text file that are after a specific keyword? - after the first occurrence of the keyword.
Example
Line1
Line2
Line3
Line4
Line5
.....
Line1000
I want to delete everything that is in that file that is after "Line3" keyword excluding that line.
Ant's replaceregexp task can handle this pretty easily:
<replaceregexp
file="input.txt"
match="(.*Line3).*"
replace="\1"
flags="s"
/>
Brief explanation: The regex pattern captures everything up to and including "Line3" in a group, then continues to match the rest of the input. The replacement consists of only the captured group, effectively deleting the part you don't want. The s flag is switched on so that newlines are matched with the . wildcard.

Read more than one text file for a foreach loop in Ant and store the line values as different properties

I have a requirement with respect to ant loops.
It says I have 3 different text files each with say 3 lines (each line is a one word string for all files).
Now I need a foreach loop in Ant that reads all 3 files at same time for the first line in each file and stores the values accordingly as 3 different properties. This way for every single iteration of loop I can refer to a target where I can pass these properties for target execution.
The loop will hence run 3 times and execute that target 3 times one for each iteration.
Say for example I have the following sample code:-
<target name="read">
<loadfile property="file" srcfile="./dist/DB_Critical_Stub_Data.txt"/>
<loadfile property="fileversion" srcfile="./dist/version.txt"/>
<loadfile property="filecomponent" srcfile="./dist/component.txt"/>
<foreach list="${file},${fileversion},${filecomponent}" param="line,line1,line2" delimiter="${line.separator}" target="start-stub"/>
</target>
<target name="start-stub">
<property name="stub-name" value="${line}"/>
<property name="stub-version" value="${line1}"/>
<property name="stub-component" value="${line2}"/>
<startStub name="${stub-name}" version="${stub-version}" component="${stub-component"}/>
</target>
Any suggestions/help here would be highly appreciated.

Join Merge only the first two lines of a file using AWK or SED

I have a file like this
Line1
Line2
Line3
Line4
Line5
I need the output like this:
Line1Line2
Line3
Line4
Line5
I tried sed ":a;N;$!ba;s/\n//g" asd.txt but it combines all lines into one.
An awk solution would be like
$ awk '{ORS=(NR==1?"":"\n")}1 ' input
Line1Line2
Line3
Line4
Line5
OR
$ awk '{ORS=(NR==1?"":RS)}1 ' input
Line1Line2
Line3
Line4
Line5
Using sed you can restrict an operation to a specific line number. In this case, we are restricting the append (to pattern space) and substitution to line 1:
sed '1 {N; s/\n//}' file
Note that this solution could also be written without the braces:
sed '1N; s/\n//' file
But please note that this last solution is somewhat less maintainable. Whether or not that's problematic for you is another thing. In either case, the results are:
Line1Line2
Line3
Line4
Line5
You could try the below sed command,
$ sed 'N;0,/\n/s/\n//' file
Line1Line2
Line3
Line4
Line5
N appends the next line into pattern-space. 0,/./ (specifies the range) which helps to do the replacement on the first match only. s/\n// replaces the first newline character with an empty string.
sed '1 {N;s/\n//}'
results
Line1Line2
Line3
Line4
Line5
take line 1 and add the next line to it . Afterwards remove the newline character

How to concat multiple files to a single file at the same time

I am trying to concat multiple files (say 15 txt files) to a single file at the same time by separate ant calls.
Say there are 15 concat() run at the same time.
However, the output file was not expected.
The data in the output file is corrupted.
Does anyone have idea to solve this problem?
Example:
Input 1:
a=1
b=2
c=3
Input 2:
d=4
e=5
f=6
Output:
a=1
b=2
d=4
e
c=3=5
f=6
You can do this with the concat task, which take a resource collection such as `filesets' as nested elements, allowing you to concatenate all the files in a single task call. Example:
<concat destfile="${build.dir}/output.txt">
<fileset file="${src.dir}/input1.txt" />
<fileset file="${src.dir}/input2.txt" />
</concat>

Ruby-on-Rails: Mixing Sanitize and Truncate can be a dirty thing

So stand alone I get what I need. But I want to truncate it, my dynamic text comes out with dirty text globbered with Microsoft Word garbage.
An Example :
&Lt;! [If Gte Mso 9]>&Lt;Xml> &Lt;Br /> &Lt;O:Office Document Settings> &Lt;Br /> &Lt;O:Allow Png/> &Lt;Br /> &Lt;/O:Off...
So how do I get the best of both worlds? Is there a shorthand ruby way to do this? For example a gsub statement that would clip off everything after the 125th char?
if you just want to slice, you can
>> long_ugly_string = "omg this is a long string"
=> "omg this is a long string"
>> long_ugly_string[10..-1]
=> "s a long string"
Reference: http://ruby-doc.org/core/classes/String.html#M000771
so, you are just specifying the starting character (10) and the ending character (-1 tells it to go to the end of the string).

Resources