The UNIX Forums  



Go Back   The UNIX Forums > Top Forums > Shell Programming and Scripting
Home Forums Register Rules & FAQDonate Members List Search Today's Posts Mark Forums Read

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

Reply
 
Submit Tools Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 1 Week Ago
Registered User
 
Join Date: May 2008
Location: Sheffield
Posts: 10
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Help with scripting

I have 2 files with a common parm - Jobname

File 1
0507 1202 JOBA
0507 1302 JOBB
0507 1452 JOBC
0507 1552 JOBA
0507 1553 JOBA

File2

JOBA abcdefg server4
JOBB defghij server22
JOBC vwxyz12 server55

I would like to take each line from File1 and match the jobname with the jobname in File 2 and produce File 3 as

0507 1202 JOBA abcdefg server4
0507 1302 JOBB defghij server22
0507 1452 JOBC vwxyz12 server55
0507 1552 JOBA abcdefg server4
0507 1553 JOBA abcdefg server4

Could anyone help please, I'm new to scripting.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 1 Week Ago
Registered User
 
Join Date: Jul 2006
Posts: 132
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Code:
nawk 'NR == FNR{rec[$1] = $2" " $3; next}

{ print $0 " " rec[$3]}

' 64491_merge1.d 64491_merge.d
output

Code:
0507 1202 JOBA abcdefg server4
0507 1302 JOBB defghij server22
0507 1452 JOBC vwxyz12 server55
0507 1552 JOBA abcdefg server4
0507 1553 JOBA abcdefg server4
Reply With Quote
Forum Sponsor
  #3 (permalink)  
Old 1 Week Ago
Registered User
 
Join Date: May 2008
Location: Sheffield
Posts: 10
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Talking

Brilliant Thanks very much indeed!
Reply With Quote
  #4 (permalink)  
Old 1 Week Ago
radoulov's Avatar
addict
 
Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,018
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Another one:
(use nawk or /usr/xpg4/bin/awk on Solaris)

Code:
awk 'NR==FNR{x[$1]=$0;next}$NF=x[$NF]' file2 file1
Reply With Quote
  #5 (permalink)  
Old 4 Days Ago
Registered User
 
Join Date: May 2008
Location: Sheffield
Posts: 10
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Having tested this on the simple files I provided it appears it doesn't work on the more complexed files. The first file has 6 field separated by blanks

File1

LVTS4687 LVTSHPLON ltreb_shu1l110 LVTS4687 LVTSHPLON shu1l110
LVTS1457 LVTSHP sy_unix_live_rooma_onsite_35_35 LVTS1457 LVTSHP syhp10
LVTS5035 LVTSHP sy_unix_live_dmz_duplex_15_35 LVTS5035 LVTSHP srublae03-mgt
LVTS1786 LVTSHPLON lmurb_shu1l282 LVTS1786 LVTSHPLON shu1l282-sy
LVTS5431 LVTSHPLON lmurb_shu1l282 LVTS5431 LVTSHPLON shu1l282-sy
LVTS5750 LVTSHP nw_unix_live_dmz_duplex_35_35 LVTS5750 LVTSHP NRUCCAE09-MGT
LVTS5430 LVTSHP nw_unix_live_dmz_duplex_15_35 LVTS5430 LVTSHP nwhp3
LVTS4021 LVTSHP nw_unix_live_phase4_duplex_35_90 LVTS4021 LVTSHP nhu1u282

The second file has 5 fields
File 2
0508 0601 LVTS4687 58 1
0509 0108 LVTS4687 58 1
0509 0108 LVTS4687 58 1
0509 0108 LVTS4687 58 1
0508 2206 LVTS5035 98 1
0508 2206 LVTS5035 98 1
0508 2206 LVTS5035 98 1
0508 2206 LVTS5430 98 1
0508 2206 LVTS5430 98 1
0508 2206 LVTS5430 98 1
0508 2206 LVTS5431 98 1
0508 2206 LVTS5431 98 1
0508 2206 LVTS5431 98 1
0506 0920 LVTS5750 141 3
0506 0920 LVTS5750 141 3
0506 0920 LVTS5750 141 3
0506 0920 LVTS5750 141 3
0506 0920 LVTS5750 141 3
0506 0920 LVTS5750 141 3


and the result would need to look like

File3
0508 0601 LVTS4687 58 1 ltreb_shu1l110 shu1l110
0509 0108 LVTS4687 58 1 ltreb_shu1l110 shu1l110
0509 0108 LVTS4687 58 1 ltreb_shu1l110 shu1l110
0509 0108 LVTS4687 58 1 ltreb_shu1l110 shu1l110
0508 2206 LVTS5035 98 1 sy_unix_live_dmz_duplex_15_35 srublae03-mgt
0508 2206 LVTS5035 98 1 sy_unix_live_dmz_duplex_15_35 srublae03-mgt
0508 2206 LVTS5035 98 1 sy_unix_live_dmz_duplex_15_35 srublae03-mgt
0508 2206 LVTS5430 98 1 nw_unix_live_dmz_duplex_15_35 nwhp3
0508 2206 LVTS5430 98 1 nw_unix_live_dmz_duplex_15_35 nwhp3
0508 2206 LVTS5430 98 1 nw_unix_live_dmz_duplex_15_35 nwhp3
0508 2206 LVTS5431 98 1 lmurb_shu1l282 shu1l282-sy
0508 2206 LVTS5431 98 1 lmurb_shu1l282 shu1l282-sy
0508 2206 LVTS5431 98 1 lmurb_shu1l282 shu1l282-sy
0506 0920 LVTS5750 141 3 nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT
0506 0920 LVTS5750 141 3 nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT
0506 0920 LVTS5750 141 3 nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT
0506 0920 LVTS5750 141 3 nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT
0506 0920 LVTS5750 141 3 nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT
0506 0920 LVTS5750 141 3 nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT


this file comprises of field 1 2 3 4 5 of file2 and field 3 and 6 of file1
Reply With Quote
  #6 (permalink)  
Old 4 Days Ago
radoulov's Avatar
addict
 
Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,018
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
It seems quite easy, did you try to adapt the code yourself?

Code:
awk 'NR==FNR{_[$1]=$3 FS $6;next}$NF=$NF _[$3]' file1 file2>file3
Reply With Quote
  #7 (permalink)  
Old 4 Days Ago
Registered User
 
Join Date: Jul 2006
Posts: 132
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
HTML Code:
Now your format is different then earlier
Reply With Quote
  #8 (permalink)  
Old 2 Days Ago
Registered User
 
Join Date: May 2008
Location: Sheffield
Posts: 10
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
The output from this awk command is
0508 0601 LVTS4687 58 1ltreb_shu1l110 shu1l110
0509 0108 LVTS4687 58 1ltreb_shu1l110 shu1l110
0509 0108 LVTS4687 58 1ltreb_shu1l110 shu1l110
0509 0108 LVTS4687 58 1ltreb_shu1l110 shu1l110
0508 2206 LVTS5035 98 1sy_unix_live_dmz_duplex_15_35 srublae03-mgt
0508 2206 LVTS5035 98 1sy_unix_live_dmz_duplex_15_35 srublae03-mgt
0508 2206 LVTS5035 98 1sy_unix_live_dmz_duplex_15_35 srublae03-mgt
0508 2206 LVTS5430 98 1
0508 2206 LVTS5430 98 1
0508 2206 LVTS5430 98 1
0508 2206 LVTS5431 98 1lmurb_shu1l282 shu1l282-sy
0508 2206 LVTS5431 98 1lmurb_shu1l282 shu1l282-sy
0508 2206 LVTS5431 98 1lmurb_shu1l282 shu1l282-sy
0506 0920 LVTS5750 41 3nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT
0506 0920 LVTS5750 41 3nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT
0506 0920 LVTS5750 41 3nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT
0506 0920 LVTS5750 41 3nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT
0506 0920 LVTS5750 41 3nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT
0506 0920 LVTS5750 41 3nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT

Which is almost there apart from there being no space between parm5 and 6 and also parm 4 is reduced to the last 2 digits. Apologies for my ignorance on this but I have never used "awk" before. The file should look like this

0508 0601 LVTS4687 58 1 ltreb_shu1l110 shu1l110
0509 0108 LVTS4687 58 1 ltreb_shu1l110 shu1l110
0509 0108 LVTS4687 58 1 ltreb_shu1l110 shu1l110
0509 0108 LVTS4687 58 1 ltreb_shu1l110 shu1l110
0508 2206 LVTS5035 98 1 sy_unix_live_dmz_duplex_15_35 srublae03-mgt
0508 2206 LVTS5035 98 1 sy_unix_live_dmz_duplex_15_35 srublae03-mgt
0508 2206 LVTS5035 98 1 sy_unix_live_dmz_duplex_15_35 srublae03-mgt
0508 2206 LVTS5430 98 1
0508 2206 LVTS5430 98 1
0508 2206 LVTS5430 98 1
0508 2206 LVTS5431 98 1 lmurb_shu1l282 shu1l282-sy
0508 2206 LVTS5431 98 1 lmurb_shu1l282 shu1l282-sy
0508 2206 LVTS5431 98 1 lmurb_shu1l282 shu1l282-sy
0506 0920 LVTS5750 141 3 nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT
0506 0920 LVTS5750 141 3 nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT
0506 0920 LVTS5750 141 3 nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT
0506 0920 LVTS5750 141 3 nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT
0506 0920 LVTS5750 141 3 nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT
0506 0920 LVTS5750 141 3 nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT

Is it possible to format the file so each parm starts in a specific column for example

Parm1 in Col1
Parm2 in Col6
Parm3 in Col12
Parm4 in Col22
Parm5 in Col27
Parm6 in Col30
Parm7 in Col65
Reply With Quote
  #9 (permalink)  
Old 2 Days Ago
radoulov's Avatar
addict
 
Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,018
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
The first problem (the missing space) is trivial:

Code:
awk 'NR==FNR{_[$1]=$3 FS $6;next}$NF=$NF FS _[$3]' file1 file2
Quote:
[...]and also parm 4 is reduced to the last 2 digits
Could you be more specific?

Quote:
Is it possible to format the file so each parm starts in a specific column for example

Parm1 in Col1
Parm2 in Col6
Parm3 in Col12
Parm4 in Col22
Parm5 in Col27
Parm6 in Col30
Parm7 in Col65
Provide an example, please.
Reply With Quote
  #10 (permalink)  
Old 2 Days Ago
era era is offline
Herder of Useless Cats
 
Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 1,642
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
That sounds like a simple printf specifier.

Code:
awk whatever ... |
awk '{ printf ("%5s%6s%9s%5s%3s%35s%s\n", $1, $2, $3, $4, $5, $6, $7) }'
(Sorry, might have miscalculated the width of a few fields. Modifying the earlier script to do this is itself is probably not too hard either.)

I don't see anything which would be reducing $6 to just two characters.
Reply With Quote
  #11 (permalink)  
Old 2 Days Ago
Registered User
 
Join Date: May 2008
Location: Sheffield
Posts: 10
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
The original file2 had the following

0508 2206 LVTS5431 98 1
0506 0920 LVTS5750 141 3

but the output to file3 shows
0506 0920 LVTS5750 41 3nw_unix_live_dmz_duplex_35_35 NRUCCAE09-MGT

where the 141 is truncated to 41

the final output I would like to see so that each parm is alligened in the specific columns
Starting in column
1 6 12 22 27 30 65
0508 0601 LVTS4687 58 1 ltreb_shu1l110 shu1l110

Last edited by Northerner : 2 Days Ago at 07:51 AM.
Reply With Quote
  #12 (permalink)  
Old 2 Days Ago
era era is offline
Herder of Useless Cats
 
Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 1,642
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Whatever is causing that is not happening here, and it's unlear why it would be caused by Radoulov's script; it doesn't alter $6. Might your input file have been corrupted at some point?
Reply With Quote
  #13 (permalink)  
Old 2 Days Ago
Registered User
 
Join Date: May 2008
Location: Sheffield
Posts: 10
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Checked the file and you are correct the input file is wrong, so I think with the help of Radoulov and Era I've got there, many thanks indeed.

Last edited by Northerner : 2 Days Ago at 08:40 AM.
Reply With Quote
  #14 (permalink)  
Old 2 Days Ago
era era is offline
Herder of Useless Cats
 
Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 1,642
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
It's not showing as aligned any particular way here. Maybe if you post an example with code tags we can see better what you mean. (This regular HTML edit box will squeeze all spaces down to one.)
Reply With Quote
  #15 (permalink)  
Old 2 Days Ago
Registered User
 
Join Date: May 2008
Location: Sheffield
Posts: 10
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Its ok I just left aligned the printf and all looks good

awk '{ printf ("%-5s%-6s%-9s%-5s%-3s%-35s%-14s\n", $1, $2, $3, $4, $5, $6, $7) }'
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripting dreams5617 SUN Solaris 1 07-06-2006 01:53 PM
difference between AIX shell scripting and Unix shell scripting. haroonec Shell Programming and Scripting 2 04-12-2006 07:12 AM
scripting guru's pls help me with scripting on AIX thatiprashant Shell Programming and Scripting 1 01-20-2006 05:58 PM
HELP! Need HELP scripting! adawg1283 Shell Programming and Scripting 7 09-29-2004 02:48 PM
scripting BigTool4u2 Shell Programming and Scripting 1 11-21-2002 11:20 AM


web tracker

All times are GMT -5. The time now is 12:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
UNIX Forum Content Copyright ©1993-2008 SilkRoad Asia All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93