How do I read line by line in a file using while loop, and in each iteration, grep each line to compare to a...
up vote
2
down vote
favorite
I have a file called abc.txt with the contents as follows:
1: It is a shell script
2: There are few lines in this file
3: I am good with programming in C, but beginner in shell
4: We are going towards end of file
5: END OF FILE
I want to read each of the file iteratively, and in each iteration I want to compare the line with "I am good with programming in C, but beginner in shell", and then do some processing.
Any help would be greatly appreciated. Thanks!
command-line grep
add a comment |
up vote
2
down vote
favorite
I have a file called abc.txt with the contents as follows:
1: It is a shell script
2: There are few lines in this file
3: I am good with programming in C, but beginner in shell
4: We are going towards end of file
5: END OF FILE
I want to read each of the file iteratively, and in each iteration I want to compare the line with "I am good with programming in C, but beginner in shell", and then do some processing.
Any help would be greatly appreciated. Thanks!
command-line grep
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a file called abc.txt with the contents as follows:
1: It is a shell script
2: There are few lines in this file
3: I am good with programming in C, but beginner in shell
4: We are going towards end of file
5: END OF FILE
I want to read each of the file iteratively, and in each iteration I want to compare the line with "I am good with programming in C, but beginner in shell", and then do some processing.
Any help would be greatly appreciated. Thanks!
command-line grep
I have a file called abc.txt with the contents as follows:
1: It is a shell script
2: There are few lines in this file
3: I am good with programming in C, but beginner in shell
4: We are going towards end of file
5: END OF FILE
I want to read each of the file iteratively, and in each iteration I want to compare the line with "I am good with programming in C, but beginner in shell", and then do some processing.
Any help would be greatly appreciated. Thanks!
command-line grep
command-line grep
asked 8 hours ago
ShellyBelly
556
556
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
6
down vote
Using a shell loop is unnecessary, as grep already iterates over lines:
grep '^[0-9]: I am good with programming in C, but beginner in shell' input.txt
If there's a matching line, it will be printed. [0-9] defines range of characters that will be matched. We can also extend that to longer numbers [0-9]*: (and I think with perl regex -P option that could be done as [0-9]+:).
If a shell loop is really necessary, we can use case statement for pattern matching
while IFS= read -r line; do
case $line in
*": I am good with programming in C") echo "Matched: $line";;
esac
done < input.xt
add a comment |
up vote
5
down vote
Try this sample code to help identify and modify to suit your needs:
#!/usr/bin/env bash
set -e
set -x
cut -d" " -f2- "$1" | while IFS= read -r line
do
if [ "$line" = "I am good with programming in C, but beginner in shell" ]
then
# Process things here
echo "same"
fi
done
Usage:
Make executable:
chmod +x script.sh
Place script in any folder then run script by passing a file to it:
./script.sh /path/to/data.txt
Info:
cut -d" " -f2- "$1" |: Removes the leading line numbers and passes the results to the while loop.
IFS=: Option before read command to prevent leading/trailing whitespace from being trimmed.
-r: Option passed to read command prevents backslash escapes from being interpreted.
set -e: Bash option to stop script on first error.
-set -x: Bash option used to debug the scrtip.
"$1": The file variable passed to the script in this casedata.txt
1
@George So you're using-d" "to split on whitespace and cut all lines starting from the second one. I'd suggest another way:while IFS= read -r linenum line. The shell will perform wordsplitting on the input line and place first item ( line numbers ) intolinenumvariable, but all other things will go intolinevariable. Aside from other things,casestatement can be used as alternative to[[comparison.
– Sergiy Kolodyazhnyy
7 hours ago
Can I implement this script in #!/bin/sh ?
– ShellyBelly
7 hours ago
@AkshayNandi Yes, but you need to change[[to[because[[is specific tobash. See my answer for portable solution
– Sergiy Kolodyazhnyy
7 hours ago
@SergiyKolodyazhnyy I tried that it didn't do so on the word splitting.
– George Udosen
6 hours ago
@GeorgeUdosen Oh ? Interesting. What was the output you were getting ? Were you still usingcut? Because if you just use the loop I suggested, you can dropcutfrom the script
– Sergiy Kolodyazhnyy
6 hours ago
|
show 2 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
Using a shell loop is unnecessary, as grep already iterates over lines:
grep '^[0-9]: I am good with programming in C, but beginner in shell' input.txt
If there's a matching line, it will be printed. [0-9] defines range of characters that will be matched. We can also extend that to longer numbers [0-9]*: (and I think with perl regex -P option that could be done as [0-9]+:).
If a shell loop is really necessary, we can use case statement for pattern matching
while IFS= read -r line; do
case $line in
*": I am good with programming in C") echo "Matched: $line";;
esac
done < input.xt
add a comment |
up vote
6
down vote
Using a shell loop is unnecessary, as grep already iterates over lines:
grep '^[0-9]: I am good with programming in C, but beginner in shell' input.txt
If there's a matching line, it will be printed. [0-9] defines range of characters that will be matched. We can also extend that to longer numbers [0-9]*: (and I think with perl regex -P option that could be done as [0-9]+:).
If a shell loop is really necessary, we can use case statement for pattern matching
while IFS= read -r line; do
case $line in
*": I am good with programming in C") echo "Matched: $line";;
esac
done < input.xt
add a comment |
up vote
6
down vote
up vote
6
down vote
Using a shell loop is unnecessary, as grep already iterates over lines:
grep '^[0-9]: I am good with programming in C, but beginner in shell' input.txt
If there's a matching line, it will be printed. [0-9] defines range of characters that will be matched. We can also extend that to longer numbers [0-9]*: (and I think with perl regex -P option that could be done as [0-9]+:).
If a shell loop is really necessary, we can use case statement for pattern matching
while IFS= read -r line; do
case $line in
*": I am good with programming in C") echo "Matched: $line";;
esac
done < input.xt
Using a shell loop is unnecessary, as grep already iterates over lines:
grep '^[0-9]: I am good with programming in C, but beginner in shell' input.txt
If there's a matching line, it will be printed. [0-9] defines range of characters that will be matched. We can also extend that to longer numbers [0-9]*: (and I think with perl regex -P option that could be done as [0-9]+:).
If a shell loop is really necessary, we can use case statement for pattern matching
while IFS= read -r line; do
case $line in
*": I am good with programming in C") echo "Matched: $line";;
esac
done < input.xt
edited 6 hours ago
dessert
21.5k55896
21.5k55896
answered 7 hours ago
Sergiy Kolodyazhnyy
68.2k9142302
68.2k9142302
add a comment |
add a comment |
up vote
5
down vote
Try this sample code to help identify and modify to suit your needs:
#!/usr/bin/env bash
set -e
set -x
cut -d" " -f2- "$1" | while IFS= read -r line
do
if [ "$line" = "I am good with programming in C, but beginner in shell" ]
then
# Process things here
echo "same"
fi
done
Usage:
Make executable:
chmod +x script.sh
Place script in any folder then run script by passing a file to it:
./script.sh /path/to/data.txt
Info:
cut -d" " -f2- "$1" |: Removes the leading line numbers and passes the results to the while loop.
IFS=: Option before read command to prevent leading/trailing whitespace from being trimmed.
-r: Option passed to read command prevents backslash escapes from being interpreted.
set -e: Bash option to stop script on first error.
-set -x: Bash option used to debug the scrtip.
"$1": The file variable passed to the script in this casedata.txt
1
@George So you're using-d" "to split on whitespace and cut all lines starting from the second one. I'd suggest another way:while IFS= read -r linenum line. The shell will perform wordsplitting on the input line and place first item ( line numbers ) intolinenumvariable, but all other things will go intolinevariable. Aside from other things,casestatement can be used as alternative to[[comparison.
– Sergiy Kolodyazhnyy
7 hours ago
Can I implement this script in #!/bin/sh ?
– ShellyBelly
7 hours ago
@AkshayNandi Yes, but you need to change[[to[because[[is specific tobash. See my answer for portable solution
– Sergiy Kolodyazhnyy
7 hours ago
@SergiyKolodyazhnyy I tried that it didn't do so on the word splitting.
– George Udosen
6 hours ago
@GeorgeUdosen Oh ? Interesting. What was the output you were getting ? Were you still usingcut? Because if you just use the loop I suggested, you can dropcutfrom the script
– Sergiy Kolodyazhnyy
6 hours ago
|
show 2 more comments
up vote
5
down vote
Try this sample code to help identify and modify to suit your needs:
#!/usr/bin/env bash
set -e
set -x
cut -d" " -f2- "$1" | while IFS= read -r line
do
if [ "$line" = "I am good with programming in C, but beginner in shell" ]
then
# Process things here
echo "same"
fi
done
Usage:
Make executable:
chmod +x script.sh
Place script in any folder then run script by passing a file to it:
./script.sh /path/to/data.txt
Info:
cut -d" " -f2- "$1" |: Removes the leading line numbers and passes the results to the while loop.
IFS=: Option before read command to prevent leading/trailing whitespace from being trimmed.
-r: Option passed to read command prevents backslash escapes from being interpreted.
set -e: Bash option to stop script on first error.
-set -x: Bash option used to debug the scrtip.
"$1": The file variable passed to the script in this casedata.txt
1
@George So you're using-d" "to split on whitespace and cut all lines starting from the second one. I'd suggest another way:while IFS= read -r linenum line. The shell will perform wordsplitting on the input line and place first item ( line numbers ) intolinenumvariable, but all other things will go intolinevariable. Aside from other things,casestatement can be used as alternative to[[comparison.
– Sergiy Kolodyazhnyy
7 hours ago
Can I implement this script in #!/bin/sh ?
– ShellyBelly
7 hours ago
@AkshayNandi Yes, but you need to change[[to[because[[is specific tobash. See my answer for portable solution
– Sergiy Kolodyazhnyy
7 hours ago
@SergiyKolodyazhnyy I tried that it didn't do so on the word splitting.
– George Udosen
6 hours ago
@GeorgeUdosen Oh ? Interesting. What was the output you were getting ? Were you still usingcut? Because if you just use the loop I suggested, you can dropcutfrom the script
– Sergiy Kolodyazhnyy
6 hours ago
|
show 2 more comments
up vote
5
down vote
up vote
5
down vote
Try this sample code to help identify and modify to suit your needs:
#!/usr/bin/env bash
set -e
set -x
cut -d" " -f2- "$1" | while IFS= read -r line
do
if [ "$line" = "I am good with programming in C, but beginner in shell" ]
then
# Process things here
echo "same"
fi
done
Usage:
Make executable:
chmod +x script.sh
Place script in any folder then run script by passing a file to it:
./script.sh /path/to/data.txt
Info:
cut -d" " -f2- "$1" |: Removes the leading line numbers and passes the results to the while loop.
IFS=: Option before read command to prevent leading/trailing whitespace from being trimmed.
-r: Option passed to read command prevents backslash escapes from being interpreted.
set -e: Bash option to stop script on first error.
-set -x: Bash option used to debug the scrtip.
"$1": The file variable passed to the script in this casedata.txt
Try this sample code to help identify and modify to suit your needs:
#!/usr/bin/env bash
set -e
set -x
cut -d" " -f2- "$1" | while IFS= read -r line
do
if [ "$line" = "I am good with programming in C, but beginner in shell" ]
then
# Process things here
echo "same"
fi
done
Usage:
Make executable:
chmod +x script.sh
Place script in any folder then run script by passing a file to it:
./script.sh /path/to/data.txt
Info:
cut -d" " -f2- "$1" |: Removes the leading line numbers and passes the results to the while loop.
IFS=: Option before read command to prevent leading/trailing whitespace from being trimmed.
-r: Option passed to read command prevents backslash escapes from being interpreted.
set -e: Bash option to stop script on first error.
-set -x: Bash option used to debug the scrtip.
"$1": The file variable passed to the script in this casedata.txt
edited 6 hours ago
answered 8 hours ago
George Udosen
18.7k94265
18.7k94265
1
@George So you're using-d" "to split on whitespace and cut all lines starting from the second one. I'd suggest another way:while IFS= read -r linenum line. The shell will perform wordsplitting on the input line and place first item ( line numbers ) intolinenumvariable, but all other things will go intolinevariable. Aside from other things,casestatement can be used as alternative to[[comparison.
– Sergiy Kolodyazhnyy
7 hours ago
Can I implement this script in #!/bin/sh ?
– ShellyBelly
7 hours ago
@AkshayNandi Yes, but you need to change[[to[because[[is specific tobash. See my answer for portable solution
– Sergiy Kolodyazhnyy
7 hours ago
@SergiyKolodyazhnyy I tried that it didn't do so on the word splitting.
– George Udosen
6 hours ago
@GeorgeUdosen Oh ? Interesting. What was the output you were getting ? Were you still usingcut? Because if you just use the loop I suggested, you can dropcutfrom the script
– Sergiy Kolodyazhnyy
6 hours ago
|
show 2 more comments
1
@George So you're using-d" "to split on whitespace and cut all lines starting from the second one. I'd suggest another way:while IFS= read -r linenum line. The shell will perform wordsplitting on the input line and place first item ( line numbers ) intolinenumvariable, but all other things will go intolinevariable. Aside from other things,casestatement can be used as alternative to[[comparison.
– Sergiy Kolodyazhnyy
7 hours ago
Can I implement this script in #!/bin/sh ?
– ShellyBelly
7 hours ago
@AkshayNandi Yes, but you need to change[[to[because[[is specific tobash. See my answer for portable solution
– Sergiy Kolodyazhnyy
7 hours ago
@SergiyKolodyazhnyy I tried that it didn't do so on the word splitting.
– George Udosen
6 hours ago
@GeorgeUdosen Oh ? Interesting. What was the output you were getting ? Were you still usingcut? Because if you just use the loop I suggested, you can dropcutfrom the script
– Sergiy Kolodyazhnyy
6 hours ago
1
1
@George So you're using
-d" " to split on whitespace and cut all lines starting from the second one. I'd suggest another way: while IFS= read -r linenum line. The shell will perform wordsplitting on the input line and place first item ( line numbers ) into linenum variable, but all other things will go into line variable. Aside from other things, case statement can be used as alternative to [[ comparison.– Sergiy Kolodyazhnyy
7 hours ago
@George So you're using
-d" " to split on whitespace and cut all lines starting from the second one. I'd suggest another way: while IFS= read -r linenum line. The shell will perform wordsplitting on the input line and place first item ( line numbers ) into linenum variable, but all other things will go into line variable. Aside from other things, case statement can be used as alternative to [[ comparison.– Sergiy Kolodyazhnyy
7 hours ago
Can I implement this script in #!/bin/sh ?
– ShellyBelly
7 hours ago
Can I implement this script in #!/bin/sh ?
– ShellyBelly
7 hours ago
@AkshayNandi Yes, but you need to change
[[ to [ because [[ is specific to bash. See my answer for portable solution– Sergiy Kolodyazhnyy
7 hours ago
@AkshayNandi Yes, but you need to change
[[ to [ because [[ is specific to bash. See my answer for portable solution– Sergiy Kolodyazhnyy
7 hours ago
@SergiyKolodyazhnyy I tried that it didn't do so on the word splitting.
– George Udosen
6 hours ago
@SergiyKolodyazhnyy I tried that it didn't do so on the word splitting.
– George Udosen
6 hours ago
@GeorgeUdosen Oh ? Interesting. What was the output you were getting ? Were you still using
cut ? Because if you just use the loop I suggested, you can drop cut from the script– Sergiy Kolodyazhnyy
6 hours ago
@GeorgeUdosen Oh ? Interesting. What was the output you were getting ? Were you still using
cut ? Because if you just use the loop I suggested, you can drop cut from the script– Sergiy Kolodyazhnyy
6 hours ago
|
show 2 more comments
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1098445%2fhow-do-i-read-line-by-line-in-a-file-using-while-loop-and-in-each-iteration-gr%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown