Solved

listing unique file prefixes in a directory Linux

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now