Help! Printing out CSV output from awk Pattern Match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help! Printing out CSV output from awk Pattern Match
# 1  
Old 05-22-2015
Help! Printing out CSV output from awk Pattern Match

Hi,

I need to search for a word using Awk and print out the line the word is in and every line after the search phrase until I hit this #------------. Then I need to send it to a csv file.

So basically the input file format is like this:
Code:
#--------------------------------------------------------------------------
[Rotor_Type] = INT_LSDEF_342        

rotor_option = 544

# RMS Related configurations
accum_tm = 3360
enable = 0
tunnel_hp = 150
modulation = 0
cum_hp = 600
cum_reset = 1350
persist = 0
lint_select = 0
moor_ghp = 5625.452
margin = 145.052
max_eng = 2327.323
eng = 15.331

#--------------------------------------------------------------------------

[Rotor_Type] = INT_LSDEF_342        

rotor_option = 544
.
.
.

I tried using pattern matching but I can't edit the output.
I also searched for [Rotor_Type] and tried printing everything after, but it didn't work.
This is what I have so far:

Code:
#!/usr/bin/gawk -f

# Opening block, done before the passed in file is parsed.
BEGIN 	{
			count = 0;
			FS = " ";
			print "Variable, Value" > "outputfile.txt"
		}

# Body block, passed in file is parsed and the lines below are executed for each line.
					
					{
						if($1 == "[Rotor_Type]")
						{
							print $1, ", ", ", ",$3 > "outputfile.txt";
							count++;
							next

							while($1 =! "[Rotor_Type]")
							{
								next
								print NR
								print $1, ", ", ", ",$3 > "outputfile.txt";	
							}	
						
						}
							
						

					}
# Ending block, done after the passed in file is parsed.
END   	{
			print count, " values found.";
		}

Can you please help me? I'm very new to AWK
Moderator's Comments:
Mod Comment Please use CODE tags for sample input and output as well as for code segments.

Last edited by Don Cragun; 05-22-2015 at 04:44 PM.. Reason: Add CODE and ICODE tags; fix spelling errors.
# 2  
Old 05-22-2015
Is this a homework assignment?

A normal CSV formatted output file uses comma as a field separator; not a comma and a space (as shown in your header output; and not a space, a comma, and two more spaces as produced for your other output lines in the main body of your script. The heading you're printing shows two output fields, but the printf commands in the main body of your loop are printing three fields (with the middle field empty).

Please show us the output you're trying to produce from your sample input file (in CODE tags).

What is the name of your awk script file? What is the name of your input file? What file do you want to contain your output?

(Note that any code following a next command in an awk script to the end of that block of code will never be executed.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get output of multiple pattern match from first field to a file

Hi All, Greetings! I have a file of 40000+ lines with different entries, I need matching entries filterd out to their files based on first filed pattern for the matching : For example: All server1 entries (in field1) to come together with its path in 2nd field. The best output I want... (9 Replies)
Discussion started by: rveri
9 Replies

2. Shell Programming and Scripting

awk pattern match not printing desired columns

Hi all, I'm trying to match the following two files with the code below: awk -F, 'NR==FNR {a=$0; next} ($12,$4) in a {print $12,$1,a}' OFS="," file4.csv file3.csv but the code does not print the entire row from file4 in addition to column 12 and 1 of file3. file4: o,c,q,co,ov,b... (1 Reply)
Discussion started by: bkane3
1 Replies

3. Shell Programming and Scripting

Printing next 6 lines from of pattern match

Hi, i have a big file having many opcodes. if (opcode="01110000000100000000" ) then --fadd result.opcode := "01110000000100000000"; result.s0 := '1'; result.s1 := '1'; result.s2 := '0'; result.inst := '0'; result.scalar := '1';... (7 Replies)
Discussion started by: twistedpair
7 Replies

4. Shell Programming and Scripting

Multiple pattern match and print the output in a single line

I need to match two patterns in a log file and need to get the next line of the one of the pattern (out of two patterns) that is matched, finally need to print these three values in a single line. Sample Log: 2013/06/11 14:29:04 <0999> (725102) Processing batch 02_1231324 2013/06/11... (4 Replies)
Discussion started by: rpm120
4 Replies

5. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

6. UNIX for Dummies Questions & Answers

Printing nth and n+1th line after a pattern match

Hi , I want to print the nth and n+1 lines from a file once it gets a pattern match. For eg: aaa bbb ccc ddd gh jjjj If I find a match for bbb then I need to print bbb as well as 3rd and 4th line from the match.. Please help..Is it possible to get a command using sed :) (6 Replies)
Discussion started by: saj
6 Replies

7. Shell Programming and Scripting

Using AWK to match CSV files with duplicate patterns

Dear awk users, I am trying to use awk to match records across two moderately large CSV files. File1 is a pattern file with 173,200 lines, many of which are repeated. The order in which these lines are displayed is important, and I would like to preserve it. File2 is a data file with 456,000... (3 Replies)
Discussion started by: isuewing
3 Replies

8. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

9. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies

10. Shell Programming and Scripting

Perl: Printing Multiple Lines after pattern match

Hello People, Need some assistance/guidance. OUTLINE: Two files (File1 and File2) File1 has some ids such as 009463_3922_1827 897654_8764_5432 File2 has things along the lines of: Query= 009463_3922_1827 length=252 (252 letters) More stufff here ... (5 Replies)
Discussion started by: Deep9000
5 Replies
Login or Register to Ask a Question