Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Need help for data extraction if files Post 302707679 by drl on Saturday 29th of September 2012 10:28:46 AM
Old 09-29-2012
Hi.

This can be done in two steps, one to extract the strings token=number and another to sum the individual items:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate extraction of string=integer.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C grep awk

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
grep -E -o '(USU-INOCT|USU-OUTOCT)=[0-9]+' $FILE |
tee f1 |
awk -F= '
/IN/	{ sumi += $2 ; next }
/OUT/	{ sumo += $2 }
END	{ print sumi, sumo, sumi+sumo }
'

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
grep GNU grep 2.5.3
awk GNU Awk 3.1.5

-----
 Input data file data1:
 <MSCC-1>   <RSU>  </RSU>  <USU> USU-TariffTimeChange=0 USU-INOCT=12772 USU-OUTOCT=39177</USU> <USU> USU-TariffTimeChange=1 USU-INOCT=178 USU-OUTOCT=144</USU> SID=46 RG=46 R.REASON=VLD </MSCC-1>   
 <MSCC-1>   <GSU>  GSU-TOTOCT=1024000 </GSU>  SID=46 RG=46 VALTIME=1700 RESULT-CODE=2001 V.Q.THRESHOLD=102400 </MSCC-1>   
 <MSCC-1>   <RSU>  </RSU>  <USU> USU-INOCT=139072 USU-OUTOCT=818813</USU> SID=46 RG=46 R.REASON=VLD </MSCC-1>   
 <MSCC-1>   <GSU>  GSU-TOTOCT=1024000 </GSU>  SID=46 RG=46 VALTIME=1700 RESULT-CODE=2001 V.Q.THRESHOLD=102400 </MSCC-1>   
 <MSCC-1>   <RSU>  </RSU>  <USU> USU-INOCT=122714 USU-OUTOCT=902004 USU-R.RES=QEX</USU> SID=46 RG=46 </MSCC-1>   
 <MSCC-1>   <GSU>  GSU-TOTOCT=1024000 </GSU>  SID=46 RG=46 VALTIME=1700 RESULT-CODE=2001 V.Q.THRESHOLD=102400 </MSCC-1>   
 <MSCC-1>   <RSU>  </RSU>  <USU> USU-INOCT=121962 USU-OUTOCT=902846 USU-R.RES=QEX</USU> SID=46 RG=46 </MSCC-1>   
 <MSCC-1>   SID=46 RG=46 RESULT-CODE=4012 </MSCC-1>   
 <MSCC-1>   <USU> USU-INOCT=52 USU-OUTOCT=0</USU> SID=46 RG=46 R.REASON=FNL </MSCC-1>

-----
 Results:
396750 2662984 3059734

See the intermediate file f1 for the extracted strings, and see man pages for other details ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Data Extraction issue.

I have a small problem, I have written a following script, which extracts all the rows from source file which strats with T101 and rights it to another file mydata.dat Script my_script #!/bin/ksh YMONTH=$1 dir1='/home/data' dir2='/clients/source_file' cd $dir1 grep "T101"... (5 Replies)
Discussion started by: irehman
5 Replies

2. Shell Programming and Scripting

help with data extraction script

Hello all, Iam newbie here and to unix programming. I have the following text file. A:Woshington,B:London,C:Paris,D:Manchester,C:Lisbon,E:Cape town. Now I would like extract this and store in database. here is the script I have tried but it did work. CITY1:`echo "$text" | grep "A:"... (11 Replies)
Discussion started by: mam
11 Replies

3. Shell Programming and Scripting

Data Extraction From a File

Hi All, I have a requirement where I have to search the file with some text say "Exception". This exception word can be repeated for more then 10 times. Suppose the "Exception" word is repeated at line numbers say x=10, 50, 60, 120. Now I want to extract all the lines starting from x-5 to... (3 Replies)
Discussion started by: rrangaraju
3 Replies

4. Shell Programming and Scripting

Another data extraction question

Hi, I have a tmp file like below: <ADATA> ANUM=900 ADESC=Saving ATYP=0 TXREGD=0 </ADATA> <ADATA> ANUM=890 ADESC=Saving ATYP=0 ABAL=9000 TXREGD=1 </ADATA> <ADATA> (6 Replies)
Discussion started by: kunigirib
6 Replies

5. Shell Programming and Scripting

Selective extraction of data from a files

Hi, I would like to seek for methods to do selective extraction of line froma file. The scenario as follows: I have a file with content: message a received on 11:10:00 file size: 10 bytes send by abc message b received on 11:20:00 file size: 10 bytes send by abc (3 Replies)
Discussion started by: dwgi32
3 Replies

6. Shell Programming and Scripting

Extraction of data from multiple text files, and creation of a chart

Hello dear friends, My problem as explained below seems really basic. Fact is that I'm totally new to programming, and have only a week to produce a script ( CShell or Perl ? ) to perform this action. While searching on the forums, I found a command that could help me, but I don't know... (2 Replies)
Discussion started by: ackheron
2 Replies

7. Shell Programming and Scripting

Data and return code extraction

Hello everybody, Another day another problem. I have to create a script which collects data from 3 csv files. I would like the script to check file1 which contains different values for the date entered previously. As you can see 01/12/2010 contains actions in the first field and in the... (7 Replies)
Discussion started by: freyr
7 Replies

8. Shell Programming and Scripting

data extraction from a file

Hi Freinds, I have a file1.txt in the following format File1.txt I want to get 2 files from the above file filextra.txt should have the lines which are ending with "<" and remaining lines in the filecompare.txt file. Please help. (3 Replies)
Discussion started by: i150371485
3 Replies

9. Shell Programming and Scripting

CSV file data extraction

Hi I am writing a shell script to parse a CSV file , in which i am facing a problem to separate the columns . Could some one help me with it. IN301330/00001 pvavan kumar limited xyz@ttccpp.com IN302148/00002 PRECIOUS SECURITIES (P) LTD viash@yahoo.co.in IN300239/00000 CENTRE india... (8 Replies)
Discussion started by: nanduri
8 Replies

10. Shell Programming and Scripting

Data extraction from .xml file

Hello, I'm attempting to extract 13 digit numbers beginning with 978 from a data file with the following command: awk '{ for(i=1;i<=NF;i++) if($i ~ /^978/) print $i; }' datafile > outfile This typically works. However, the new data file is an .xml file, and this command is no longer working... (6 Replies)
Discussion started by: palex
6 Replies
avc_compute_create(3)					     SELinux API documentation					     avc_compute_create(3)

NAME
avc_compute_create, avc_compute_member - obtain SELinux label for new object. SYNOPSIS
#include <selinux/selinux.h> #include <selinux/avc.h> int avc_compute_create(security_id_t ssid, security_id_t tsid, security_class_t tclass, security_id_t *newsid); int avc_compute_member(security_id_t ssid, security_id_t tsid, security_class_t tclass, security_id_t *newsid); DESCRIPTION
avc_compute_create is used to compute a SID to use for labeling a new object in a particular class based on a SID pair. This call is iden- tical to security_compute_create, but does not require converting from userspace SID's to contexts and back again. avc_compute_member is used to compute a SID to use for labeling a polyinstantiated object instance of a particular class based on a SID pair. This call is identical to security_compute_member, but does not require converting from userspace SID's to contexts and back again. These functions return a SID for the computed context in the memory referenced by sid. RETURN VALUE
On success, zero is returned. On error, -1 is returned and errno is set appropriately. ERRORS
EINVAL The tclass and/or the security contexts referenced by ssid and tsid are not recognized by the currently loaded policy. ENOMEM An attempt to allocate memory failed. AUTHOR
Eamon Walsh <ewalsh@tycho.nsa.gov> SEE ALSO
avc_init(3), avc_context_to_sid(3), security_compute_create(3), selinux(8) 30 Mar 2007 avc_compute_create(3)
All times are GMT -4. The time now is 10:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy