I want to create a batch file with awk, grep or sed that keeps all lines beginning with 'INSERT' and deletes the other lines.
After this, I want to replace a string "change)" by "servicechange)" when the 3rd word in the treated line is "donextsit".
Can someone explain how to do this?
awk '/INSERT/{
if ($3=="donextsit"){
gsub("change","servicechange");
print
}
}' file
since this is homework, something is still not working..you should find out for yourself
sed '
/^INSERT/ ! d;
/^ *[^ ]\+ *[^ ]\+ *donextsit / s/change)/servicechange)/g;
' -i file
Edit: Incorporated Jonathan Leffler's suggestions.
Related
Suppose I have a file having a string AKASHMANDAL
I want to replace 7th positioned character (whatever the character may be) with "D"
Output will looks like
AKASHMDNDAL
I tried with the following command which only add the character after 7th position
sed -E 's/^(.{7})/\1D/' file
This gives me AKASHMADNDAL
How can I replace the character instead of just adding?
Substitute any character in the 7th position using sed
$ sed 's/./D/7' input_file
AKASHMDNDAL
You can simply match one character outside of the capture group:
sed -E 's/^(.{6})./\1D/'
(notice the dot outside the parenthesis)
If you can consider an awk solution. awk can handle it better without regex and with more power to tweak based on positions:
awk '{print substr($0,1,6) "D" substr($0,8)}' file
AKASHMDNDAL
With your shown samples only, please try following awk code. Written and tested in GNU awk. Here is the Online demo for used awk code here.
awk -v RS='^.{7}' '
RT{
sub(/.$/,"",RT)
ORS=RT"D"
print
}
END{
ORS=""
print
}
' Input_file
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'm trying to write a sed command to convert lines:
<http://dbpedia.org/resource/BoA> <http://dbpedia.org/ontology/wikiPageWikiLink> <http://dbpedia.org/resource/Ne-Yo> .
<http://dbpedia.org/resource/BoA> <http://dbpedia.org/ontology/wikiPageWikiLink> <http://dbpedia.org/resource/Tablo> .
to
BoA, Ne-Yo
BoA, Tablo
I know how to match and print using /(/) but I can't find a way to print two matches.
Using awk you can do:
awk -F"[/>]" '/http/ {print $5 ", " $15}' file
BoA, Ne-Yo
BoA, Tablo
Use parentheses and then \1 to print the first match, \2 to print the second match, and so on.
sed 's|<http://dbpedia.org/resource/\([^>]\+\)> <[^>]\+> <http://dbpedia.org/resource/\([^>]\+\)>.*|\1,\2|g' input.txt
A little verbose, though. Put your text into input.txt file.
Less verbose, but also less accurate than #rendon's solution:
sed -e 's?.*/resource/\([^>]*\)>.*/resource/\([^>]*\).*?\1, \2?' input.txt
If it's good enough then this is more readable.
This might work for you (GNU sed):
sed -r 's|[^>]*/([^>]*)>.*/([^>]*).*|\1, \2|' file
How would I remove all text between certain delimiters.
example:
hello;you;are;nice
returns:
hello;you;nice
in sed, i know how to remove text before the first delimiter and after the last, but not sure otherwise...
thanks as always to everyone.
What about using cut -
cut -d; -f2-3
It is quite straigthforward with sed
sed "s/\w*;//3"
awk -F\; -v OFS=";" '{print $1,$2,$4}' file
I am very new to sed so please bear with me... I have a file with contents like
a=1
b=2,3,4
c=3
d=8
.
.
I want to append 'x' to a line which starts with 'c=' and does not contain an 'x'. What I am using right now is
sed -i '/^c=/ s/$/x/'
but this does not cover the second part of my explanation, the 'x' should only be appended if the line did not have it already and hence if I run the command twice it makes the line "c=3xx" which I do not want.
Any help here would be highly appreciated and I know there are a lot of sharp heads around here :) I understand that this can be handled pretty easily through bash but using sed here is a hard requirement.
You can do something like this:
sed -i '/^c=/ {/x/b; s/$/x/}'
Curly brackets are used for grouping. The b command branches to the end of the script (stops the processing of the current line).
b label
Branch to label; if label is omitted, branch to end of script.
Edit: as William Pursell suggests in the comment, a shorter version would be
sed -i '/^c=/ { /x/ !s/$/x/ }'
awk is probably a better choice here as you can easily combine regular expression matches with logical operators. Given the input:
$ cat file
a=1
b=2,3,4
c=3
c=x
c=3
d=8
The command would be:
$ awk '/^c=/ && !/x/ {$0=$0"x"; print $0}' file
a=1
b=2,3,4
c=3x
c=x
c=3x
d=8
Where $0 is the awk variable that contains the current line being read.
This might work for you (GNU sed):
sed -i '/^c=[^x]*$/s/$/x/' file
or:
sed -i 's/^c=[^x]*$/&x/' file