awk- report generation from input file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk- report generation from input file
# 1  
Old 03-15-2008
awk- report generation from input file

I have input file with below content:

Person:
Name: Firstname1 lastname1
Address: 111, Straat
City : Hilversum

Person:
Name : Fistname2 lastname2
Address: 222, street
Cit: Bussum

Person:
Name : Firstname2 lastname3
Address: 333, station straat
City: Amsterdam

I need the output file withe the below contents:
lastname1 111 Hilversum
lastname2 222 Bussum
lastname3 333 Amsterdam

is it possible, if yes how?

Thanks
# 2  
Old 03-15-2008
This way:
Code:
awk '/^Name/{n=$NF}/^Address/{a=$2}/^City/{print n,a,$NF}' FS="( )|(,)" file

# 3  
Old 03-16-2008
awk- report generation from input file

Thanks Klashxx
its Wonderful, it works.
I was trying to undersand what does FS="( )|(,)" do?
Could you please give more info on this, however without the above also the output is same
awk '/^Name/{n=$NF}/^Address/{a=$2}/^City/{print n,a,$NF}' inpfile
lastname1 111, Hilversum
lastname2 222, Bussum
lastname3 333, Amsterdam

now the request is different:
the input file is :
Person:
Name: Firstname1 lastname1
Address: 111, Street "Narder straat"
City : Hilversum

Person:
Name : Fistname2 lastname2
Address: 222, Street "Zoud straat"
City: Bussum

Person:
Name : Firstname2 lastname3
Address: 333, Street "Station straat"
City: Amsterdam


I need the outout file as below:
lastname1 111 "Narder straat" Hilversum
lastname2 222 "Zoud straat" Bussum
lastname3 333 "Station straat" Amsterdam

Thanks again
# 4  
Old 03-16-2008
FS means Field Separator , using FS="( )|(,)" means current FS is the space or ','
if you use the statement, the result is
lastname1 111 Hilversum
lastname2 222 Bussum
lastname3 333 Amsterdam

without FS="()|(,)" result is :
lastname1 111, Hilversum
lastname2 222, Bussum
lastname3 333, Amsterdam

they are different, first is 111 and the next is 111,

Code:
> cat data
Person:
Name: Firstname1 lastname1
Address: 111, Street "Narder straat"
City : Hilversum
 
Person:
Name : Fistname2 lastname2
Address: 222, Street "Zoud straat"
City: Bussum
 
Person:
Name : Firstname2 lastname3
Address: 333, Street "Station straat"
City: Amsterdam

Code:
> awk 'BEGIN{FS="( )|(,)"} /^Name/{n=$NF}/^Address/{a=$2" "$4" "$5}/^City/{print n,a,$NF}' data
lastname1 111 "Narder straat" Hilversum
lastname2 222 "Zoud straat" Bussum
lastname3 333 "Station straat" Amsterdam

.Aaron
# 5  
Old 03-16-2008
Hi Aaron,
This is fine, so we are displaying 4 and 5 th fields of Address.
I am not sure if the street contains only 2 fields , it can contain more fields like “your and my straat & others straat as well”, in this case I can't use just 4 th and 5 th fileds.
So to be very clear, my requirement is
I need a 3 rd field from the line that contains Name with FS “ “ space:
I need 2 nd field from the line that contains Address with FS ( ) space and need 2 nd filed from the same line with FS ( “ ) Quote and
I need 2 nd field form the line that contains City with FS ( ) space.

Thanks again.
McLan
# 6  
Old 03-16-2008
Code:
awk 'BEGIN{FS="( )|(,)"} /^Name/{n=$NF}/^Address/{split($0, arr, "\""); a=$2" \"" arr[2]"\""}/^City/{print n,a,$NF}' data

maybe, This is what you want!

.Aaron
# 7  
Old 03-17-2008
Or:
Code:
awk '/^Name/{n=$NF}/^Address/{gsub(/Address: *|,/,"");a=$0}/^City/{print n,a,$NF}' FS='( )|(,)' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Report generation using script

Hi all I have a unix script that generates a report with the following information: uptime, mounted file systems, disk usage (> 90% --> critical, <75%-90%> --> warning, < 75% healthy), Mem usage, CPU usage and load average. But I would like to create one single report containing all this... (5 Replies)
Discussion started by: fretagi
5 Replies

2. Shell Programming and Scripting

Report generation using perl script

Hi, I have a perl script to read the log file and create a report from it. I have the script file and log file in a different directories. Now i have pipe the log file data to the perl script to create the report (HMTL file). I am using the below command this isn't working tail -f... (4 Replies)
Discussion started by: vel4ever
4 Replies

3. Shell Programming and Scripting

String generation from user input

Hi I have one thing I need advice on, and I don't know where to start so I have no sample code. I want the user to provide input like: 1-3,6,7,9-11 When the input is like this, I want a string to be generated including all the numbers. In the example above, the string would look like: 1... (13 Replies)
Discussion started by: Tobbev
13 Replies

4. Shell Programming and Scripting

File Report Generation

hi all i need to generate a report file that contains the following details of files present in a directory. 1. File name 2.Complete path for each files and directory 3.File size 4.Days older example i have a directory testing that contains sub-directories and some files. i need to make a... (5 Replies)
Discussion started by: yashwantkumar
5 Replies

5. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

6. Shell Programming and Scripting

Report Generation with Grep

All, I am pretty new to Unix Environment. I am not sure if my requirement can be accomplished in Unix. I did try searching this forum and others but could not get an answer. Requirement is explained below: I have a set of files in a folder. file1_unload file2_unload file3_unload... (7 Replies)
Discussion started by: bharath.gct
7 Replies

7. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

8. Shell Programming and Scripting

Report generation

Hello, I got a requirement in writing a KSH script in unix, please help me out the requirement is there are two folders Folder1 and Folder2 and there are same files in the different folders. like file1,file2 in folder1 and file1 and file2 in folder2. I would like to compare all the similar... (3 Replies)
Discussion started by: gmahesh2k
3 Replies

9. UNIX for Dummies Questions & Answers

report generation

Hello, I got a requirement in writing a sheel script in unix, please help me out the requirement is there are two folders Folder1 and Folder2 and there are same files in the different folders. like file1,file2 in folder1 and file1 and file2 in folder2. I would like to compare all the... (2 Replies)
Discussion started by: gmahesh2k
2 Replies

10. Shell Programming and Scripting

Oracle Report generation

Hi, I am beginner in shell programming.In a shell script i found a call to a script 'runrep25m',which i think is to generate oracle reports?Could anyone help me by providing some details about its usage With Thanks & Regards Dileep (7 Replies)
Discussion started by: DILEEP410
7 Replies
Login or Register to Ask a Question