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
# 15  
Old 02-08-2010
try using perl as below:-

Code:
perl  -wnl -e ' /Start/ ... /[a-zA-Z]/ and /\d+\s+\d+/ and print  ;'  infile.txt

BR

SmilieSmilieSmilie

---------- Post updated at 18:37 ---------- Previous update was at 18:33 ----------

or even better

Code:
perl  -wnl -e ' /Start/ ... /^\D+/ and /\d+\s+\d+/ and print  ;'  infile.txt

SmilieSmilieSmilie
# 16  
Old 02-08-2010
NickC, in you sample the first character of the lines that need to be printed are starting with a digit. I am testing for that: /^[a-zA-Z]/ and /^[^0-9]/ mean testing for the case that the first character is a letter or not a digit respectively.

IMO, the construct that you suggest would not work since it would print the labels as well and not every occurence.
# 17  
Old 02-08-2010
ahmad.diab: the perl script worked well, but only printed out the first instance of the number string after /Start/

Scrutinizer: I'm sorry - what I failed to mention was that when the number string (that includes the exponents) was finished, the next word does not always start with the first character. There might be one or two blank characters before the letters begin. I don't know if your code is looking for something as the first character.

This is why I thought it might be easier to start the search with /Start/ and stop the next time awk encounters a line that does NOT contain the string "E+" in it - and then print out all the lines in between! Smilie And then repeat this throughout the file.

For more clarity, I generated some output files. So here are a couple of samples (in red color are the lines I want printed):

Code:
               EPSILON              EXTERNAL WORK      START
                1         -5.1077400E-06          2.3338035E+03
                2         -1.2286651E-05          2.6901846E+03
                3         -2.4254409E-06          4.0875334E+03   
 *** USER INFORMATION MESSAGE 4114 (OUTPX2)
     DATA BLOCK OUGV1    WRITTEN ON FORTRAN UNIT 12, TRL =

Or

Code:
         EPSILON              EXTERNAL WORK      START
               1         -4.3371111E-09          8.6807975E+05 
1    OUTPUT FILES                  PAGE    22

0     BASELINE                                                                                   
 *** SYSTEM INFORMATION MESSAGE 6916 (DFMSYN)
     DECOMP ORDERING
 *** USER INFORMATION MESSAGE 5293 (SSG3A)
    FOR DATA BLOCK KLL
 EPSILON              EXTERNAL WORK      START
                2         -3.1694217E-10          2.4290496E+07 
1    OUTPUT FILES                         PAGE    23

0     BASELINE                                                                            
 *** SYSTEM INFORMATION MESSAGE 6916 (DFMSYN)
     DECOMP ORDERING 
 *** USER INFORMATION MESSAGE 5293 (SSG3A)
    FOR DATA BLOCK KLL
 EPSILON              EXTERNAL WORK      START
                3         -1.1569729E-09          1.5256892E+07 
1    OUTPUT FILES            PAGE    24

0     BASELINE                                                                            
 *** SYSTEM INFORMATION MESSAGE 6916 (DFMSYN)
     DECOMP ORDERING


It is the differing formats that made it difficult for me. Initially, I had it working with a simple awk script that just searched between /START/ and /USER/ and printed the lines in between. But then I came across an output format like the second example! Thanks again for everyone's patience! Smilie

Last edited by NickC; 02-08-2010 at 02:05 PM..
# 18  
Old 02-08-2010
That is quite different from your original input spec.

Try this:
Code:
awk '$1=="START" {flag=1;next} !/^  / {flag=0} flag' infile

try this to remove the spacing:
Code:
awk '!/^  /{p=0}{$1=$1}p;$1=="START"{p=1}' infile

# 19  
Old 02-08-2010
Yeah, sorry - when I posted the first thread I didn't think it made such a big difference but now I know! Live and learn...

I tried those two commands but I'm getting no output at all. Not sure if the !/^ sequence depends on the version of UNIX (like I said above, I'm using AIX and ksh.

Going back to my question above - would it be too complicated or difficult to start the search at START - print out every line that has E+ in it, and stop when a line without E+ is encountered? From what I can tell, that sequence should capture what I'm trying to get. I was trying something like :

awk '/START/ {flag=1;next} /^E\+/ {flag=0} flag {print}' <filename>

hoping that would work - where the E\+ would mean look for "E+" without attaching special character to +. And the ^ would say until E+ is no longer found. But I think you said above that this command wouldn't work. And you're right that it didn't! Smilie But I'm not sure why.
# 20  
Old 02-08-2010
Try this one:
Code:
awk '/START/{p=1;next}p && $2 !~ /E-..$/{p=0} p' file

# 21  
Old 02-08-2010
Got it - that works!! Smilie Now I have to figure out just what you did! Smilie

Thanks again all of you for your patience and help!

Last edited by NickC; 02-08-2010 at 04:45 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