![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | 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 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 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 Code:
filename1 filename2 filename5 Need to run code so that the dictionary file reads Code:
filename1|v2|20091021 filename2|v2|20091021 filename3|v1|20091020 filename5|v2|20091021 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 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.
|
|
||||
|
You can save the code in a shell script and call it with parameters like this:
Code:
./myscript V2 20091021 lsoutfile dictionaryfile 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"
|
|
||||
|
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
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 |
|
||||
|
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!!
|
![]() |
| Bookmarks |
| Tags |
| awk, grep, nawk, sed |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|