search and combine lines in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search and combine lines in awk
# 1  
Old 02-12-2009
search and combine lines in awk

Hi All,

I have 1 "keyword" file like this:

00-1F-FB-00-04-18
00-19-CB-8E-66-DF
00-1F-FB-00-48-9C
00-1F-FB-00-AA-4F
....

and the 2nd "details" file like this:

Wed Feb 11 00:00:02 2009
NAS-IP-Address = xxxxxxxxxxxxxxxxxx
Class = "P1-SHT-AAA01;1233704662;4886720"
Calling-Station-Id = "00-1F-FB-00-48-B5"
Acct-Status-Type = Stop
Acct-Session-Id = "0000000088730000"
Acct-Input-Octets = 276384
Acct-Output-Octets = 5445853
Acct-Input-Gigawords = 0
Acct-Output-Gigawords = 0
Acct-Input-Packets = 3036
Acct-Output-Packets = 4507
Acct-Session-Time = 6222
Acct-Terminate-Cause = NAS-Error
Framed-IP-Address = xxxxxxxxxxxxx
WiMAX-BS-ID = 000012A00452
User-Name = "xxxxxxxxxxxxxxxxx"
--
Wed Feb 11 00:00:17 2009
NAS-IP-Address = xxxxxxxxxxxxxxxxxx
Class = "P1-SHT-AAA01;1233704662;4886720"
Calling-Station-Id = "00-1F-FB-00-48-B6"
Acct-Status-Type = Stop
Acct-Session-Id = "0000000088730000"
Acct-Input-Octets = 276384
Acct-Output-Octets = 5445853
Acct-Input-Gigawords = 0
Acct-Output-Gigawords = 0
Acct-Input-Packets = 3036
Acct-Output-Packets = 4507
Acct-Session-Time = 6222
Acct-Terminate-Cause = NAS-Error
Framed-IP-Address = xxxxxxxxxxxxx
WiMAX-BS-ID = 000012A00452
User-Name = "xxxxxxxxxxxxxxxxx"
--
Wed Feb 11 00:00:17 2009
NAS-IP-Address = xxxxxxxxxxxxxxxxxx
Class = "P1-SHT-AAA01;1233704662;4886720"
Calling-Station-Id = "00-1F-FB-00-48-B7"
Acct-Status-Type = Stop
Acct-Session-Id = "0000000088730000"
Acct-Input-Octets = 276384
Acct-Output-Octets = 5445853
Acct-Input-Gigawords = 0
Acct-Output-Gigawords = 0
Acct-Input-Packets = 3036
Acct-Output-Packets = 4507
Acct-Session-Time = 6222
Acct-Terminate-Cause = NAS-Error
Framed-IP-Address = xxxxxxxxxxxxx
WiMAX-BS-ID = 000012A00452
User-Name = "xxxxxxxxxxxxxxxxx"
......

and I want to collect only the "calling-station-id" and the "username" field like below:

00-1F-FB-00-48-B5,xxxxxxxxxxxxxxxxx
00-1F-FB-00-48-B6,xxxxxxxxxxxxxxxxx
00-1F-FB-00-48-B7,xxxxxxxxxxxxxxxxx
.....

Really appreciate anyone help on this. thanks
# 2  
Old 02-12-2009
In the example the 1st file with the list of (I assume) Calling-Station-Ids will not match with any in the 2nd example file. So this is just an unlucky example or does the 1st file not really have to correspondent with any match of Calling-Station-Ids in the 2nd file?
# 3  
Old 02-12-2009
You mean in one file you will have the calling Calling-Station-Id and you want to prepare a list of Calling-Station-Id and user name for those Calling-Station-Id ???
# 4  
Old 02-12-2009
hi zaxxon

the above file2 only for example and what Iam trying to do is pulling details for each keyword from file1 using file2.

ranjit,
yes, that what I want to do if I understood you correctly.
Thanks
# 5  
Old 02-12-2009
grep -E "Calling-Station-Id|User-Name" second.file | tr "\n" "=" | grep -f first.file|cut -d"=" -f2-4
# 6  
Old 02-12-2009
With one awk command:

Code:
awk -F"\"" '
NR==FNR{a[$0];next}
$2 in a{p=$2}
p && /User-Name =/{print p "," $2;p=""}
' file1 file2

Regards
# 7  
Old 02-12-2009
hi ranjit, your script output just 1 line. Fraklin, your script working fine. let me analysis the output see if everyting fine. thanks all for the help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk with sed to combine lines and remove specific odd # pattern from line

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

awk to combine lines if fields match in lines

In the awk below, what I am attempting to do is check each line in the tab-delimeted input, which has ~20 lines in it, for a keyword SVTYPE=Fusion. If the keyword is found I am splitting $3 using the . (dot) and reading the portion before and after the dot in an array a. If it does have that... (12 Replies)
Discussion started by: cmccabe
12 Replies

4. Shell Programming and Scripting

awk to combine matching lines in file

I am trying to combine all matching lines in the tab-delimited using awk. The below runs but no output results. Thank you :). input chrX 110925349 110925532 ALG13 chrX 110925349 110925532 ALG13 chrX 110925349 110925532 ALG13 chrX 47433390 47433999 SYN1... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

(n)awk: print regex search output lines in one line

Hello. I have been looking high and low for the solution for this. I seems there should be a simple answer, but alas. I have a big xml file, and I need to extract certain information from specific items. The information I need can be found between a specific set of tags. let's call them... (2 Replies)
Discussion started by: Tobias-Reiper
2 Replies

6. Shell Programming and Scripting

Awk: Combine multiple lines based on number of fields

If a file has following kind of data, comma delimited 1,2,3,4 1 1 1,2,3,4 1,2 2 2,3,4 My required output must have only 4 columns with comma delimited 1,2,3,4 111,2,3,4 1,222,3,4 I have tried many awk command using ORS="" but couldnt progress (10 Replies)
Discussion started by: mdkm
10 Replies

7. Shell Programming and Scripting

Combine multiple unique lines from event log text file into one line, use PERL or AWK?

I can't decide if I should use AWK or PERL after pouring over these forums for hours today I decided I'd post something and see if I couldn't get some advice. I've got a text file full of hundreds of events in this format: Record Number : 1 Records in Seq : ... (3 Replies)
Discussion started by: Mayday22
3 Replies

8. Shell Programming and Scripting

Awk to search for sting in lines and then email.

I'm trying to use awk in a script to search for a string number in a plist file. basically its a plist that records the bandwidth usage, and if the number is greater than 3GB which is equals 3221225472 bytes, it will send me an alert by email. here is the file: =<?xml version="1.0"... (4 Replies)
Discussion started by: ivaldiz
4 Replies

9. Shell Programming and Scripting

awk how to search strings within a file from two different lines

Hi, i would really appreciate any help anyone can give with the following info. Thanks in advance. I need to run a search on a file that contains thousands of trades, each trade is added into the file in blocks of 25 lines. i know the search has to take place between a time stamp specified... (4 Replies)
Discussion started by: sp3arsy
4 Replies

10. UNIX for Dummies Questions & Answers

SImple HELP! how to combine two lines together using sed or awk..

hi..im new to UNIX... ok i have this information in the normal shell... there are 2 lines display like this: h@hotmail.com k@hotmail.com i want it to display like this with a space betweem them h@hotmail.com k@hotmail.com the information is stored in a text file.... anyone... (10 Replies)
Discussion started by: forevercalz
10 Replies
Login or Register to Ask a Question