This question already has answers here:
Delete the first line that matches a pattern
(5 answers)
Closed 2 years ago.
I have file like this,then now I want to delete only the first def_xxx
abc_xxx
def_xxx
ghi_xxx
abc_yyy
def_yyy
ghi_yyy
It delete the two lines def_xxx,def_yyy.
sed -e '/def/d' myfile.txt
How can I delete the only first line def_xxx??
sed -e '0,/def/{/def/d;}' myfile.txt
This deletes the first occurrence of the pattern.
From its manual:
0,addr2
Start out in "matched first address" state, until addr2 is found. This is similar
to 1,addr2, except that if addr2 matches the very first line of input the
0,addr2 form will be at the end of its range, whereas the 1,addr2 form will
still be at the beginning of its range. This works only when addr2 is a
regular expression.
Ref: https://linux.die.net/man/1/sed
Related
This question already has answers here:
How do I replace single quotes with another character in sed?
(6 answers)
Closed 5 months ago.
Trying to put ' before each line of text and ' at the end of each line of text.
I have been using sed 's/^/1/' file.txt to replace to begging of each line and sed 's/$/0/' file.txt to replace the end of each line.
What I am trying to make work is sed 's/^/'/' and sed 's/$/'/'
This would format my file to make each line reach as a command, when applied to a separate script.
echo abc | sed "s/.*/'&'/"
Output:
'abc'
From man sed:
The replacement may contain the special character & to refer to that portion of the pattern space which
matched
This question already has answers here:
How to replace a whole line with sed?
(6 answers)
Closed 1 year ago.
hello I am needing help to be able to replace a line of a file with ssh and sed.
The problem is the following I have a string for example:
mystring = 1234
The point is that after the = it is not always 1234, so I am not able to replace that line.
sed -i 's/mystring =/mystring=1234/g' file.ini
when I run it two things happen:
mystring = 12341234 (add the numbers)
mystring = 123412345 (don't delete it, leave it there)
and if the variable (sed -i 's/mystring=/mystring=1234/g' file.ini) is executed more than twice, what it does is add the number at the end.
That is why I need to replace the entire line, something that if it is done more than 1 you see it will always remain the same, and if it is different, change it to the value that is set in the command.
From already thank you very much.
You can always match and replace the whole line:
sed -i 's/^mystring=.*/mystring=newvalue/' yourfile
.* in a regular expression matching any number of characters, including none.
You can also change all lines containing a certain pattern:
sed -i '/^mystring=/c\
mystring=newvalue' yourfile
Or with GNU sed:
sed -ie '/^mystring=/c\' -e 'mystring=newvalue' yourfile
This question already has answers here:
Reverse input order with sed
(6 answers)
Closed 4 years ago.
For example, we have:
This is the song that doesn`t end
What sed command will turn it into this?
end doesn`t that song the is This
I've found only how to reverse lines in a file (a.k.a. tac):
sed -n '1!G;h;$p'
This might work for you (GNU sed):
sed -r 'G;:a;s/^(\S+)(\s*)(.*\n)/\3\2\1/;ta;s/\n//' file
Append a newline as a delimiter. Split the current line into three and prepend the first word, the following space and the remainder of the line following the newline in that order. Iterate until the pattern matching fails and then remove the introduced newline.
Could you please try following and let me know if this helps you.
awk '{for(i=NF;i>0;i--){printf("%s%s",$i,(i>1?OFS:ORS))}}' Input_file
This question already has answers here:
How to replace the nth occurrence of a string using sed
(2 answers)
Closed 8 years ago.
Is there any way to replace the nth occurrence of a string in a file using sed?
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
fifth line
jack
seventh line
consider a variable var = jill.
I want to replace the 2nd occurrence of jack with value of variable $var.
This might work for you (GNU sed):
sed ':a;$!{N;ba};s/jack/'"$var"'/2' file
This slurps the file into memory and then substitutes the second occurence of jack for $var.
EDIT:
:a is a place holder for a b command, it tell sed to break/jump to position i.e ba jump to :a.
$! is an address. $ means end of file and ! means not. Put together this means any address which is not-the-end-of-file.
{...} groups the commands between the braces.
N appends a newline and then the next line to the pattern space except when there are no more lines when it passes sed to just passed the last command (if the -n is not set it will print what ever is in the pattern space, if -n is set it will just end processing).
s/jack/'$var"'/2 this is a sed substitution command and replaces the second occurrence of jack by the contents of $var. N.B. the '...' which effectively breaks out sed commands into the underlying shell and then back again allowing the shell variable to be interpolated.
In summary the whole file is slurped into memory and the second occurrence of jack is replaced by the contents of $var.
In most cases this could by replaced by:
sed -z 's/jack/'"$var"'/2' file
Here is an awk version if you like to use awk
awk -v c="$var" '/jack/ && ++a==2 {sub(/jack/,c)}1' file
first line
second line
third line
jack
fifth line
jill
seventh line
This question already has answers here:
Remove everything after the first / including the first / for each line
(3 answers)
Closed 8 years ago.
I'm trying to use command line to find and replace some text. I have a file with a few million lines that are similar to this:
Something-Here/Grafton-WV</loc>
More-Information/Claremore-OK</loc>
This-Is-It/Seminole-OK</loc>
Your-Company/Lunenburg-MA</loc>
What I need to do is remove the slash and everything after it. I've done wildcard find/replace before but I'm not sure what command would need to be used to start at the slash and continue until the end of the line.
Here's what the output should be:
Something-Here
More-Information
This-Is-It
Your-Company
The following one-liner could work for you:
perl -pe 's{/.*}{}' file.txt
Explanation:
Switches:
-p: Creates a while(<>){...; print} loop for each “line” in your input file.
-e: Tells perl to execute the code on command line.
Code:
s{/.*}{}: Remove all characters after the first forward slash from the line
This is usually done with sed:
sed 's|/.*||' file.txt > newfile.txt