How to Remove Only the First Occurrence of a Line in a File Using Sed

How to use sed to replace only the first occurrence in a file?


 # sed script to change "foo" to "bar" only on the first occurrence
1{x;s/^/first/;x;}
1,/foo/{x;/first/s///;x;s/foo/bar/;}
#---end of script---

or, if you prefer: Editor's note: works with GNU sed only.

sed '0,/foo/s//bar/' file 

Source

How to remove only the first occurrence of a line in a file using sed

You could make use of two-address form:

sed '0,/tat/{/tat/d;}' inputfile

This would delete the first occurrence of the pattern.

Quoting from info sed:

 A line number of `0' can be used in an address specification like
`0,/REGEXP/' so that `sed' will try to match REGEXP in the first
input line too. In other words, `0,/REGEXP/' is similar to
`1,/REGEXP/', except that if ADDR2 matches the very first line of
input the `0,/REGEXP/' form will consider it to end the range,
whereas the `1,/REGEXP/' form will match the beginning of its
range and hence make the range span up to the _second_ occurrence
of the regular expression.

sed command to replace first occurrence in file is not working

/tmp/test.txt

Hi foo bar!
This is again foo bar

Use the following sed commando to replace only the first orrouance of the search-string;

sed -e '1 s/foo/linux/; t' -e '1,// s//linux/' /tmp/test.txt

Result:

Hi linux bar!
This is again foo bar

  • Terminal output + OSX version

  • Terminal output; first foo on 3th line

sed remove select char and everything before first occurrence of character in line

You can use

sed -i 's/.*: *//' file.txt

This will remove all text up to last : including : and all spaces (if any) right after the :.

See the online demo:

#!/bin/bash
s='[09/11/2020 15:01:37] Name: Hello!'
sed 's/.*: *//' <<< "$s"
# => Hello!

Removing first occurrence of a string in a file with sed on mac

Could you please try following if you are ok with awk.

##Creating shell variable named TM here.
TMP="export PATH=/Users/Name/with pace/and_variables:$PATH"
awk -v tmp="$TMP" '/CHANGE_THIS/ && ++flag==1{$0=tmp} 1' Input_file

In case you want to save output into Input_file itself then append > temp_file && mv temp_file Input_file in above code too.

Adding explanation for above code too here.

awk -v tmp="$TMP" '              ##Creating an awk variable named tmp whose value is bash variable TMP value.
/CHANGE_THIS/ && ++flag==1{ ##Checking condition if a line is having string CHANGE_THIS and variable flag value is 1 then do following.
$0=tmp ##Setting current line value to value of tmp awk variable here.
} ##Closing the block for condition here.
1 ##By mentioning 1 printing edited/non-edited line here.
' Input_file ##Mentioning Input_file name here.

Sed - remove first occurence of string from file

This sed one-liner will help you:

sed '0,/test3/{/test3/d}' file
test1
test2
test4
test5
test3

This sed command has used sed's address. Sed has different address expressions, this was explained in detail in sed's Info page.

For this command, 0, /pattern/ will focus from the first line till the first line matching /pattern/, then do the action: /pattern/d, that is, remove the first matching line. After this was done, all following lines will not be satisfied with address 0,/pattern/, thus default action was taken: print as they are.



Related Topics



Leave a reply



Submit