help parsing txt with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help parsing txt with awk
# 1  
Old 10-26-2009
help parsing txt with awk

Hi all

I would need some help to introduce 'break' lines into the following
data by using awk:

original data:
Code:
RECORD 1000 aaa xxxxxx
RECORD 1001 aaa xxxxxx
RECORD 1002 bbb xxxxxx
RECORD 1003 bbb xxxxxx
RECORD 1004 bbb xxxxxx
RECORD 1005 ccc xxxxxx
RECORD 1006 ccc xxxxxx
RECORD 1007 ddd xxxxxx
RECORD 1008 ddd xxxxxx
RECORD 1009 ddd xxxxxx
RECORD 1010 ddd xxxxxx
RECORD 1011 eee xxxxxx
RECORD 1012 eee xxxxxx
RECORD 1013 eee xxxxxx
RECORD 1014 eee xxxxxx

desired output:
Code:
RECORD 1000 aaa xxxxxx
RECORD 1001 aaa xxxxxx
RECORD 1002 bbb xxxxxx
RECORD 1003 bbb xxxxxx
RECORD 1004 bbb xxxxxx
RECORD 1005 ccc xxxxxx
RECORD 1006 ccc xxxxxx
break
RECORD 1007 ddd xxxxxx
RECORD 1008 ddd xxxxxx
RECORD 1009 ddd xxxxxx
RECORD 1010 ddd xxxxxx
break
RECORD 1011 eee xxxxxx
break
RECORD 1012 eee xxxxxx
break
RECORD 1013 eee xxxxxx
break
RECORD 1014 eee xxxxxx
break

The point is that I'm not able to process it properly
with awk, so I would need some help...

Code:
awk '{ if( $3 ~ /ddd/ )                                     
       { while ( $3 == "ddd" )                               
         { getline; print $0 }                             
           print "break"
         }                             
       else if( $3 ~ /eee/ )
         { print $0 }
       else { print $0 }
     }' file.dat > new_file.dat

Thanks in advance.

Last edited by yomaya; 10-26-2009 at 02:21 PM.. Reason: code tags, please!
# 2  
Old 10-26-2009
You can start form here:
Code:
awk '$3=="ddd" && !f{$0=RS $0;f=1}$3=="eee"{$0=RS $0}1' file

# 3  
Old 10-26-2009
Code:
nawk '
    # if the third field is "ddd", increment the counter "d"
    $3=="ddd" {d++} 

    # if the counter "d" is 1, print the line containing "break"
    d==1 {print "break"}

    # if the counter "d" is positive (not 0) AND the third field is NOT "ddd", print a line containing "break"
    # and reset the counter "d" to 0
    d&&$3!="ddd"{print "break";d=0}

    # print a current record - could have been written as "{ print $0 }"
    1

    # if the third field is "eee" - print a line containing "break"
    $3=="eee" {print "break"}' myFile


Last edited by vgersh99; 10-26-2009 at 07:10 PM.. Reason: added comments
# 4  
Old 10-26-2009
Wow...

I think I'm definitely missing too much with awk... Could you please briefly give me some insights on this?
# 5  
Old 10-26-2009
For literal "break"
Code:
awk '$3=="ddd" && !f{$0=R$0;f=1}$3=="eee"{$0=R$0}1END{printf R}' R="break\n"  file

# 6  
Old 10-26-2009
Thank you both very much!

Y.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk, sed, shell all words in INPUT.txt find in column1 of TABLE.txt and replce with column2 in

Hi dears i have text file like this: INPUT.txt 001_1_173 j nuh ]az 001_1_174 j ]esma. nuh ]/.xori . . . and have another text like this TABLE.txt j j nuh word1... (6 Replies)
Discussion started by: alii
6 Replies

2. Shell Programming and Scripting

Parsing a txt file according to two different tags

I need to parse a txt file like below. starts from the first occurence of SASN2010Aber.CallEventRecord.egsnPDPRecord { till the last occurence of } in other saying name of the part(header) is SASN2010Aber.CallEventRecord.egsnPDPRecord and the content of the header is in two... (3 Replies)
Discussion started by: snr_silencer
3 Replies

3. Shell Programming and Scripting

Desired output.txt for reading txt file using awk?

Dear all, I have a huge txt file (DATA.txt) with the following content . From this txt file, I want the following output using some shell script. Any help is greatly appreciated. Greetings, emily DATA.txt (snippet of the huge text file) 407202849... (2 Replies)
Discussion started by: emily
2 Replies

4. Windows & DOS: Issues & Discussions

Parsing a UNIX txt to separate files

I have a requirement to parse a dataflex .txt file and break it into separate files within the Windows server env. Is there any special characters I should pay particular attention on the unix side? Any ideas? Thanks in advance (2 Replies)
Discussion started by: kicklinr
2 Replies

5. Shell Programming and Scripting

awk append fileA.txt to growing file B.txt

This is appending a column. My question is fairly simple. I have a program generating data in a form like so: 1 20 2 22 3 23 4 12 5 43 For ever iteration I'm generating this data. I have the basic idea with cut -f 2 fileA.txt | paste -d >> FileB.txt ???? I want FileB.txt to grow, and... (4 Replies)
Discussion started by: theawknewbie
4 Replies

6. Shell Programming and Scripting

Parsing txt, xml files and preparing csv file

Hi, I need to parse text, xml files to get the statistic numbers and prepare summary csv file. What is the best way to parse these file and prepare csv file. Any idea you have , please? Regards, (2 Replies)
Discussion started by: LinuxLearner
2 Replies

7. Shell Programming and Scripting

parsing txt file, saving graphics files

hi everyone, i am a newbie in shell programming. and i want to simply go through a text file that contains 3 "columns", split by ';' customerID ; link-to-contract ; save-as-filename so an example would simply look like this now i want to loop through every line, and save the file from... (3 Replies)
Discussion started by: Confidence
3 Replies

8. Shell Programming and Scripting

awk-files in a txt

There is a file that contains access's data to a web server. Each line of the file (it's big) it's : IP Client - - "Command Path Protocol" code size "client software" Example,for the first line: IP Client is 67.195.37.107, date-hour is , Command Path Protocol is "Get /papers/ISO4nm.pdf ... (1 Reply)
Discussion started by: Mark_orig
1 Replies

9. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies

10. UNIX for Dummies Questions & Answers

echo "ABC" > file1.txt file2.txt file3.txt

Hi Guru's, I need to create 3 files with the contents "ABC" using single command. Iam using: echo "ABC" > file1.txt file2.txt file3.txt the above command is not working. pls help me... With Regards / Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies
Login or Register to Ask a Question