nick shmick
nick shmick

Reputation: 925

How to remove a line from a file in bash?

I have a file that named tmp that contains 4 strings:

dirs
f1
f2
f3

I want to stay with

f1
f2
f3

What bash command helps me to accomplish this? (Only bash please, no sed or awk.)

Upvotes: 0

Views: 203

Answers (3)

Vincent Achard
Vincent Achard

Reputation: 231

regular expression with grep: either

grep 'f' tmp > name_of_output_file

or

grep '[^"dirs"]' tmp > name_of_output_file

work for me

Upvotes: 0

heldt
heldt

Reputation: 4266

One of many ways is using tail:

tail -n 3 tmp

Upvotes: 1

arco444
arco444

Reputation: 22821

You could use:

grep -v "dirs" tmp

The -v argument of grep will negate the match, i.e. it will exclude anything matching dirs from the file tmp

Or if you wanted a pure bash solution:

while read -r line
do
  if ! [[ $line =~ "dirs" ]]
  then
    echo $line
  fi
done < tmp

Upvotes: 2

Related Questions