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
Related
I want to replace '_v' with a whitespace and the last dot . into a dash "-". I tried using
sed 's/_v/ /' and tr '_v' ' '
Original Text
src-env-package_v1.0.1.18
output
src-en -package 1.0.1.18
Expected Output
src-env-package 1.0.1-18
This might work for you (GNU sed):
sed -E 's/(.*)_v(.*)\./\1 \2-/' file
Use the greed of the .* regexp to find the last occurrence of _v and likewise . and substitute a space for the former and a - for the latter.
If one of the conditions may occur but not necessarily both, use:
sed -E 's/(.*)_v/\1 /;s/(.*)\./\1-/' file
With your shown samples please try following sed code. Using sed's capability to store matched regex values into temp buffer(called capturing groups) here. Also using -E option here to enable ERE(extended regular expressions) for handling regex in better way.
Here is the Online demo for used regex.
sed -E 's/^(src-env-package)_v([0-9]+\..*)\.([0-9]+)$/\1 \2-\3/' Input_file
OR if its a variable value on which you want to run sed command then use following:
var="src-env-package_v1.0.1.18"
sed -E 's/^(src-env-package)_v([0-9]+\..*)\.([0-9]+)$/\1 \2-\3/' <<<"$var"
src-env-package 1.0.1-18
Bonus solution: Adding a perl one-liner solution here, using capturing groups concept(as explained above) in perl and getting the values as per requirement.
perl -pe 's/^(src-env-package)_v((?:[0-9]+\.){1,}[0-9]+)\.([0-9]+)$/\1 \2-\3/' 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 very much a junior when it comes to the sed command, and my Bruce Barnett guide sits right next to me, but one thing has been troubling me. With a file, can you filter it using sed to select only specific items? For example, in the following file:
alpha|november
bravo|october
charlie|papa
alpha|quebec
bravo|romeo
charlie|sahara
Would it be possible to set a command to return only the bravos, like:
bravo|october
bravo|romeo
With sed:
sed '/^bravo|/!d' filename
Alternatively, with grep (because it's sort of made for this stuff):
grep '^bravo|' filename
or with awk, which works nicely for tabular data,
awk -F '|' '$1 == "bravo"' filename
The first two use a regular expression, selecting those lines that match it. In ^bravo|, ^ matches the beginning of the line and bravo| the literal string bravo|, so this selects all lines that begin with bravo|.
The awk way splits the line across the field separator | and selects those lines whose first field is bravo.
You could also use a regex with awk:
awk '/^bravo|/' filename
...but I don't think this plays to awk's strengths in this case.
Another solution with sed:
sed -n '/^bravo|/p' filename
-n option => no printing by default.
If line begins with bravo|, print it (p)
2 way (at least) with sed
removing unwanted line
sed '/^bravo\|/ !d' YourFile
Printing only wanted lines
sed -n '/^bravo\|/ p' YourFile
if no other constraint or action occur, both are the same and a grep is better.
If there will be some action after, it could change the performance where a d cycle directly to the next line and a p will print then continue the following action.
Note the escape of pipe is needed for GNU sed, not on posix version
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
I am trying to exchange two words in a line but it doesn't work. For example: "Today is my first day of university" should be "my is Today first day of university"
This is what I tried:
sed 's/\([a-zA-z0-9]*\)\([a-zA-z0-9]*\)\([a-zA-z0-9]*\)/\3\2\1/' filename.txt
What am I doing wrong?
I start to make it with \s which means any whitespaces chars.
I use it for match every words with [^\s]*which match with everything but not spaces.
And I had \s* for match withspaces between words. And don't forget to rewrite a space in replacement.
Look a this for an example:
sed 's#\([^ ]*\)\s+#\1 #'
( I use # instead of /)
sed -r 's/^(\w+)(\s+\w+\s+)(\w+)(.*)/\3\2\1\4/'
with your example:
kent$ echo "Today is my first day of university"|sed -r 's/^(\w+)(\s+\w+\s+)(\w+)(.*)/\3\2\1\4/'
my is Today first day of university
for your problem, awk is more straightforward:
awk '{t=$1;$1=$3;$3=t}1'
same input:
kent$ echo "Today is my first day of university"|awk '{t=$1;$1=$3;$3=t}1'
my is Today first day of university
Try this:
sed -rn 's/(\w+\s)(\w+\s)(\w+\s)(.*)/\3\2\1\4/p' filename.txt
-n suppress automatic printing of pattern space
-r use extended regular expressions in the script
\s for whitespace
This might work for you (GNU sed):
sed -r 's/(\S+)\s+(\S+)\s+(\S+)/\3 \2 \1/' file
You are not accounting for whitespace.
use [ \t]+ between words.