I have a file like this:
<?php $unused.. ?><?php
.. content varies.
I need to remove that line and insert <?php to the start.
I've been trying with following, but line doesn't get inserted in the output.
What am I doing wrong here?
sed '/<\?php $unused/d;1s/^/<\?php \n/' testfile
To insert <?php then delete <?php $unused :
sed '1i \
<?php
/<\?php \$unused/d;' testfile
Related
For example;
I'd love to replace /test src path only within <img> tag.
However <p>test</p> should not be touched.
$ cat test.html
<img src="/test" width="18" alt="" /><br>
<p>test</p>
For now I could execute something like;
sed -i '/test'|/hoge|g' test.html
However it changes the word globally.
sed '/<img/s|/test|/hoge|g' test.html would work for one line <img tags
Sed allows the s///g replacement to be prefixed with another /PATTERN/ to restrict the replacement to lines matching PATTERN.
But you should really use an xml parser to be safe.
Another approach with sed:
sed -i 's|\(<img *src="/\)test|\1hoge|' test.html
<img *src="/ is captured and backreferenced using \1 in substitution string.
Following string(test) is replaced with hoge.
I would like to delete the first 100 lines of a text file using sed. I know how delete to the first line by using:
sed '1d' filename
or the 100th line by typing
sed '100d' filename
How do I specify a range? I thought something like this would work:
sed '1:100d' filename
However, this obviously didn't work. Can someone show me how to specify a range? Thanks in advance for your help.
This should work in gnu sed
sed '1,100d' file
awk can also be used to print data based on conditions related to rows.
Like: Following will print the lines (Records in terms of awk) whose number is greater than 100.
awk 'NR>100' inputfile
One can also use other conditions like:
awk 'NR==100' inpuftile #this will print the 100th line
awk 'NR<100' inputfile #this will print 1-99th line
awk 'NR>100' inputfile #this will print from 101st line onwards
awk 'NR>=100' inputfile #this will print from 100th onwards
try: following too:
sed -n '1,100p' Input_file
I want to change lines like:
<A HREF="classes_index_additions.html"class="hiddenlink">
to
<A HREF="classes_index_additions.html" class="hiddenlink">
(note the added ' ' before class) but it should leave lines like
<meta name="generator" content="JDiff v1.1.1">
alone. sed -e 's|\("[^"]*"\)\([^ />]\)|\1 \2|g' satisfies the first condition but it changes the other text to
<meta name="generator" content=" JDiff v1.1.1"/>
How do I get sed to process the correct pairs of double quotes?
You can try this:
sed -e 's/"\([^" ]*\)=/" \1=/g'
But with sed, it may be possible that the regular expression matches other parts of your document that you didn't intend, so best to try it and look over the results to see if there are any unintended side effects!
You can try putting each attributes on a new line and then triming trailing spaces on each line before removing new lines.
sed -r 's/(\w*="[^"]*")/\n\1/g; s/ *\n/\n/g; s/\n/ /g'
This works as follow :
s/(\w*="[^"]*")/\n\1/g
Put every attributes on a new line so your node looks like this
<A
HREF="classes_index_additions.html"
class="hiddenlink">
After that you remove trailing spaces
s/ *\n/\n/g
And remove new lines
s/\n/ /g
Problem : Cannot insert a text using sed
content of file
aa=
i want to add a text after aa= using sed?
the output should be like below
aa=testing
The following should do it:
sed 's/aa=/aa=testing/'
You can try awk if you like.
awk '/aa=/ {$0=$0"testing"}1' file
If you like to make sure it only replace line that only contains aa= and nothing more, do:
awk '/^aa=$/ {$0=$0"testing"}1' file
I have a file with output like this:
MIKE;123456
JOHN-HELPER;654321
SAM.SMITH;182364
I need everything before the delimiter to move to the end of the line, so it'll look like this:
123456;MIKE
654321;JOHN-HELPER
182364;SAM.SMITH
Struggling it work it out with sed... any ideas?
Like this, for example:
$ sed -r 's/([^;]*);(.*)/\2;\1/' a
123456;MIKE
654321;JOHN-HELPER
182364;SAM.SMITH
It "catches" two groups: everything before ; and then the rest. Next step is to print these blocks the other way round: \2;\1.
Or with awk:
$ awk -F";" '{print $2";"$1}' a
123456;MIKE
654321;JOHN-HELPER
182364;SAM.SMITH
It sets ; as field delimiter and then prints the fields the other way round.