The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replacing File values hgjdv Shell Programming and Scripting 4 05-31-2009 12:37 PM
sed question - replacing param values majormark Shell Programming and Scripting 3 08-05-2008 04:51 PM
MAX file size limited to 2GB mhbd UNIX for Dummies Questions & Answers 1 04-10-2008 06:59 AM
replacing a line that may have different values on different servers fed.linuxgossip Shell Programming and Scripting 3 12-31-2007 06:36 PM
replacing variable values in all files in directories newtoshell Shell Programming and Scripting 3 12-30-2005 04:11 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 4 Weeks Ago
maheudi maheudi is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 3
Replacing values in a de-limited file

Hi - I was wondering if someone can assist me with the following. Thanks in advance for your help!

I have 2 files. A dictionary file which is pipe delimited. This dictionary file is responsible for keeping track of all output files from a process. The fields I am storing in this file are
Code:
 
 filename | version | timestamp
So everytime my process runs, it produces a particular set of output files. Once my output files are produced, I need to update the dictionary file using filename as a matching key and updating the version & timestamp fields.

The second file is basically a LS -1 listing of files that my process produces (lsoutfile). So essentially, what I am trying to do is loop through the lsoutfile and for every record in the lsoutfile, search for that filename in the dictionary file. If I find a match, I need to update the version & timestamp. If I don't find a match, I need to add a new record to the dictionary file.

So for eg

Dictionary File contents

Code:
 
filename1|v1|20091020
filename2|v1|20091020
filename3|v1|20091020
lsoutfile contents

Code:
 
filename1
filename2
filename5
I run my process which is v2 and run on 20091021

Need to run code so that the dictionary file reads
Code:
 
filename1|v2|20091021
filename2|v2|20091021
filename3|v1|20091020
filename5|v2|20091021
Reading through the forums, I have realized that I will have to use some combination of sed or awk but can't exactly pinpoint

This is what I have so far :
Code:
 
for source in `cat lsout.txt`
do
 for LineFromFile in `cat Dictionary.txt`
   do
   ## lets read in the filename first of the file
   ModifiedLineFromFile=`echo $LineFromFile|sed s,"|"," ",g`
   FileName=`echo $ModifiedLineFromFile |nawk '{ print $1 }'`
   if [[ $source == $FileName ]] then
   echo "Match Found : $FileName"
  #####Need code here to update the line with the new information######   
   fi
   done
done
Thanks again for the assistance!
  #2 (permalink)  
Old 4 Weeks Ago
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Try this:

Code:
awk -F"|" -v v="v2" -v d="20091021" '
NR==FNR{a[$0]=$0;next}
a[$1]{print $1 FS v FS d;delete a[$1];next}
{print}
END{
  for(i in a){
    if(a[i])print a[i] FS v FS d
  }
}' lsoutfile dictionaryfile
  #3 (permalink)  
Old 4 Weeks Ago
maheudi maheudi is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 3
Thanks Franklin52. Do I need to run this code independently or place this as part of the existing code that I have. Also, if you have the time, would you mind breaking down the code in high level steps to explain what its doing? Thanks again! Much appreciated.
  #4 (permalink)  
Old 4 Weeks Ago
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
You can save the code in a shell script and call it with parameters like this:

Code:
./myscript V2 20091021 lsoutfile dictionaryfile
The script should look like:

Code:
#!/bin/sh

awk -F"|" -v v="$1" -v d="$2" '
NR==FNR{a[$0]=$0;next}
a[$1]{print $1 FS v FS d;delete a[$1];next}
{print}
END{
  for(i in a){
    if(a[i])print a[i] FS v FS d
  }
}' "$3" "$4"
Regards
  #5 (permalink)  
Old 4 Weeks Ago
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,511
bash
Code:
#!/bin/bash

mycode="20091021"
process="v2"
EOL=false
lsout=( $(< "lsout.txt" ) )
until $EOL
do
IFS="|" read -r a b c || EOL=true
    f=0
    for (( i=0; i<=${#lsout[*]};i++ ))    
    do 
        if [ "${lsout[$i]}" == "${a}" ] ; then
            L="$a|$b|$mycode"
            f=1
            unset lsout[$i]
            break
        fi
    done
    if [ "$f" -eq "1" ];then    
        echo "$L"
    else
        echo "$a|$b|$c"
    fi
done <"dictionary.txt"
for k in "${lsout[@]}" 
do
    echo "$k|$process|$mycode"
done
output
Code:
$ more dictionary.txt
filename1|v1|20091020
filename2|v1|20091020
filename3|v1|20091020

$ more lsout.txt
filename1
filename2
filename5

$ ./shell.sh
filename1|v1|20091021
filename2|v1|20091021
filename3|v1|20091020
filename5|v2|20091021
  #6 (permalink)  
Old 4 Weeks Ago
maheudi maheudi is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 3
Thank you very much Franklin52 and GhostDog74. Greatly appreciate you taking the time to help me out. I am coding in KSH, so I'll use Franklin52's approach...but I am sure GhostDog74's solution will be useful for others. Thanks again!!
Reply

Bookmarks

Tags
awk, grep, nawk, sed

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 On




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

Content Relevant URLs by vBSEO 3.2.0