How to modify lines in file using SED - sed

I have a file in which each line starts with test.x:x.
I want to delete .x:x from each line using sed. I want to know how can I do that?
I tried below but it deletes the whole line:
sed '/.x:x/d' tt.txt

I fixed it. here's what i wanted.
I had a file which contains lines as below.
harpreet$ cat ttt.txt
test1.set:set
test2.set:set
test3.set:set
test4.set:set
test5.set:set
and I wanted to print only test on the console using sed as below.
harpreet$ sed 's/.set:set//' ttt.txt
test1
test2
test3
test4
test5
harpreet$$

Related

What is this sed command doing?

I am looking for non printable characters into a file, and I found this web page.
It shows the following command:
sed "l" file
If I am not mistaken, according to man, this option is:
List out the current line in a ''visually unambiguous'' form.
Moreover, when I run this command on a fake file with one line, the output is as follow:
The line is displayed twice, but each displayed line (in the output) contains at most 69 bytes of the input line. The rest of the line is displayed at the next line.
The second time the line is displayed, it is in its full length.
fake file
toto, titi, tatafdsfdsfdgfgfdsgrgdfgzfdgzgffgerssssssssssssssssssssssssss
Command
sed "l" fake_file
output
$ sed "l" fake_file
toto, titi, tatafdsfdsfdgfgfdsgrgdfgzfdgzgffgerssssssssssssssssssssss\
ssss$
toto, titi, tatafdsfdsfdgfgfdsgrgdfgzfdgzgffgerssssssssssssssssssssssssss
Questions
What does ''visually unambiguous'' exactly mean ?
Why is the output like this ? I was expecting only one line with the $ sign at the end. I was also not expecting output to be displayed on 69 bytes max.
Environment
Tested with same output on:
sed (GNU sed) 4.7
sed (GNU sed) 4.2.2
By default, sed outputs the result after processing a line. If you handle the output yourself, tell sed not to output the line by the -n switch.

how to do sed in-line replacement without backup file if the original file is not changed ? in case the file was not changed

I would like to retain a backup file, only if sed altered the original file.
for example:
I have the following file:
# cat test
This is example file
abcd
efgh
process with sed so there is nothing to change:
# sed -i.BAK "s/AAAA/BBBB/" test
The "test" file is not changed because nothing matched. In this case, I would like to avoid the backup file that was created:
# md5sum test*
d3ca57583595576338ad6f9a01276cd5 test
d3ca57583595576338ad6f9a01276cd5 test.BAK
I learned that what I was asking is not possible by "sed" as I suspected by RTFM.
I solved by adding "if [ grep ... ] " on the expression needed to replace.
The "sed" is performed if and only if the expression exists.
Thanks for the people that commented.

Using sed can't replace text in file with contents of variable

I have $subs variable and bbb file.
$subs variable contents:
echo $subs
http://somesite21.something.com:8088/premier-esb-emulator-app/services/es bServiceStub
bbb file contents:
BBBBbbbAAAAA
The command I try to use:
sed "s|bb|${subs}|" bbbb
But it doesn't work, error output:
sed: -e expression #1, char 85: unterminated `s' command
Please advise. Thanks in advance!
The following is my script:
#!/bin/bash
set -x
subs="http://somesite21.something.com:8088/premier-esb-emulator-app/services/es bServiceStub"
sed "s|bb|${subs}|" bbb
This is how bbb file looks like:
BBBBbbbAAAAA
and here is the output:
I know it is a bad answer, but it seems to be working for me.
__UPDATE__
Let's see If I got you right:
Assuming your working environment is unix-like
Jenkins sets a variable, namely subs. I assumed it is an exported var
You need to replace some part of a file, called bbb.
Substitution would be a single line of code.
So, this still works for me:
Am I missing something ?
__UPDATE_II__
I think I found the problem in your case: the way you set the env variable. Please look at my console output below and pay extra attention to the way I set subs var from output.txt file. It works the way I did it so it should work on your side too.
I decided too add commands here for your convenience:
cat output.txt
export subs=`cat output.txt`
echo ${subs}
sed "s|bb|${subs}|" bbb
Let's hope this time it will work for you =]

Add filter within the perl script to remove unwanted lines from the console/output

Basically, I want to add the filter to the output of my perl script. This filter would chop the redundant lines matching the pattern , 'Jobs found shutdown' and would result in the clean output. Now what and where should I use grep or sed to implement this approach?? And this filter should be the part of the script which would help in getting the clean output.
I am planning to use below sed command to match the lines and remove them from the console output. But need help in implementation
have the script which has some redundant lines from the server in its output.At the end of the script , I would be running the following sed command to clear the output of the script at the console.
"sed -i '/No Job found./d' ";
I think grep is your friend in this case. Something along the lines
perl ... | grep -v "No Job found"
Will result in only lines not containing No Job found being printed

sed add line if not exists

I need to make a change in the php.ini configuration file via sed (or similar).
I need to add the following text:
extension=solr.so
The line has to be added as line number 941 in the configuration file. However, if the file is already there, it should not be added again.
I guess there are two approaches: 1) replace line 941 with the text, or 2) search for the text and add it to line 941 if there are not matches.
I have the following command that works fine, except that the line is added again if the script is run again:
sed '941i\
extension=solr.so' /etc/php5/apache2/php.ini > /etc/php5/apache2/php.ini
How can I make sure that this command does not add the line if it is already there?
The easiest way would be to test before using grep, for example:
grep -q -e 'extension=solr.so' file || sed '...'
Also, it is estrange that you need exactly that line. You should add it at the end, or something like that.
Also, note that taking the same file as input and output never should be done. This can damage the file badly. You should be using the -i sed parameter to do in-place editing.