Delete specific word from file

sourav.srivastava

New Member
Joined
Jul 28, 2021
Messages
4
Reaction score
3
Credits
33
Hi,

I am looking for a solution to delete specific words with extension a file .
for Example from below file content:

build/blueprint.git
build/soong.git
build/make.git
libnativehelper.git
prebuilts/abi-dumps/ndk.git
prebuilts/abi-dumps/vndk.git
prebuilts/abi-dumps/platform.git

After delete it should look like :

build/
build/
build/

prebuilts/abi-dumps/
prebuilts/abi-dumps/
prebuilts/abi-dumps/


I was trying to do it in vim editor but its not taking *.git as pattern.
Need help Request you for the same.
 


Using a terminal, go into each folder with the files. Use the rm (remove) command the same way you were trying with vim.
Code:
rm *.git


Or, from any folder, you can remove them by using the full path.
Code:
rm /full/path/to/build/*.git
rm /full/path/to/prebuilts/abi-dumps/*.git


If you get a "permission denied" error, use sudo rm instead of just rm.
 
Using a terminal, go into each folder with the files. Use the rm (remove) command the same way you were trying with vim.
Code:
rm *.git


Or, from any folder, you can remove them by using the full path.
Code:
rm /full/path/to/build/*.git
rm /full/path/to/prebuilts/abi-dumps/*.git


If you get a "permission denied" error, use sudo rm instead of just rm.

Hi,

this will not work. There are 2000 lines and is not possible to go one by one manually. That’s the reason I was looking for some other method.
 
rm does not delete "lines"... it deletes files. In this case, it would delete all files that end with the .git extension in the folders you specified. It's good that you didn't listen to me! :)

So, I apologize... I misunderstood your problem. @JasKinasis is our resident expert with vim, and now that I've mentioned him, he may be able to help you when he returns. Please be patient. Or maybe someone else will know how to do this with vim.
 
Last edited:
Hello, Sourav,
You could try this command in the terminal:

sed 's/[A-Z|a-z]*\.git$//g' inputfile.txt > outputfile.txt

This will do what you want, if and only if all of the .git filenames begin with a letter.
It uses the 'sed' (string editor) program to match [anyletter].git at the end of a line, and replaces it with nothing. You only need to substitute your input file name where it says "inputfile.txt", and substitute your desired output file name where it says "outputfile.txt".

Hope that helps.
 
Python is your friend.

This code is for Python 3. You may need to edit the source's path to your python 3 binary and of course. Change the name of both the source file you're reading and the output file you're writing too.
Python:
#!/usr/bin/env python

import os
import string

outfile = open("newfile.txt", "w")

with open("orig.txt", "r") as infile:
    for line in infile.readlines():
        line = line.replace('\n', '')
        if line.endswith(".git"):
            path = os.path.dirname(line)
            if len(path) == 0:
                outfile.write("\n")
            else:
                outfile.write("{}/\n".format(path))
        else:
            outfile.write(line)
 
Hello, Sourav,
You could try this command in the terminal:

sed 's/[A-Z|a-z]*\.git$//g' inputfile.txt > outputfile.txt

This will do what you want, if and only if all of the .git filenames begin with a letter.
It uses the 'sed' (string editor) program to match [anyletter].git at the end of a line, and replaces it with nothing. You only need to substitute your input file name where it says "inputfile.txt", and substitute your desired output file name where it says "outputfile.txt".

Hope that helps.
Hello, Sourav,
You could try this command in the terminal:

sed 's/[A-Z|a-z]*\.git$//g' inputfile.txt > outputfile.txt

This will do what you want, if and only if all of the .git filenames begin with a letter.
It uses the 'sed' (string editor) program to match [anyletter].git at the end of a line, and replaces it with nothing. You only need to substitute your input file name where it says "inputfile.txt", and substitute your desired output file name where it says "outputfile.txt".

Hope that helps.


Hi

Thanks a lot , Yes it worked !!
 
Python is your friend.

This code is for Python 3. You may need to edit the source's path to your python 3 binary and of course. Change the name of both the source file you're reading and the output file you're writing too.
Python:
#!/usr/bin/env python

import os
import string

outfile = open("newfile.txt", "w")

with open("orig.txt", "r") as infile:
    for line in infile.readlines():
        line = line.replace('\n', '')
        if line.endswith(".git"):
            path = os.path.dirname(line)
            if len(path) == 0:
                outfile.write("\n")
            else:
                outfile.write("{}/\n".format(path))
        else:
            outfile.write(line)


Thanks for sharing the script, I will use this to automate the process. As sed 's/[A-Z|a-z]*\.git$//g' v1.txt.copy > outputfile. this has worked sweet and simple for me. Thanks for your effort. really appreciate.
 

Staff online


Latest posts

Top