LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script: Scanning for text (https://www.linuxquestions.org/questions/programming-9/bash-script-scanning-for-text-699941/)

tboss888 01-26-2009 09:17 AM

Bash script: Scanning for text
 
Hi all. Just looking for some clues to get started.

I want to use bash to scan a text file for a keyword, look at the next character and then increment a counter depending on the character. So it would look for the word START and if the next character in the file is not E from (END) then a counter would be incremented. Any ideas? I'm thinking it would involve awk and sed.

ie.

Code:

START
#
#
END

START
END

START
abcd
END


colucix 01-26-2009 05:13 PM

Code:

count=$(awk '/START/{start=NR};/END/{if (NR > start + 1) count++}END{print count}' file)

unSpawn 01-26-2009 05:17 PM

...and without awk:
Code:

COUNTER=0
 for LINE in $(< sometextfile); do LINE=(${LINE})
 for (( i = 0 ; i < ${#LINE[@]}; i++ )); do
  [ "${LINE[$i]:0:1}" = "E" ] && ((COUNTER++))
 done; done; echo $COUNTER

( http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html http://www.tldp.org/LDP/Bash-Beginne...tml/index.html http://www.tldp.org/LDP/abs/html/ )

crabboy 01-27-2009 10:27 AM

... and since you asked for sed:

Code:

for i in `sed ':t; /START/,/END/ { /END/!{ $!{ N; bt; } }; s/\n//g; s/START//; s/END//; /./!d; }' sometextfile` ; do
echo $i | wc -m
done



All times are GMT -5. The time now is 05:06 AM.