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
# 8  
Old 02-05-2010
Code:
awk '/Start/{p=1;next}p && !int($0){exit}p' file

# 9  
Old 02-05-2010
Quote:
Originally Posted by NickC
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.
instead of
Code:
/^[0-9]/

try:
Code:
/[^0-9]/

# 10  
Old 02-08-2010
Scrutinizer: Reading some more on awk, I thought the ^ inside the [ ] means that the script will look for a number at the beginning of the line, and the ^ outside the [ ] means it will exclude that. In any case, it unfortunately gave me the same results as before.

Franklin52: I think that worked! However, I realized that sometimes this output file may have more than one instance of such a sequence of numbers like this:

Code:
Start
1  10
2  20
 ...
 ...
<some text characters>

Start 
3 30
4 40
 ...
 ...
<some text characters>

Is there an easy way to tweak your command to let it find all such instances? The awk command without the extra code you put in did find it.

Thanks again to all of you for spending time to help me! Smilie

Last edited by NickC; 02-08-2010 at 10:48 AM..
# 11  
Old 02-08-2010
Quote:
Originally Posted by NickC
Franklin52: I think that worked! However, I realized that sometimes this output file may have more than one instance of such a sequence of numbers like this:

Code:
Start
1  10
2  20
 ...
 ...
<some text characters>

Start 
3 30
4 40
 ...
 ...
<some text characters>

Is there an easy way to tweak your command to let it find all such instances? The awk command without the extra code you put in did find it.

Thanks again to all of you for spending time to help me! Smilie
With a slightly modification of the code:
Code:
awk '/Start/{p=1;next}p && !int($0){p=0}p' file

# 12  
Old 02-08-2010
Wonderful! Now I'm going to have to go read to figure out what you did! Smilie

I also realized something that might have made this easier. The list of numbers seems to always include one positive exponent. So it actually looks like:

Code:
Start
1  1.0E+05
2  2.0E+04
 ...
 ...
<some text characters>

Start 
3  3.0E+06
4  4.0E+02
 ...
 ...
<some text characters>

Knowing this, could we also use a simple search string to begin by searching for Start and then end when an exponent is no longer found? e.g.:

awk '/Start/,/^E+/' <filename>

I tried that but the tricky part seems to be forcing the search to look for E+ as exact text and not treat the + as a special character. I tried combinations of putting the E+ in quotes as well as with a backslash before the + but it didn't work.
# 13  
Old 02-08-2010
Quote:
Originally Posted by NickC
Scrutinizer: Reading some more on awk, I thought the ^ inside the [ ] means that the script will look for a number at the beginning of the line, and the ^ outside the [ ] means it will exclude that.
Hi NickC it really is the other way around.

So it is because of the scientific notation that was not in your original sample that the other solutions did not work.

Well this should work as well then:
Code:
awk '/Start/ {flag=1;next} /^[a-zA-Z]/ {flag=0} flag {print}'  <filename>

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

or for a more cryptic one
Code:
awk '/^[^0-9]/{p=0};p;/Start/{p=1}' <filename>


Last edited by Scrutinizer; 02-08-2010 at 11:56 AM..
# 14  
Old 02-08-2010
Thanks Scrutinizer for the clarification!

I'm confused by your other code suggestions though. If I want awk to look for the next time that a scientific notation is NOT used, then how does /^[a-zA-Z]/ or /^[^0-9]/ work? Wouldn't they get confused as well whether it's looking for a word or the letter "E" in the scientific notation?

Could we not search for lines between /Start/ and the next line where E+ is no longer encountered? Or is that what your code does? I tried it but it showed all lines from Start until the end of the file.

Last edited by NickC; 02-08-2010 at 12:31 PM..
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