I use the command
sed -n "/*/{=;p}" file.txt | sed "{N;s/\n/ /}"
to numbering line.
But, how to save in file or new file?
The file contains
* text one
* text two
* text three
Pipe output of second sed to new file using ">".
Also, there is an nl command in most distros that adds line numbers without needing sed.
Related
http://www.somesite/play/episodes/xyz/fred-episode-110
http://www.somesite/play/episodes/abc/simon-episode-266
http://www.somesite/play/episodes/qwe/mum-episode-39
http://www.somesite/play/episodes/zxc/dad-episode-41
http://www.somesite/play/episodes/asd/bob-episode-57
i have many url's saved in a txt file like show above i want to move everything after the 6th backslash up one line with a sed script
the txt after the 6th backslash is the title and always different i need to select the title so i can play it
so i need it to look like this
fred-episode-110
http://www.somesite/play/episodes/xyz/fred-episode-110
simon-episode-266
http://www.somesite/play/episodes/abc/simon-episode-266
mum-episode-39
http://www.somesite/play/episodes/qwe/mum-episode-39
dad-episode-41
http://www.somesite/play/episodes/zxc/dad-episode-41
bob-episode-57
http://www.somesite/play/episodes/asd/bob-episode-57
using just sed
i can do this with awk but i want to do this with just sed
You can use the following sed command:
sed 'h;s#\([^/]*/\)\{6\}##;p;x;' sed_test.txt
On your input:
Explanations:
h; copy your pattern buffer to your hold buffer
s#\([^/]*/\)\{6\}##; delete until the 6th / the content of your pattern buffer
p; print the pattern buffer
x exchange the pattern buffer and hold buffer content
then do the default action -> print the content of the pattern buffer
You can use this one too
sed -E 's|(.*/)(.*)|\2\n&|' infile
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 have a couple very large files that will not open up in any file editing program. I want to use sed to edit only the first line of headers to take the header and put a descriptor in front of it. My Files are a combination of pipe,comma and tab delimited.
Example:
Name City State Zip
will be ...
ExampleName ExampleCity ExampleState ExampleZip
Like this if you mean the first line of the file:
sed -i '1 s/^.*$/NEW FIRST LINE/' yourfile
Or if the line you mean is not the first, but you only know it starts with "Name":
sed -i '/^Name/s^.*$/NEW HEADER LINE/' yourfile
Added
You can also do this sort of thing
echo Name City State | sed -E 's/([A-Za-z]+)/Example&/g'
I have many lines in a file which only contain '--' on each line which i want to rmeove. But there are many other lines in the file that contain 'SOMETEXT--SOMETEXT'.
sed -i "/--/d" will remove all instances of '--' but I only want to remove all lines that contain only '--'.
You can use ^ and $ to indicate beginning and end of line
sed -i '/^--$/d'
A line containing only -- would match the regex ^--$
If you want to include lines with leading/trailing whitespaces, it could be extended to
^\s*--\s*$
sed -i '/^--$/' file
The ^ and $ chars "anchor" the search to the beginning and end of the line, respectively.
OR if there can be spaces at the front or back AND assuming an modernish sed
sed -i '/^[[:space:]]*--[[:space:]]*$/' file
where [:space:] will find space chars and tabs.
ELSE a total retro sed should handle
sed '/^[ ]*--[ ]*$/' file > newFile && mv newFile file
and if there could be tabs, then just include a tab char along with the space char, i.e.\
[<Space><TAB>]
but not spelled, out, just typing a space char and a tab char will do it.
IHTH
Is there any way to replace the nth occurrence of a string in a file using sed?
I'm using sed -i '0,/jack.*/ s//jill/' to replace the first occurrence.
How can i change it so that it replaces the nth occurrence?
My file contents the following lines:
first line
second line
third line
jack=1
fifth line
jack=
seventh line
I don't know the value after jack=, it can be anything or nothing.
I want to replace the 2nd occurrence of jack= and anything that follows it with jill.
First replace all the newlines with a unique character that does not occur anywhere else in your file (e.g. ^) using tr. You need to do this in order to create a single string for sed. Then pass it to sed and tell it to replace the nth occurrence of your string. Finally, pass the output back through tr to recreate the newlines.
For n=2, the command is:
$ tr '\n' '^' < file | sed 's/jack/jill/2' | tr '^' '\n'
first line
second line
third line
jack
fifth line
jill
seventh line
Update:
It can also be done with sed, WITHOUT changing the newlines first, using the following command:
$ sed ':a;N;$!ba;s/jack/jill/2' file
Alternatively, use awk:
$ awk '/jack/{c+=1}{if(c==2){sub("jack","jill",$0)};print}' file
Try this, sed ':a;N;$!ba;s/word1/word2/n' filename
Here, :a;N;$!ba is used to load the entire file into memory, line by line, so that sed can process the whole file in a single pass. The s/word1/word2/N substitution then replaces every Nth occurrence of word1 with word2.