Using awk to find next word available


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk to find next word available
# 1  
Old 02-05-2010
Using awk to find next word available

I'm trying to use awk to extract certain text from a file. The file looks like this:

Code:
Start
1  10
2  20
3  30
 ...
 ...
 ...
<some word>

Now what I want to extract is the list of numbers (without the search text printed) that occur between "Start" and <some word>. The catch is that <some word> isn't always the same word!

I've gotten close with an awk script that looks like this:

Code:
awk '/Start/ {flag=1;next} /End/ {flag=0} flag {print}'  <filename>

This gives me exactly what I want, provided that <some word> happens to be "End" - problem is that it could be something else. The only sure thing is that it's some kind of letter. So I need to replace /End/ with some kind of variable that says to search between "Start" and the next time a letter A..Z is found.

I thought it should be fairly straightforward but I can't find a way (searched the boards). I'm not looking for anything too elaborate, so please don't spend too much time with complicated code! Smilie I have a workaround - just would like to see if there is a cleaner way to do it!
# 2  
Old 02-05-2010
Try:
Code:
str="End"
awk '/Start/ {flag=1;next} $0~s {flag=0} flag {print}' s="$str" infile


Last edited by Scrutinizer; 02-05-2010 at 03:02 PM..
# 3  
Old 02-05-2010
Another one:
Code:
awk '/Start/{p=1;next}/End/{exit}p' file

# 4  
Old 02-05-2010
Thanks both of you for the fast answers! Unfortunately, they didn't work for my case.

I'm not sure if I'm understanding the coding you are suggesting. Like I said in my initial post, the end of the numerical sequence doesn't occur necessarily with the word "End" - both of your code examples seem to be written as such, so I wasn't sure if I was supposed to replace that with something else.

As I said, the difficulty is that I don't know what is going to end the numerical list. All I know is that it's going to be some kind of word. is there a way to replace the search string /End/ within awk to say that as soon as it encounters any letter from A .. Z, stop the command?
# 5  
Old 02-05-2010
OK, then I think you need this:
Code:
awk '/Start/ {flag=1;next} /[a-zA-Z]/ {flag=0} flag {print}'  <filename>

# 6  
Old 02-05-2010
Weird. That looks perfect - it should do what I want. But when I try that, I only get the one line that has the word Start in it.

I'm using AIX and ksh, if that makes any difference.

I tried doing it backwards by saying find anything BUT a number:

Code:
awk '/Start/ {flag=1;next} /^[0-9]/ {flag=0} flag {print}'  <filename>

... but that didn't work either.


EDIT: I realized that the above code means it will stop when it encounters something that is not a number, but at the BEGINNING of the line. Is that true?

I'd like it to stop extracting as soon as it sees a letter even if it doesn't occur at the beginning of the line.

Last edited by NickC; 02-05-2010 at 03:47 PM..
# 7  
Old 02-05-2010
Code:
awk '/Start/ {flag=1;next} /[A-Za-z]/ {flag=0} flag {print}'  <filename>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. Shell Programming and Scripting

Find a word and increment the number in the word & save into new files

Hi All, I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl: run_build_model sparc_ifu_dec run_drc set_faults -model path_delay -atpg_effectiveness -fault_coverage add_delay_paths P1 set_atpg -abort_limit 1000 run_atpg -ndetects 1000 I would like... (6 Replies)
Discussion started by: jypark22
6 Replies

3. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

4. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

5. Shell Programming and Scripting

Find repeated word and take sum of the second field to it ,for all the repeated words in awk

Hi below is the input file, i need to find repeated words and sum up the values of it which is second field from the repeated work.Im trying but getting no where close to it.Kindly give me a hint on how to go about it Input fruits,apple,20,fruits,mango,20,veg,carrot,12,veg,raddish,30... (11 Replies)
Discussion started by: 100bees
11 Replies

6. Shell Programming and Scripting

awk to find lines containing word that occur multiple times

i have a script that scans a log file every 10 minutes. this script remembers the last line of the log and then uses it to continue monitoring the log when it runs again 10 minutes later. the script searches the log for a string called MaxClients. now, how can i make it so that when the... (7 Replies)
Discussion started by: SkySmart
7 Replies

7. Shell Programming and Scripting

perl lwp find word and print next word :)

hi all, I'm new there, I'm just playing with perl and lwp and I just successfully created a script for log in to a web site with post. I have a response but I would like to have something like this: I have in my response lines like: <div class="sender">mimi020</div> <some html code.....>... (3 Replies)
Discussion started by: vogueestylee
3 Replies

8. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

9. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

10. Shell Programming and Scripting

find a word in a file, and change a word beneath it ??

Hi all, I have a file with lines written somewhat like this. aaaa ccc aa linux browse = no xssxw cdcedc dcsdcd csdw police dwed dwd browse = no cdecec (2 Replies)
Discussion started by: vikas027
2 Replies
Login or Register to Ask a Question