Search and delete in ubuntu - linux

Hi i am using ubuntu shell, i am trying to delete unique line in a file after the user has key in 2 inputs however is not deleting in the txt docs. Is there something wrong with my code? thanks. ":" is a delimiter in the txt
read input1
read input2
sed "/$input1:$input2/d" sample.txt

You need inplace change and you could use -i option with your command like:
sed -i.bak "/$input1:$input2/d" sample.txt
So post your command you will get additional file:
sample.txt.bak - as original file
sapmple.txt with line containing your pattern deleted.

Related

How to use --file=script-file option to input a file to search and replace in sed command

I am doing a jenkins migration using jenkins-cli where in one step I am using sed command to replacing values manually as like below :
sed 's/mukesh/architect/g' target_file
But I would like to enter all the possible values in Input file with two column with delimeter as = and supply to target file
Input file looks like
ex:
mukesh=architect
abdul=manager
Now I want to use this file as input in sed command for search and replace in my target file. Instead of using s///g manually, I want to use the below option that I found in man page
-f script-file, --file=script-file
But not sure how to use this input file to auto search and replace the pattern in to the target file. It would be grateful if I get any samples, examples.
You can use below code to read input file, parse it and update outfile.
Here I am reading input file, separating values based on delimeter "=" and then updating outfile/target file.
while read name
do
x=`echo $name|cut -d"=" -f1`
y=`echo $name|cut -d"=" -f2`
sed -i "s/$x/$y/g" outfile
done < inputfile
This should solve your problem. Let me know if you are looking for something else or extra.cheers :)

Want to append records in two file using shell script

My first input file contains records name abc.txt:
abc#gmail.com
bscd#yahoo.co.in
abcd.21#gmail.com
1234#hotmail.com
My second file contains record name details.txt:
123456^atulsample^1203320
I want my final file having output to be Final.txt:
abc#gmail.com^123456^atulsample^1203320
bscd#yahoo.co.in^123456^atulsample^1203320
abcd.21#gmail.com^123456^atulsample^1203320
I have uses sed command but I am not getting my required output.
Kindly help as I don't have much knowledge in shell scripting.
try something like this;
#!/bin/bash
while read -r line
do
detail="$line"
sed '/^[ \t]*$/d' abc.txt | sed "s/$/^${detail}/" >> Final.txt
done < "details.txt"
this is to delete blank lines;
sed '/^[ \t]*$/d' abc.txt
this is to append from details.txt
sed "s/$/^${detail}/"

Getting extra line when output to file?

I'm using a diff command and it's printing out to a file. The file keeps getting an extra line in the end that I don't need to appear. How can I prevent it from being there?
The command is as follows:
diff -b <(grep -B 2 -A 1 'bedrock.local' /Applications/MAMP/conf/apache/httpd.conf) /Applications/MAMP/conf/apache/httpd.conf > test.txt
The file being used is here (thought I don't think it matters): http://yaharga.com/httpd.txt
Perhaps at least I'd like to know how to check the last line of the file and delete it only if it's blank.
To delete empty last line you can use sed, it will delete it only if it's blank:
sed '${/^\s*$/d;}' file
Ok i made research with your file on my MacOS.
I created file new.conf by touch new.conf and then copied data from your file to it.
btw i checked file and didn't have extra empty line at the bottom of it.
I wrote script script.sh with following:
diff -b <(grep -B 2 -A 1 'bedrock.local' new.conf) new.conf > test.txt
sed -i.bak '1d;s/^>//' test.txt
It diffed what was needed and deleted first useless row and all > saving it to a new file test.txt
I checked again and no extra empty line was presented.
Additionaly i would suggest you to try and delete the extra line you have like this: sed -i.bak '$d' test.txt
And check a number of lines before and after sed = test.txt
Probably your text editor somehow added this extra line to your file. Try something else - nano for example or vi

How to replace a string at the end of a line in a text file with another string in UNIX?

I have to replace in a text file the string ".htm" with ".html" if it is placed at the end of line. I should use sed but I don't get how to use it. I tried using grep instead but didn't work.
Use this:
sed 's/\.htm$/.html/' file
It looks for .htm (the dot has to be escaped) whenever it is followed with end of line ($). In that case, it replaces it with .html.
If you want to do an in place edit, add the -i option:
sed -i.bak 's/\.htm$/.html/' file
This will create a backup file.bak while the original will be modified with the new data.
Example
$ cat a
hello this is.htm
hello this is.htm blabla
hello this ishtm
hi!
$ sed 's/\.htm$/.html/' a
hello this is.html
hello this is.htm blabla
hello this ishtm
hi!
or do the following :
1) Open file in vi editor : vi filename
2) go to command mode : press :
3) type the following command and press enter: 1,$s/.htm$/.html/
As suggested in other posts, the same results can be achieved using sed command, but in that case you cannnot make changes in the existing file. Instaed you will have to create a new file. Using the solution above, you can make changes in the existing file.
Hope that helps!!
sed -e 's/\.htm$/\.html/' < foo
assuming foo is the file that you want to operate. output will we standard out, redirect it somewhere useful.

Delete text in file using bash script in linux

I want to create an script that deletes few lines from a file containing the following text
you.com
me.ac.id
burger.co.us
manheal.com
If i want to delete this:
burger.co.us
How is the syntax in shell programming??
sed "/burger.co.us/d" < inputfile > outputfile
will match and delete that line from inputfile and write to outputfile using redirection.
Note that you can't read your input and write to the same file using the above. Instead use the -i field to specify replacement in-place.
See here for more about sed.

Resources