sed replace string in a first line - string

How can I replace a string but only in the first line of the file using the program "sed"?
The commands s/test/blah/1 and 1s/test/blah/ don't seem to work. Is there another way?

This might work for you (GNU sed):
sed -i '1!b;s/test/blah/' file
will only substitute the first test for blah on the first line only.
Or if you just want to change the first line:
sed -i '1c\replacement' file

This will do it:
sed -i '1s/^.*$/Newline/' textfile.txt
Failing that just make sure the match is unique to line one only:
sed -i 's/this is line one and its unique/Changed line one to this string/' filename.txt
The -i option writes the change to the file instead of just displaying the output to stdout.
EDIT:
To replace the whole line by matching the common string would be:
sed -i 's/^.*COMMONSTRING$/Newline/'
Where ^ matches the start of the line, $ matches the end of the line and .* matches everything upto COMMONSTRING

this replaces all matches, not just the first match, only in the first line of course:
sed -i '1s/test/blah/g' file
the /g did the trick to replace more than one matches, if any exists.

Related

How to toggle a comment in a line using sed

I have a txt file with a line I want to comment, I am using sed
comment out
sed -i 's/line1/#line1/' myfile.ini
uncomment out
sed -i 's/#line1/line1/' myfile.ini
this works but the problem is that sometimes I run this twice by mistake, and I end up with a line like
##line1
so when I run the uncomment command, the line is now
#line1
is there an alternative to toggle the comment? this is inside a bash script so I am happy to add a few lines if necessary.
thanks
You can use this sed command:
sed -i 's/^[[:blank:]]*line1/#&/' myfile.ini
Here matching pattern is ^[[:blank:]]*line1 which means match start of line followed by 0 or more whitespace characters before matching line1. And replacement is #& to place # before matched string.
Similarly for uncommenting use:
sed -Ei 's/^#([[:blank:]]*line1)/\1/' myfile.ini
Which matches # at line start followed by 0 or more whitespaces before matching line1. It capture part after # in capture group #1 and uses back-reference \1 in replacement to remove just the # part.
Building Upon #Anubhava's Answer,
You can combine the two into a One-Liner to Toggle comments with:
sed -Ei 's/^[[:blank:]]*line1/#&/;t;s/^#([[:blank:]]*line1)/\1/' myfile.ini
For MacOS/OSX and Other BSD Systems, You can do:
sed -Ei'' 's/^[[:blank:]]*line1/#&/;t;s/^#([[:blank:]]*line1)/\1/' myfile.ini
This is a simple "if-else" statement in sed.
More About Sed Branching & Flow Control Here

Find variable pattern match and delete line by sed

I have a variable pattern. And I want to match pattern in file and if pattern is matched then line should be deleted.
I tried with:
sed '/$pattern/d' file.txt
But it doesn't work.
Please give me guidence for the same.
Thanks.
Just do that:
sed /$pattern/d file.txt
The quotes were transforming your variable in a string. Then you need to remove that.
And if you need to to write the changes in the file, just add -i
sed -i /$pattern/d file.txt

delete and replace a line using linux command

I am trying to delete a line with the pattern matches and replacing the entire line with the another line using sed command.
File contents:Sample.txt
Testfile=xxxx
Testfile3=uuuu
Testfile4=oooo
Testfile5=iiii
Testfile2=ikeii
I am using sed command to delete a line contains Testfile3=* and replace by Testfile3=linechanged
sed -i 's/Testfile3=\*/Testfile3=linechanged/' Sample.txt.
But it just appends the replaceable string in the line as shown below
Testfile3=linechanged=uuuu.
I am expecting the output to be
Testfile3=linechanged.
What i am doing wrong?
The star is not matched right:
sed -i 's/Testfile3=.*/Testfile3=linechanged/' Sample.txt
# ^^
.* matches any character (.) for any length (*), so it will match everything till the end of the line.
You can use captured group to keep what will be preserved and use the desired replacement for the rest:
sed -i 's/^\(Testfile3=\).*/\1linechanged/' file.txt
In your case, escaping the Regex token * like \* will match * literally e.g. Testfile3=* would be matched then.
This might work for you (GNU sed):
sed '/Testfile3/cTestfile3=linechanged' file
This matches the line containing Testfile3 and changes it to the required string.

How to remove blank space between some words using sed?

I want to replace characters between specific words in a line (multiple lines). for example:
first second third | first line
first second third | second line
first second third | third line
first second third | forth line
....
I want to replace characters between third and first/second/third/forth etc...using sed or vi in linux.
If this question is already answered, can you please provide me the link?
Thanks!
You can use the following:
sed 's/ |.[^a-z]*//g' text.txt
or if you want to have a space after 'third':
sed 's/ |.[^a-z]*/ /g' text.txt
remember about the -i flag to make permanent changes.
sed -i 's/\ /whatever/g' ej.txt
-i: in file, means that changes are made directly in the file
-s: substitute
-'\ ': to recognize blank space
-g: all matches on each line
Try this
sed 's/second third[^a-zA-Z]*/second third/g' file
It will replace everything between third and this first letter. And if it works use -i if you want to modify the original file

Inserting string in file in nth line after pattern using sed

I want to insert word after nth line after pattern using sed.
I tied to modify this command but it inserts only in first line after pattern.
sed -i '/myPattern/a \ LineIWantToinser ' myFile
What command should I use to insert for example in third line after pattern?
Easiest way to do it with GNU sed is.. (maybe some direct solution exists!?)
sed -n '/pattern/=' file
to see line where pattern is (grep also can be used here with -n)
then if linenumber+ numoflines is for example 123
sed '123aSOME INSERTED TEXT AFTER THAT LINE' file
where little a is append command (after that line, if i is used will be pre pattern line)
ps. I'm eager to see if #neronlevelu (or other sed Lover) will find some better sed solution.
Edit: i've found it, it seems a for append or i for insert must? be on first position on line when using { with ; inside } like
sed '/pattern/{N;N;N;
a SOME TEXT FOR INSERTING
}' file
sed '/pattern/{N;N;N;i \
Line to add after 3 lines with patterne as starting counter
' YourFile
number of N to add line between pattern and inserted line.
there is no check for end of file or pattern in the 3 lines. (not specified in PO)
A version with bash and ed:
ed -s myFile <<<$'/myPattern/+3a\n LineIWantToinser \n.\nwq'
ed enables us to use the line addressing /myPattern/+3.

Resources