Kihira
Kihira

Reputation: 158

How to determine if a line contains a character in bash?

I would like to make a bash script that uses 2 arguments, file1 file2 that copies all lines from the file1 that contains the letter b into file2 . I have found the solution to determine if a string is contains the letter

if [[ $string == *"b"* ]]; then
  echo "It's there!"
fi

I just can figure how to apply this code to my problem, and run through each line of a random file.

In the course description i have found that this problem can be solved with the usage of head -n tail -n cat echo wc -c wc -l wc -w if case test , but we don't have to limit ourselves to the usage of just these commands.

Upvotes: 0

Views: 532

Answers (1)

Dominique
Dominique

Reputation: 17551

This is the reason why grep has been invented:

grep "b" file1.txt >>file2.txt

(This copies all lines from file1.txt, containing the character b, to file2.txt)

Upvotes: 0

Related Questions