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



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-24-2008
Registered User
 

Join Date: Mar 2008
Posts: 28
compare 2 files and replace

Hi,

I have a file which contains names with counts for eg:

0622 0031 JOHN MAX 20080622003104. STAT 1.
0622 0031 BILL MAX 20080622003104. STAT 7.

and I have an exception file containing.

BILL

Can anyone help to write a script using this exception files to replace the STAT *(could be any number) to STAT 0 for the names that are in this list

the output should be and the format remains the same

0622 0031 JOHN MAX 20080622003104. STAT 0.

any comments or help appriciated.

I got a script which can do this, but it changes the format.
#!/usr/bin/ksh
nawk '
FNR==NR {ex[$1]; next}
{ $9 = ($1 in ex) ? "0." : $2; print }
' a2 a1 > a3

any help appriciated.
thanks

Last edited by antointoronto; 06-25-2008 at 01:32 AM..
Sponsored Links
  #2 (permalink)  
Old 06-24-2008
radoulov's Avatar
--
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 3,212
Use nawk or /usr/xpg4/bin/awk on Solaris:


Code:
awk 'NR==FNR{_[$1];next}$NF=$3 in _?0".":$NF' exceptions names

  #3 (permalink)  
Old 06-25-2008
Registered User
 

Join Date: Mar 2008
Posts: 28
Quote:
Originally Posted by radoulov View Post
Use nawk or /usr/xpg4/bin/awk on Solaris:


Code:
awk 'NR==FNR{_[$1];next}$NF=$3 in _?0".":$NF' exceptions names

sorry I made a mistake in the output. the output should be

0622 0031 JOHN MAX 20080622003104. STAT 1.
0622 0031 BILL MAX 20080622003104. STAT 0.

for BILL which is found in the exception file the STAT should change from STAT 7. to STAT 0.

also the length of the line should not change during the replace..

your help is appriciated

thanks Antony
  #4 (permalink)  
Old 06-25-2008
Registered User
 

Join Date: Oct 2007
Posts: 75
using grep and sed

You can also do this using grep and sed


Code:
grep -f exp.txt inp.txt | sed 's/STAT [0-9][0-9]*/STAT 0/g'

exp.txt is the exception file.

Regards,
Chella
  #5 (permalink)  
Old 06-25-2008
radoulov's Avatar
--
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 3,212
Quote:
Originally Posted by antointoronto View Post
sorry I made a mistake in the output. the output should be

0622 0031 JOHN MAX 20080622003104. STAT 1.
0622 0031 BILL MAX 20080622003104. STAT 0.

for BILL which is found in the exception file the STAT should change from STAT 7. to STAT 0.

also the length of the line should not change during the replace..
[...]
Since in your first post ti was JOHN and not BILL in the exception file the code works as expected ...
1. If an entry in the names file matches an entry in the exception file should the stat change only if the current value is 7?
2. You mentioned the record length and I suppose it's because of the recalculation of the current record; could you post sample of your input data using the code tags.
  #6 (permalink)  
Old 06-26-2008
Registered User
 

Join Date: Mar 2008
Posts: 28
Quote:
Originally Posted by chella View Post
You can also do this using grep and sed


Code:
grep -f exp.txt inp.txt | sed 's/STAT [0-9][0-9]*/STAT 0/g'

exp.txt is the exception file.

Regards,
Chella
This worked except the output printed only the ones that were found on exception file..all the records were not printed.
  #7 (permalink)  
Old 06-26-2008
Registered User
 

Join Date: Oct 2007
Posts: 75
using grep and sed with a while loop

Try this,


Code:
#! /bin/ksh
cp inp.txt tmp_1.txt
while read line
do
        sed "/$line/s/STAT [0-9][0-9]*/STAT 0/g" tmp_1.txt > tmp_2.txt
        cp tmp_2.txt tmp_1.txt
done < exp.txt
cat tmp_2.txt
rm tmp_2.txt tmp_1.txt

inp.txt


Code:
0622 0031 JOHN MAX 20080622003104. STAT 1
0622 0031 PETER MAX 20080622003104. STAT 3
0622 0031 BILL MAX 20080622003104. STAT 7
0622 0031 MARY MAX 20080622003104. STAT 2

exp.txt


Code:
BILL
PETER

Regards,
Chella
Sponsored Links
Closed Thread

Bookmarks

Tags
solaris

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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare files antointoronto Shell Programming and Scripting 8 06-24-2008 06:13 PM
compare files prashanth.spl Shell Programming and Scripting 0 06-18-2008 05:22 PM
Compare & replace contents within a file kaustubh137 Shell Programming and Scripting 2 05-27-2008 07:56 AM
compare files spt Shell Programming and Scripting 1 05-09-2008 11:57 AM
compare files ingunix UNIX for Dummies Questions & Answers 3 05-24-2001 12:44 PM



All times are GMT -4. The time now is 06:32 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-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0