Need help in AWK;Search String and rearrange columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in AWK;Search String and rearrange columns
# 1  
Old 04-05-2007
Need help in AWK;Search String and rearrange columns

Hi AWK Experts,

file1.txt contains:
29b11b820ddcc:-|OHad.perWrk|spn_id=AH111|spn_ordtyp=MY_REQ|msg_typ=ah.ntf.out|spn_ordid=928176|spn_nid=3|msg_strt=1 175615334703|msg_que=oput|diff=371|17:48:55,074|17:48:55,084|10

file2.txt contains:
29b11b820ddcc:-|spn_id=QA999|spn_ordid=98976098|spn_ordtyp=MY_RES|OHad.perWrk|msg_strt=11756153398760|diff=1571|msg _typ=ah.out.res|msg_que=res_oput|17:48:55,074|17:48:55,084|10


The columns in the input record that match below string must be selected and printed out in the same order as given below.
$1 -> OHad
$2 -> spn_id
$3 -> spn_ordid
$4 -> spn_ordtyp
$5 -> msg_typ
$6 -> spn_nid
$7 -> msg_que
$8 -> diff
$9 -> HH:MI:SS,sss
$10 -> 9999 (i.e., number of any length)

Hence the targetfile.txt must contain:
OHad.perWrk|spn_id=AH111|spn_ordid=928176|spn_ordtyp=MY_REQ|msg_typ=ah.ntf.out|spn_nid=3|msg_que=opu t|diff=371|17:48:55,074|17:48:55,084|1000
OHad.perWrk|spn_id=QA999|spn_ordid=98976098|spn_ordtyp=MY_RES|msg_typ=ah.out.res||msg_que=res_oput|d iff=1571|17:48:55,074|17:48:55,084|990810

Please note that [msg_strt] column is not required and [spn_nid] is required but is missing in file2.txt hence a blank is acceptable.

Could you please help me in getting the concept or a working prototype using AWK or any better tool that runs in shell script.

with best regrads,
Spring-Buck
# 2  
Old 04-05-2007
Spring_Buck,
Based on what you wrote and the two files sample you gave, I have a
solution assuming there is only one record per file.

You must run the shell twice, separately for each of the one record file.

I also noticed that your "targetfile.txt" does not have the first pipe "|" at the
beginning of each record -- I am putting it in my solution.

Before you run the shell, create these two files:

1) Create one file "egrep_file" as follows:
OHad
spn_id
spn_ordid
spn_ordtyp
msg_typ
spn_nid
msg_que
diff
..:..:..,
^[0-9][0-9]*$

2) Create one file "sed_file" as follows:
s/\(OHad\)/01\1/
s/\(spn_id\)/02\1/
s/\(spn_ordid\)/03\1/
s/\(spn_ordtyp\)/04\1/
s/\(msg_typ\)/05\1/
s/\(spn_nid\)/06\1/
s/\(msg_que\)/07\1/
s/\(diff\)/08\1/
s/\(..:..:..,\)/09\1/
s/^\([0-9][0-9]*\)$/10\1/

Then create a shell with the following commands:
## Create another file with one field per line without pipes "|":
tr '|' '\n' < input_file > $$one_col_file

## Using "egrep_file", create a file with the wanted target output:
egrep -f egrep_file $$one_col_file > $$wanted_target_file

## Using "sed_file", create a file with keys prefixed to be sorted:
sed -f sed_file $$wanted_target_file | sort > $$sort_file

## Remove the sort keys:
sed 's/^|../|/' $$sort_file > $$no_keys_file

## Create the final file with one record:
paste -d'\0' -s $$no_keys_file > FINAL_file

Let me know if it does what you want.

Last edited by Shell_Life; 04-05-2007 at 12:33 PM..
# 3  
Old 04-05-2007
something to start with:

nawk -f spring.awk file1.txt file2.txt fileN.txt

spring.awk:
Code:
BEGIN {
  FS=OFS="|"

  FLD_regex="^OHad" FS "^spn_id=" FS "^spn_ordid=" FS "^spn_ordtyp=" FS "^msg_tp=" FS "^spn_nid=" FS "^msg_que=" FS "^diff=" FS "-3" FS "-2" FS "0"

  colN=split(FLD_regex, colA, FS)
}

{
  for(i=1; i <= NF; i++)
     for(cols=1; cols <= colN; cols++) {
        if ( colA[cols] ~ "^-*[0-9][0-9]*$" ) {
           fld=int( colA[cols])
           outputA[cols] = (fld > 0 ) ? $fld : $(NF + fld)
        }
        else if ( $i ~ colA[cols] )
                outputA[cols] = $i
     }

  for(j=1; j<= colN; j++)
    printf("%s%s", (j in outputA) ? outputA[j] : "", (j != colN) ? FS : "\n")
  split("", outputA)

}


Last edited by vgersh99; 04-05-2007 at 01:44 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rearrange a file (2000 base64 strings in 1 row into 1 string by rows)

I have 1 row which contains abouts 20000 base64 string. e.g: /p4bdllBS8qcvW/69GUYej8nEv6gwt7UAYl0g==WZdjwTUQX9UEKsT/zWaZdQ==uI would like rearrange this file by base64 strings. So the output should be this ( 1 string in 1 row): 69GUYej8nEv6gwt7UAYl0g== WZdjwTUQX9UEKsT/zWaZdQ==How could I do... (4 Replies)
Discussion started by: freeroute
4 Replies

2. UNIX for Dummies Questions & Answers

Transpose matrix, and rearrange columns common with another file

This is my first post, I apologize if I have broken rules. Some assistance with the following will be very helpful. I have a couple of files, both should ultimately have common columns only, arranged in the same order. This file needs to be transposed, to bring the rows to columns ... (2 Replies)
Discussion started by: abh.kumar
2 Replies

3. Shell Programming and Scripting

Rearrange Lines with awk

I need to rearrange the lines in the input file in the example below: Input: LG1 R500 A-170 F1:81 F1:22 F2:32 F1:71 LG1 R700 A-203 F2:17 E2:18 LG1 R700 B-224 E1:9 LG2 R500 C-235 E2:9 F2:17 Output: LG1 R500 A-170 F1:81 LG1 R500 A-170 F1:22 LG1 R500 A-170 F2:32 LG1 R500 A-170... (2 Replies)
Discussion started by: aydj
2 Replies

4. Shell Programming and Scripting

Using awk to rearrange fields

Hi, I am required to arrange columns of a file i.e make the 15th column into the 1st column. I am doing awk 'begin {fs=ofs=","} {print $15,$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14}' ad.data>ad.csv the problem is that column 15 gets to column 1 but it is not comma separated with the... (10 Replies)
Discussion started by: seddoubt
10 Replies

5. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

6. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

7. Shell Programming and Scripting

awk help to search columns in two files

Hello, I'm trying to compare multiple columns between two files. I would like to use columns 1,2 from file1 and search file2 in columns 2,3 for any matches. If they match, then print columns 2,3 from file2. file1 11:22:33:44:55:66|2010-07-25 12:30|xyz 22:22:22:22:22:22|2010-07-25... (5 Replies)
Discussion started by: pinseeker
5 Replies

8. UNIX for Dummies Questions & Answers

Rearrange columns and rows with awk

Hello, I have the following problem I have two columns with numbers arranged as follows: x1 y1 x2 y2 .... .... x250 y250 Now I need them arranged as follows: "string a" x1 y1 x1 y2 "string b" "string a" x1 y2 x2 y2 (3 Replies)
Discussion started by: Tom46
3 Replies

9. Shell Programming and Scripting

awk/sed search lines in file1 matching columns in file2

Hi All, as you can see I'm pretty new to this board. :D I'm struggling around with small script to search a few fields in another file. Basically I have file1 looking like this: 15:38:28 sz:10001 pr:14.16 15:38:28 sz:10002 pr:18.41 15:38:29 sz:10003 pr:19.28 15:38:30 sz:10004... (1 Reply)
Discussion started by: floripoint
1 Replies

10. Shell Programming and Scripting

Need help with a script to rearrange columns

I have a file that is semi-colon delimited and the column headers are always the same but the column number is totally random each time this file is generated. I don't have the skills to make a script for this so maybe someone can help. I would like to be able to take this file which has over... (11 Replies)
Discussion started by: n3al10
11 Replies
Login or Register to Ask a Question