script to sort a string of numerical data in set fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to sort a string of numerical data in set fields
# 1  
Old 02-22-2010
script to sort a string of numerical data in set fields

So, I will be working with someone and basically we are trying to build a form that is submitted most likely via the web and the data is just a string of numbers.

like:

19383882872201110929282821818182827349190102837364718191001932873711

Now, each number is part of a numerical value of some data they want. It will always be sent like this in plain text but there is a pattern to it. Like the first 5 digits are data entry A, the next 8 digits are data entry B, the next 3 digits are for C and so forth.

Then I need to output it so it is like this

A = 19383
B = 88287220
C = 111

I don't know all the details but these numbers plug into some system for data analysis and I just need to be able to organize the data for whomever is going to input it. I have no clue if this will be done email or web form. I told them to just send me a plain text file to begin with. I will have to split up each huge string of numbers and assign them to the proper value. I also do not know the patterns yet, but every giant string of numbers will contain the same amount of digits and be in the same pattern.

Now, lets say it is over web, I could have php invoke the shell or run a shell script correct?

Thanks in advance for any insight

-tl
# 2  
Old 02-22-2010
I didn't understand you properly, but if you want to want to split the fields:

there are various ways:

Code:
string="19383882872201110929282821818182827349190102837364718191001932873711"

A=$(echo $string | cut -c1-5)
B=$(echo $string | cut -c6-13)
C=$(echo $string | cut -c14-16)
echo $A $B $C

or

awk -v  string="19383882872201110929282821818182827349190102837364718191001932873711" 'BEGIN {A=substr(string,1,5);B=substr(string,6,8);C=substr(string,14,3);print A,B,C}'

# 3  
Old 02-22-2010
Thanks, but I think there are tons and tons of these files with just numeric strings and they need to be formatted into certain fields.

So if I copied all these files to a folder, and then ran a script that would loop through said folder and grab each set of numbers and put them into a specific value, I think that is what I want to do.

Again, I am not sure of some of the details as I am being asked for help by someone else in a completely different area of where I work. I have no idea what this data is even for.
# 4  
Old 02-22-2010
Code:
/home/forum->cat f
1938388287220111092928282181818
5678388287220111092928282181818
1938388287220111092928282181818
1938388286660111666609082181818
1938388287220111092928282145678
1938388287545676788968999181818
1938388287220111092928282181818
1938388287220111092928282181818
/home/forum->awk '{A=substr($0,1,5);B=substr($0,6,8);C=substr($0,14,3);print A,B,C}' f
19383 88287220 111
56783 88287220 111
19383 88287220 111
19383 88286660 111
19383 88287220 111
19383 88287545 676
19383 88287220 111
19383 88287220 111
/home/forum->

you can put more than one files after the command. or even wild card.

Last edited by clx; 02-22-2010 at 03:19 AM.. Reason: typo
# 5  
Old 02-22-2010
Here is the basic code

Code:
cd "your path where your files are"

DESTPATH="Your dest path"
for filename in *;
do
    
X=`cat $filename |cut -c1-5`
Y=`cat $filename |cut -c6-13`
Z=`cat $filename |cut -c14-16`

echo "A = $X" > /DESTPATH/$filename.new
echo "B = $Y" >> /DESTPATH/$filename.new
echo "C= $Z" >> /DESTPATH/$filename.new
done

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort file data according to a custom list of string

I have a string of pre defined ip address list which will always remain constant their order will never change like in below sample: iplist=8.8.5.19,9.7.5.14,12.9.9.23,8.8.8.14,144.1.113 In the above example i m considering only 5 ips but there could be many more. Now i have a file which... (15 Replies)
Discussion started by: mohtashims
15 Replies

2. Shell Programming and Scripting

Use sort to sort numerical column

How to sort the following output based on lowest to highest BE? The following sort does not work. $ sort -t. -k1,1n -k2,2n bfd.txt BE31.116 0s 0s DOWN DAMP BE31.116 0s 0s DOWN DAMP BE31.117 0s 0s ... (7 Replies)
Discussion started by: sand1234
7 Replies

3. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

4. UNIX for Dummies Questions & Answers

How to sort a column based on numerical ascending order if it includes e-10?

I have a column of numbers in the following format: 1.722e-05 2.018e-05 2.548e-05 2.747e-05 7.897e-05 4.016e-05 4.613e-05 4.613e-05 5.151e-05 5.151e-05 5.151e-05 6.1e-05 6.254e-05 7.04e-05 7.12e-05 7.12e-05 (6 Replies)
Discussion started by: evelibertine
6 Replies

5. Shell Programming and Scripting

Sort numerically a non numerical

Hello, I have this sample data: 01 * * * * 01 * * * * 01 * * * * 01 * * * * 01 0 * * * 01 0 * * * 01 0 * * * 01 0 * * * 02 * * * 0 02 * * * 0 02 * * * 6 02 * * * 6 02 0 * * 1 02 0 * * 1 02 0 * * 2 02 0 * * 2 02 0 * * 3 (3 Replies)
Discussion started by: gio001
3 Replies

6. Shell Programming and Scripting

Reading Numerical Binary Data using KSH

Hi, I've searched and couldn't find anyone else with this problem. Is there anyway (preferably using ksh - but other script languages would do) that I can read in binary float data into a text file. The data (arrays from various stages of radar processing) comes in various formats, but mainly... (3 Replies)
Discussion started by: Jonny2Vests
3 Replies

7. Shell Programming and Scripting

Script to sort data

Hi All, I have a .csv file with 3 columns called nLats, nLongs, and fRes. in following format : "nLats","nLongs","fRes" 0,0,-1 0,1,-1 0,2,-1 0,3,-1 0,4,-1 ......... ......... 0,143,-1 nLats increments at nLongs=143 1,0, -1 1,1, -1 .......... .......... 1,143,-1... (1 Reply)
Discussion started by: wizardy_maximus
1 Replies

8. Shell Programming and Scripting

How to strip non numerical data out of file?

Hi, How can I remove all non numerical data from line, so I don't want to delete the line but to have only the numbers. e.g.: ######### 123 aaa124 125bbb 126 127 ######### So I want all the leading and trailing non numerical stuff(letters/white space/tabs anything else except... (10 Replies)
Discussion started by: Juha
10 Replies

9. Programming

Adding files of numerical data

Hi I was hoping that maybe someone could help me with a small piece of C code. I have a number of files, which are all of similar layout ie. three lines of text and 5-6 columns of numerical data. I need to add each of the elements of the second column in one file to their counterparts in the second... (17 Replies)
Discussion started by: Boucho
17 Replies
Login or Register to Ask a Question