Solved

listing unique file prefixes in a directory Linux

Posted on 2016-09-28
1
63 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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Fine Tune your automatic Updates for Ubuntu / Debian
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
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…

809 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