Solved

listing unique file prefixes in a directory Linux

Posted on 2016-09-28
1
72 Views
Last Modified: 2016-10-24
Hi guys,
I'm trying to achieve the following.
I have a folder with the following path: /home/service
in this folder I'll have many files all with the same format
user1.node1
user1.node2
user2.node4
user1.node3

and etc and the order can be randomized each time.
each of those files has a simple integer in them. and the prefix (user#) corresponds to a certain in user a DB.
basically what I need to do, is to read the current integer from the DB for the selected user (I already have the command for that, it's tested and working) and then. to add to it ALL the integers of files corresponding to that user and write the new total (meaning current + sum of all integers of said user) back into the DB (The writing is done by calling a python script that accepts the user name and the total as arguments)

how would I go about achieving the following?

The script I have at the moment isn't working so well.

files=$(find /home/service/ -type f |grep -v "bash" | sed 's#.*/##')
for file in $files
do
  	user=$(echo $file | awk -F'[.]' '{print $1}')
        total=0
        curr_traffic=$(echo "select traffic from campaigns.Campaigns where UserName='$user';" | mysql -N -u ##### -p#####)
        let total=$total+$curr_traffic
        echo "pre DB write total is $total"
        ufiles=$(find /home/service/ -type f |grep -v "bash" |sed 's#.*/##' |grep $user)
        for ufile in $ufiles
        do
          	traffic=$(cat /home/service/$ufile)
                let total=$total+$traffic
        done
	echo "Updating the traffic for $user as $total"
        rm -f /home/service/$user.*
       	#./update_statistics.py $user $total
done

Open in new window

0
Comment
Question by:David Sankovsky
[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
1 Comment
 
LVL 1

Accepted Solution

by:
Yuri G earned 500 total points
ID: 41819968
#!/bin/bash
function a {
    let total=0
    for ffile in /home/service/$1.*
      do
      if [ -f $ffile ]; then let total=total+$(cat $ffile); fi
      done
      echo "Total: $total"
      echo "Running update MYSQL for user: $1 with traffic: $total"
      rm -f /home/service/$1.*
}
for files in /home/service/*
    do
      username=$(echo $files | cut -f4 -d"/"| cut -f1 -d".")
      if [ -f $files ]; then a $username; fi
    done
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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