?
Solved

Unix Script to read files and manipulate the values

Posted on 2009-04-02
9
Medium Priority
?
353 Views
Last Modified: 2013-12-26
I have three files:-
a) yest-unr = list of items saved on yesterday
b) tod-unr = list of items saved today
c) yest-num = list of number of days for items listed on file `yest-unr` correspondignly

I want to write a scipt to read an item line by line for file `tod-unr` and search it on `yest-unr` list
and if it finds there... it should increment the corresponding number to it on `yest-num` file.
and finnaly the tod-unr file will replace the yest-unr file and the yest-unr should be deleted.

The aim of the task is to count the number of days an item occured on `tod-unr` file...
below is the code i tried but running with error... on my script.... c = count read from yest-num file.., nc= c+1 for the incremented, tod-unr = list of items for today, yest-unr= list of items from yesterday, yest-num= file contains c's of yest-unr
#!/bin/sh
c=$0
nc=$0
for i in `(cat tod-unr)`
do
    c= expr $c+1
    for j in `(cat yest-unr)`
        do
          if [$i == $j]; then
              yc=echo `sed -n '$c p' yest-num`
              typeset -i $yc
              nc= expr $yc+1
              echo $nc >> yest-num-new
          else
              nc=$1
              echo $nc >> yest-num-new
          fi
         done
done
mv tod-unr yest-unr
mv yest-num-new yest-num

Open in new window

0
Comment
Question by:mystockid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 24052211


Why do you keep the count separate from the items?
Maybe this could be a better design:

awk 'BEGIN {while ((getline line < "tod_unr") > 0) t[line]=0;}
{$1 !~ "#"
  n=$2; for (f in t) if (f == $1) {n+=1; t[f]=1}
  print $1, n;}
  END {for (f in t) if (t[f] == 0) print f,1;}
' yest_unr|sort >yest_new_unr
echo "### Last updated: `date`" >yest_unr
cat yest_new_unr >>yest_unr
rm yest_new_unr

Open in new window

0
 

Author Comment

by:mystockid
ID: 24053574
I tried your code and still running with syntax erro.

bash-2.05# ./ccm-sc-v3
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 2
awk: illegal statement near line 2
./ccm-sc-v3: yest-unr: execute permission denied
#!/bin/sh
awk 'BEGIN {while ((getline line < "tod-unr") > 0) t [line]=0;}
{$1 !~ "#"
  n=$2; for (f in t) if (f == $1) {n+=1; t[f]=1}
  print $1, n;}
  END {for (f in t) if (t[f] == 0) print f,1;}'
yest-unr | sort > yest-new-unr
echo "### Last Updated: `date`" > yest-unr
cat yest-new-unr >> yest-unr
rm yest-new-unr
 
 
 
bash-2.05# ./ccm-sc-v3
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 2
awk: illegal statement near line 2
./ccm-sc-v3: yest-unr: execute permission denied

Open in new window

0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 24061710
What os are you using?
Use the "bash" shell (not sh), works for me:
 
 
 
 

#!/bin/bash
##
cat - <<EOF >yest_unr
aaaa 1
tttt 1
dddd 1
ssss 1
EOF
cat - <<EOF >tod_unr
tttt
dddd
uuuu
EOF
 
echo "+-----------------------------"
cat yest_unr|grep -v '#'|\
awk  'BEGIN {while ((getline line < "tod_unr") > 0) t[line]=0;}
{n=$2; for (f in t) if ($1 == f) {n+=1; t[f]=1;}
 print $1, n;}
 END {for (f in t) if (t[f] == 0) print f,1;}
' |sort >yest_new_unr
echo "### Last updated: `date`" >yest_unr
cat yest_new_unr >>yest_unr
cat yest_unr
rm yest_new_unr
##
## Execution:
$ ./o0
+-----------------------------
### Last updated: Fri Apr  3 12:25:25 EDT 2009
aaaa 1
dddd 2
ssss 1
tttt 2
uuuu 1

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mystockid
ID: 24063463
it is solaris OS 5.9 Generic_118558-14 sun4u sparc SUNW,Ultra-60
...

thanks for your time the expected output is:-
tttt 2
ddd 2
uuuu 1

0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 24071235

If you only need the count of the "todays" files, what happens to the previous count?

Example:

## tod_unr
tttt
dddd
uuuu

## yest_unr
aaaa 1
tttt 1
dddd 1
ssss 1

If you discard "aaaa 1" and "ssss 1" you loose the count and if next day you receive these items the count would begin at 1 (one) again.

The modified script to do this would be:

#!/bin/bash
##
cat - <<EOF >yest_unr
aaaa 1
tttt 1
dddd 1
ssss 1
EOF
cat - <<EOF >tod_unr
tttt
dddd
uuuu
EOF
 
echo "+-----------------------------"
cat yest_unr|grep -v '#'|\
awk  'BEGIN {while ((getline line < "tod_unr") > 0) t[line]=0;}
{n=$2; for (f in t) if ($1 == f) {n+=1; t[f]=1; print $1, n;}}
 END {for (f in t) if (t[f] == 0) print f,1;}
' |sort >yest_new_unr
echo "### Last updated: `date`" >yest_unr
cat yest_new_unr >>yest_unr
cat yest_unr
rm yest_new_unr
exit
##
## Execution:
$ ./o0
+-----------------------------
### Last updated: Sun Apr  5 08:14:11 EDT 2009
dddd 2
tttt 2
uuuu 1

Open in new window

0
 

Author Comment

by:mystockid
ID: 24086835

I still running with syntax error. I tried to filter the error but couldn't. Here is the output.

Thanks,
Alem
bash-2.05# more tod_unr
tttt
dddd
uuuu
bash-2.05# more yest_unr
aaaa 1
tttt 1
dddd 1
ssss 1
bash-2.05# more ccm-sc-v5
#!/bin/bash
echo "+-----------------------------"
cat yest_unr | grep -v '#'|\
awk  'BEGIN {while ((getline line < "tod_unr") > 0) t[line]=0;}
{n=$2; for (f in t) if ($1 == f) {n+=1; t[f]=1; print $1, n;}}
 END {for (f in t) if (t[f] == 0) print f,1;}
' | sort >yest_new_unr
echo "### Last updated: `date`" >yest_unr
cat yest_new_unr >>yest_unr
cat yest_unr
rm yest_new_unr
exit
##
## Execution:
bash-2.05# ./ccm-sc-v5
+-----------------------------
awk: syntax error near line 1
awk: illegal statement near line 1
### Last updated: Tue Apr  7 09:06:08 EDT 2009
bash-2.05#

Open in new window

0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 24090130

I copied and pasted your code, tested with bash 3.2 and it works:

-bash-3.2$ cat tod_unr
tttt
dddd
uuuu
-bash-3.2$ cat yest_unr
aaaa 1
tttt 1
dddd 1
ssss 1
-bash-3.2$ cat ccm-sc-v5
#!/bin/bash
echo "+-----------------------------"
cat yest_unr | grep -v '#'|\
awk  'BEGIN {while ((getline line < "tod_unr") > 0) t[line]=0;}
{n=$2; for (f in t) if ($1 == f) {n+=1; t[f]=1; print $1, n;}}
 END {for (f in t) if (t[f] == 0) print f,1;}
' | sort >yest_new_unr
echo "### Last updated: Tue Apr  7 13:57:04 EDT 2009" >yest_unr
cat yest_new_unr >>yest_unr
cat yest_unr
rm yest_new_unr
exit
##
## Execution:
-bash-3.2$ ./ccm-sc-v5
+-----------------------------
### Last updated: Tue Apr  7 13:57:04 EDT 2009
dddd 2
tttt 2
uuuu 1
-bash-3.2$ 

Open in new window

0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 24090150

PS: You could maybe try gawk or nawk instead?

0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 2000 total points
ID: 24090329
-- OR -- in ksh without awk:
 

#!/bin/ksh
while read fn
do
  line=(`grep $fn yest_unr`)
  if [ ! -z ${line[0]} ]; then
    echo "${line[0]} `expr ${line[1]} + 1`"
  else
    echo "$fn 1"
  fi
done < tod_unr|sort >tod_new_unr
mv tod_new_unr yest_unr

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question