Help with awk (making simple/advanced ini parser)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with awk (making simple/advanced ini parser)
# 1  
Old 07-18-2011
Help with awk (making simple/advanced ini parser)

Hello

I'm searching some kind of example (or ready-made solution, but I don't really want it, because I want to learn awk more), to make something like a parser in awk for something like this (I put example, because I don't really know how to explain this):
Code:
[something]
line1=1
line2=0
line3=1
line4=1

[something]
line1=1
line2=0
line3=5
line4=1

[something]
line1=1
line2=0
line3=2
line4=1

[something]
line1=1
line2=0
line3=5
line4=15

And I want to have output like this:
Code:
[something]
line1=1
line2=0
line3=5
line4=1

[something]
line1=1
line2=0
line3=5
line4=15

I mean... I want to export from one file whole lines from "[something]" to "\n" which have line3=5
I have to notice, that lines aren't in same amount, for example there can be:
Code:
[something]
line1=1
line2=0
line3=5
line4=15

But can be also:
Code:
[something]
line1=1
line2=0
line3=5
line4=15
line5=1
line6=6

etc.

Is it possible to made? And sorry for my english, I'm still learning Smilie

Hope, that you will understand me.
jormung
# 2  
Old 07-18-2011
Well, if you want a hint to start with, then consider this: if you run AWK like this:
Code:
awk 'code' RS= file

then it will split file to records separated by empty lines, not by newline characters, so first record (NR==1) will be
Code:
[something]
line1=1
line2=0
line3=1
line4=1

second (NR==2) will be
Code:
[something]
line1=1
line2=0
line3=5
line4=1

and so on. You can then search each of those records for "line3=5" with regular expression.
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 07-18-2011
Thanks, about this RS="" I even tried RS="\n[something]", which is wrong as I think, but I don't really have idea how to write this.
Now I made, that I'm getting this specify line or whole lines, but I have to make for it loop for as I think, right?
And how exactly could I search in whole lines? Because for now, I'm making it in this way (specify line and I'm looking for a way to search all lines):
Code:
awk '{if(NR == 2 && $4 == "line3=5"){print $0} }' RS= file.txt

It is some kind of succes for me Smilie

And about loop, I tried to make it in this way, but it won't send me anything:
Code:
awk '{ble=0; for(NR == 1;  ble<=NR; NR++){if ($4 == "line3=5") {print $4;} } }' RS= file.txt

In my thinking, teoretically it should increase NR by 1, but it seems, that I don't really know how to build a loop "for"
# 4  
Old 07-18-2011
To search whole record you can use // predicate, like this:
Code:
awk '/line3=5/' RS= file.txt

Check if it is giving proper result.
This User Gave Thanks to bartus11 For This Post:
# 5  
Old 07-18-2011
Yes, it's working exactly how I wanted, even I didn't dream, that it could be that short code.
Thank You so much.

Or maybe not, damn, with longer things (not like with my example) it just copy whole ini instead of specify lines, hmm strange... So maybe I'll try to make something with this loop

Last edited by jormung; 07-18-2011 at 08:36 AM..
# 6  
Old 07-18-2011
Through Sed..
Code:
sed -n 'H;/^$/{x;/line3=5/s//&/p};${x;/line3=5/s//&/p}' inputfile

# 7  
Old 07-18-2011
Quote:
Originally Posted by jormung
Yes, it's working exactly how I wanted, even I didn't dream, that it could be that short code.
Thank You so much.

Or maybe not, damn, with longer things (not like with my example) it just copy whole ini instead of specify lines, hmm strange... So maybe I'll try to make something with this loop
Do those longer things have each block separated by an empty line?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Making a parser

input 1..100km 112..403km 500..623km required output 1..51 112..162 500..550 (i.e 50kms added to the initial distance) (2 Replies)
Discussion started by: ANKIT ROY
2 Replies

2. Shell Programming and Scripting

Java stack trace parser in awk

I want the developers to get a mail with Java stack traces on a daily bases. When something is flaged as known issue and will get a fix but mean while this does not need to get sent each dayl. This is what I got so far. It's a bash script that runs some AWK in it. To get the files that needs to... (6 Replies)
Discussion started by: chipmunken
6 Replies

3. Shell Programming and Scripting

More efficient awk parser

I have an awk parser, that works great if the data is NC_0000 (four digits), but if it is not that then the data is parsed. I'm not sure the most efficient way to obtain the desired output. Thank you :). Code: awk 'FNR > 1 && match($0, /NC_0000(*)\..*g\.(+)(.)>(.)/, a){ print a, a, a, a, a }'... (14 Replies)
Discussion started by: cmccabe
14 Replies

4. Shell Programming and Scripting

Develop a simple command line parser

I want to develop a simple command line parser so that then I can print the values of different options :tions. -analyze -filename<fileName> Via man pages I got to know getopt command would be used. Buut how to use it.. how to encrypt this in a perl script.. grateful if u could help (2 Replies)
Discussion started by: shubhamsachdeva
2 Replies

5. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

6. Shell Programming and Scripting

Advanced AWK Regexp substring to int & Replace

Hi! I have a difficult problem, to step up a unknown version number in a text file, and save the file. It would be great to run script.sh and the version gets increased. Example the content of the textfile.txt hello version = x bye This include three steps 1. First find the char after... (2 Replies)
Discussion started by: Beachboy72
2 Replies

7. Shell Programming and Scripting

Advanced sed/awk help

I have thousands of files in HTML that looks like this: .... .... .... <!-- table horaire --> <!-- table horaire --> <table border="0" cellspacing="0" cellpadding="0" class="tblHoraires" summary="Table des horaires de la ligne 12"> <tr> <th scope="row"... (13 Replies)
Discussion started by: charafantah
13 Replies

8. Shell Programming and Scripting

advanced awk

Hi all Input group1 user1 user2 user3 group2 user4 user5 user1 group3 user6 user7 user8 Desired output group1 group2 (12 Replies)
Discussion started by: wakatana
12 Replies

9. UNIX for Advanced & Expert Users

Simple advanced question

This is a fairly basic advanced question! Does anyone happen to know what level of page faults should be acceptable on a fully operational production system? Useful (?) information: Production system using Oracle database Compaq Tru64 UNIX Server (not sure what model, but it's big!) 8... (14 Replies)
Discussion started by: Neil_mw
14 Replies
Login or Register to Ask a Question