awk parsing file to create a database


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk parsing file to create a database
# 1  
Old 03-22-2016
awk parsing file to create a database

Hi Guys,

I have a list a hotels stored in many different text files.

This list is kept in the following format:

Code:
20/03
Hotel:
The Bear Hotel
Honey Street 
Woodstock
UK
Tel:+44-xxxxxx
Rate: 100

21/03
Hotel:
The Bush Hotel
Nice Street
Farnham
UK
Tel:+44-xxxxxx
Rate: 90

22/03
Hotel:
The Bear Hotel
Honey Street 
Woodstock
UK
Tel:+44-xxxxxx
Rate: 100

I would like to make a script (using awk) that parses all files containing this kind of data and that will produce an output file containing all hotels only once and sorted by towns.

Many thanks for your help and keep up the good work.
Cheers,
Fred

Last edited by Scrutinizer; 03-22-2016 at 03:09 PM.. Reason: code tags
freddie50
# 2  
Old 03-22-2016
A Quick way using a semicolon as record separator only works if the are no semicolons in the text, otherwise use a character that does not occur in the text..
The lines between the input records need to be completely empty, there can be no spaces. Try:
Code:
awk '{$1=$1}1' FS='\n' OFS=\; RS= infile | sort -t\; -k3,3 -u | awk '{$1=$1}1' ORS='\n\n' FS=\; OFS='\n'

if your sort combines -u with the -k option

--
Otherwise:
Code:
awk '{$1=$1}1' FS='\n' OFS=\; RS= infile | sort -t\; -k3,3 | awk '!A[$3=$3]++' ORS='\n\n' FS=\; OFS='\n'


Last edited by Scrutinizer; 03-22-2016 at 04:08 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-22-2016
Are hotels with same name in two or more towns possible? Try
Code:
sort -t\; -k3,3 -k5,5 -u

, then.
These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 03-22-2016
Good point, or likewise with the second approach:
Code:
.... | sort -t\; -k3,3 -k5,5 | awk '!A[$3=$3, $5]++' ORS='\n\n' FS=\; OFS='\n'


Last edited by Scrutinizer; 03-22-2016 at 04:32 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 03-23-2016
Code:
perl -00 -alnF'\n' -e '@{$h{"$F[2]$F[4]"}}=@F; END{for(sort {$h{$a}->[4] cmp $h{$b}->[4]} keys %h){print join "\n", @{$h{$_}}}}' freddie50.hotels

Code:
21/03
Hotel:
The Bush Hotel
Nice Street
Farnham
UK
Tel:+44-xxxxxx
Rate: 90

22/03
Hotel:
The Bear Hotel
Honey Street
Woodstock
UK
Tel:+44-xxxxxx
Rate: 100


Last edited by Aia; 03-23-2016 at 03:44 AM..
This User Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Create html <ui> <li> by parsing text file

Hi you all, this is my first post in this forum. I'm italian (please forgive me) :-) so my english will fail to be correct... Anyway, let's get straight to the point! I have a text file like this: ,,,, Disney: 00961-002,,,, ,Pippo: 00531-002,,, ,,Pluto: 00238-002,, ... (5 Replies)
Discussion started by: alcresio
5 Replies

2. Shell Programming and Scripting

Parsing a file name with awk

I have a command to print out the top 5 most cpu intensive commands: ps aux --sort=-%cpu --no-headers I would like to make a printout containing only the parsed file name. So, I want to turn this: $ ps aux --sort=-%cpu --no-headers user 24883 4.3 1.7 2010436 131440 ? Sl ... (2 Replies)
Discussion started by: bedtime
2 Replies

3. Shell Programming and Scripting

awk parsing file

Looking to parse a file to remove the duplicates and get only few fields and uniq rows. input.tx Loc (TC) ID : ssfad_fs Serial : PIC002340098 ... (8 Replies)
Discussion started by: shunya
8 Replies

4. Shell Programming and Scripting

Help in file parsing with awk

Hi, I have a data set as shown below: 09e757fd,22727,2012-03-01,text1,text2,to 3fd0cae7,310,2012-03-01,text3,text4,to 3fd0cae7,310,2012-03-01,text3,text5,to 3fd0cae7,311,2012-03-01,text7,text10,cc 3fd0cae7,311,2012-03-01,text7,text11,to 3fd0cae7,312,2012-03-01,text8,text15,to... (3 Replies)
Discussion started by: shekhar2010us
3 Replies

5. Shell Programming and Scripting

Create database using Backup file

Hi, I have backup file of database in my server. I want to create a that database in the same Mysql Server. How can I do that? Please send the steps to create the database using backup file? Thanks a lot, (1 Reply)
Discussion started by: aish11
1 Replies

6. Solaris

Can't create database after Oracle Database installation

I installed Oracle 10 software on Solaris 11 Express, everything was fine execpt I can't create database using dbca.rsp file. I populated file with following options. OPERATION_TYPE = "createDatabase" GDBNAME = "solaris_user.domain.com" SID = "solaris_user" TEMPLATENAME = "General... (0 Replies)
Discussion started by: solaris_user
0 Replies

7. Shell Programming and Scripting

Parsing a file with AWK

Input Group: Erecords: Copy: ADC R2: Replication volumes: Replication set: RSet 1 Replication size: 50.00GB SAN Info: 50.00GB DGC VRAID CX4-960 LUN 1040 (1040) 60,06,01,60,32,bb,21,00,84,a0,08,b1,da,ec,df,11... (2 Replies)
Discussion started by: greycells
2 Replies

8. Programming

parsing a database with java

hi all i have a databasewhich consists few fields in the form Time(starting from 0 in asc order) bytes service flags protocols e.g. 0, 0, 0, 56, 86 1, 524, 3, 6, 65 1, 624, 0, 43, 33 2, 72, 0, 43, 80 3, 26, 3, 6, 86 4, 323, 3, 1459, 95 5, 325, 3, 1459, 33 6, 225, 3, 1436, 57 now... (7 Replies)
Discussion started by: stuggler
7 Replies

9. Shell Programming and Scripting

awk and file parsing

Hi, I have a input file like this TH2TH2867Y NOW33332106Yo You Baby TH2TH3867Y NOW33332106No Way Out TH2TH9867Y NOW33332106Can't find it TJ2TJ2872N WOW33332017sure thing alas TJ2TJ3872N WOW33332017the sky rocks TJ2TJ4872N WOW33332017nothing else matters ... (4 Replies)
Discussion started by: devtakh
4 Replies

10. Shell Programming and Scripting

parsing file through awk

hi, how can i achieve this in awk CON Controllers Department R abcuser usernamedesc1 R defuser usernamedesc2 R ... (13 Replies)
Discussion started by: bbeugie
13 Replies
Login or Register to Ask a Question