Skip to main content
Solved

Removing a line of text from a .txt file


JRodgers17
Forum|alt.badge.img+4

Hello! 

I am trying to create a script that would remove certain lines/strings from a .txt file. 

I have tried: 
sed "/$String/ d" File.txt

But had no luck, any thoughts or tips? 

Best answer by mark_mahabir

The sed command you have tried should work to remove the lines containing the specified string. However, there are a few things to keep in mind:

  1. Make sure that $String is properly defined in your script. If the variable is not set correctly, then the sed command will not work as expected.

  2. Make sure that File.txt is the correct file name and that it is in the same directory as your script. If the file is located in a different directory, then you will need to provide the full path to the file.

Here's an example of how you can use sed to remove lines containing a specific string:

#!/bin/bash # Define the string to remove string="example" # Define the file to modify file="example.txt" # Use sed to remove lines containing the string sed "/$string/d" "$file" > temp.txt # Replace the original file with the modified file mv temp.txt "$file"

In this example, the sed command is used to remove any line containing the string "example" from the file "example.txt". The modified file is saved as "temp.txt", and then the original file is replaced with the modified file using the mv command.

View original
Did this topic help you find an answer to your question?

2 replies

mark_mahabir
Forum|alt.badge.img+15
  • Jamf Heroes
  • 336 replies
  • Answer
  • February 23, 2023

The sed command you have tried should work to remove the lines containing the specified string. However, there are a few things to keep in mind:

  1. Make sure that $String is properly defined in your script. If the variable is not set correctly, then the sed command will not work as expected.

  2. Make sure that File.txt is the correct file name and that it is in the same directory as your script. If the file is located in a different directory, then you will need to provide the full path to the file.

Here's an example of how you can use sed to remove lines containing a specific string:

#!/bin/bash # Define the string to remove string="example" # Define the file to modify file="example.txt" # Use sed to remove lines containing the string sed "/$string/d" "$file" > temp.txt # Replace the original file with the modified file mv temp.txt "$file"

In this example, the sed command is used to remove any line containing the string "example" from the file "example.txt". The modified file is saved as "temp.txt", and then the original file is replaced with the modified file using the mv command.


JRodgers17
Forum|alt.badge.img+4
  • Author
  • Contributor
  • 12 replies
  • February 24, 2023
mark_mahabir wrote:

The sed command you have tried should work to remove the lines containing the specified string. However, there are a few things to keep in mind:

  1. Make sure that $String is properly defined in your script. If the variable is not set correctly, then the sed command will not work as expected.

  2. Make sure that File.txt is the correct file name and that it is in the same directory as your script. If the file is located in a different directory, then you will need to provide the full path to the file.

Here's an example of how you can use sed to remove lines containing a specific string:

#!/bin/bash # Define the string to remove string="example" # Define the file to modify file="example.txt" # Use sed to remove lines containing the string sed "/$string/d" "$file" > temp.txt # Replace the original file with the modified file mv temp.txt "$file"

In this example, the sed command is used to remove any line containing the string "example" from the file "example.txt". The modified file is saved as "temp.txt", and then the original file is replaced with the modified file using the mv command.


This did work for me! The issue I believe is that I didn't create a temp text file. Also needed to include the entire location of the file and it worked as expected. Really appreciate the help on this one, thank you! 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings