LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-26-2009, 09:17 AM   #1
tboss888
LQ Newbie
 
Registered: Dec 2008
Posts: 10

Rep: Reputation: 0
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
 
Old 01-26-2009, 05:13 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
count=$(awk '/START/{start=NR};/END/{if (NR > start + 1) count++}END{print count}' file)
 
Old 01-26-2009, 05:17 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
...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/ )
 
Old 01-27-2009, 10:27 AM   #4
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
... 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

Last edited by crabboy; 01-27-2009 at 10:30 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script text replacement... matthurne Programming 4 06-07-2011 06:46 PM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-10-2008 11:13 PM
bash script that can read lines of text palceksmuk Programming 1 12-25-2005 03:49 AM
bash script help to parse out text slack guy Linux - Newbie 3 12-30-2004 08:42 AM
Appending Text Files From Bash Script alts Programming 3 11-18-2004 06:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration