Delete text in file using bash script in linux - 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.

Related

How to remove 1st empty column from file using bash commands?

I have output file that can be read by a visualizing tool, the problem is that at the beginning of each line there is one space. Because of this the visualizing tool isn't able to recognize the scripts and, hence crashing. When I manually remove the first column of spaces, it works fine. Is there a way to remove the 1st empty column of spaces using bash command
What I want is to remove the excess column of empty space like shown in this second image using a bash command
At present I use Vim editor to remove the 1st column manually. But I would like to do it using a bash command so that I can automate the process. My file is not just full of columns, it has some independent data line
Using cut or sed would be two simple solutions.
cut
cut -c2- file > file.cut && mv file.cut file
cut cannot modify a file, therefore you need to redirect its output to a different file and then overwrite the old file with the new file.
sed
sed -i 's/^.//' file
-i modifies the file in-place.
I would use
sed -ie 's/^ //' file
to just remove spaces (in case a line does not contain it)

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 :)

How to insert a text in middle of nth line of file

I am trying to insert a pipe symbol in between nth line of a file. Like the one present in the output of diff command.
Do not want to use VI editor.
For example,desired line is 2nd line of the file:
cat filename
Hi Hi
Hello Hello
This This
is Is
it it
desired output:
cat filename
Hi Hi
Hello | Hello
This This
is Is
it it
For your own sanity, just use awk:
$ awk 'NR==2{mid=length($0)/2; $0=substr($0,1,mid) "|" substr($0,mid+1)} 1' file
Hi Hi
Hello | Hello
This This
is Is
it it
To modify the original file you can add > tmp && mv tmp file or use -i inplace if you have GNU awk.
You basically cannot modify in place some textual file by inserting a character inside a line. You need to read all its content, modify that content in memory, then write the new content.
You might be interested in GNU ed, which can edit a file programmatically (inside some script).
You could use awk (or any other filter) and redirect its output to some temporary file, then rename that temporary file as the original one.
sed '
# select 2nd line
/2/ {
# keep original in memory
G;h
:divide
# cycle by taking the 2 char at the egde of string (still string end by the \n here) and copy only first to other part of the separator (\n)
s/\(.\)\(.*\)\(.\)\(\n.*\)/\2\4\1/
# still possible, do it again
t divide
# add last single char if any and remove separator
s/\(.*\)\n\(.*\)/\2\1/
# add original string (with a new line between)
G
# take first half string and end of second string, and place a pipe in the middle in place of other char
s/\(.*\)\n\1\(.*\)/\1|\2/
}' YourFile
posix sed, so --POSIXfor GNU sed
Sel explain

Search and delete in ubuntu

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.

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.

Resources