The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Report generation gmahesh2k Shell Programming and Scripting 3 05-16-2008 03:33 AM
report generation gmahesh2k UNIX for Dummies Questions & Answers 2 05-16-2008 02:41 AM
Generation file copying for tech support backup sjohnson UNIX for Dummies Questions & Answers 12 04-04-2008 08:39 AM
Oracle Report generation DILEEP410 Shell Programming and Scripting 7 01-04-2007 04:52 AM
Bar code generation in to the text file and printing the same using lp command. Manjunath Naik SUN Solaris 0 05-26-2005 03:50 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-15-2008
McLan McLan is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 21
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 (permalink)  
Old 03-15-2008
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almería, Spain
Posts: 393
This way:

Code:
awk '/^Name/{n=$NF}/^Address/{a=$2}/^City/{print n,a,$NF}' FS="( )|(,)" file

  #3 (permalink)  
Old 03-16-2008
McLan McLan is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 21
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 (permalink)  
Old 03-16-2008
yunccll yunccll is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 23
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 (permalink)  
Old 03-16-2008
McLan McLan is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 21
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 (permalink)  
Old 03-16-2008
yunccll yunccll is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 23

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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0