Sponsored Content
Top Forums Shell Programming and Scripting Sort data in text file in particular format Post 303002420 by RavinderSingh13 on Wednesday 23rd of August 2017 11:20:59 PM
Old 08-24-2017
Quote:
Originally Posted by Adfire
Hi thanks for your reply it's working now.Now i need to add sum (Total).Example below
Code:
20170308
PM,Total,246
PM,I,123
PM,U,123
DA,Total,135
DA,I,113
DA,U,22
20170309
PM,Total,246
PM,I,23
PM,U,233
DA,Total,134
DA,I,11
DA,U,123

Hello Adfire,

Please always use code tags as per forum rules for your commands/codes/Input_files, could you please try following and let me know if this helps you.
Code:
awk -F, '
val && /^[0-9]+/ && val !~ $0{
    print val;
    for(i in b){
       if(a[i]){
         print c[i],a[i];
         delete a[i]
};
       print b[i]
};
    delete b;
    delete c
}
/^[0-9]+/{
    val=$0;
    next
}
{
    c[$1]=$1 FS $2;
    a[$1]+=$3;
    b[$1]=b[$1]?b[$1] ORS $0:$0
}
END{
    print val;
    for(i in b){
       if(a[i]){
         print c[i],a[i];
         delete a[i]
};
    print b[i]
}
}
'   Input_file

Output will be as follows.
Code:
20170308
PM,U 246
PM,I,123
PM,U,123
DA,U 135
DA,I,113
DA,U,22
20170309
PM,U 256
PM,I,23
PM,U,233
DA,U 134
DA,I,11
DA,U,123

Thanks,
R. Singh
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

write data into a text file in bold format

Hi, can anyone help to write data into a text file in bold format and rollback to actual format. Thanks, Regards, Milton Y. (1 Reply)
Discussion started by: miltony
1 Replies

2. UNIX for Dummies Questions & Answers

Arrangeing Swap text data in format

Hi, Please guide me if there is any option of converting text from the following format using regexp etc, 1,a,b,c,d,e,f,d 2,aa,bb,cc,dd,ee,ff 1,6a,6b,6c,6d,6e,6c 2,7a,7b,7c,7d,7e,7f to be group into 1'ns together and 2's together as follows. 1,a,b,c,d,e,f,d 1,6a,6b,6c,6d,6e,6c... (1 Reply)
Discussion started by: shar_prabs
1 Replies

3. Shell Programming and Scripting

Sort a big data file

Hello, I have a big data file (160 MB) full of records with pipe(|) delimited those fields. I`m sorting the file on the first field. I'm trying to sort with "sort" command and it brings me 6 minutes. I have tried with some transformation methods in perl but it results "Out of memory". I was... (2 Replies)
Discussion started by: rubber08
2 Replies

4. Shell Programming and Scripting

Sort a the file & refine data column & row format

cat file1.txt field1 "user1": field2:"data-cde" field3:"data-pqr" field4:"data-mno" field1 "user1": field2:"data-dcb" field3:"data-mxz" field4:"data-zul" field1 "user2": field2:"data-cqz" field3:"data-xoq" field4:"data-pos" Now i need to have the date like below. i have just... (7 Replies)
Discussion started by: ckaramsetty
7 Replies

5. UNIX for Advanced & Expert Users

Sort mixed data file

I have a text file and each field is separated by semicolon ( ; ). Field number 7 is internally separated by comma ( , ) and pipe ( | ) symbol. I want to sort file based on three different fields which are marked in BOLD. Here first BOLD field will have numbers upto the length of 9 characters,... (6 Replies)
Discussion started by: jnrohit2k
6 Replies

6. Shell Programming and Scripting

Converting text files to xls through awk script for specific data format

Dear Friends, I am in urgent need for awk/sed/sh script for converting a specific data format (.txt) to .xls. The input is as follows: >gi|1234|ref| Query = 1 - 65, Target = 1677 - 1733 Score = 8.38, E = 0.6529, P = 0.0001513, GC = 46 fd sdfsdfsdfsdf fsdfdsfdfdfdfdfdf... (6 Replies)
Discussion started by: Amit1
6 Replies

7. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

8. Shell Programming and Scripting

Sort data file by case

Hello, I'm trying to sort a large data file by the 3rd column so that all of the first words in the 3rd column that are in all uppercase appear before (or after) the non uppercase words. For example, Data file: xxx 12345 Rat in the house xxx 12345 CAT in the hat xxx 12345 Dog in the... (4 Replies)
Discussion started by: palex
4 Replies

9. Shell Programming and Scripting

Merge and Sort tabular data from different text files

I have 42 text files; each containing up to 34 lines with following structure; file1 H-01 23 H-03 5 H-05 9 H-02 14 . . file2 H-01 17 H-02 43 H-04 7 H-05 8 H-03 7 . . file3 (6 Replies)
Discussion started by: Syeda Sumayya
6 Replies

10. UNIX for Beginners Questions & Answers

Convert text file data into XL file format

Hi i have a file containing below info and want it to put in xl format 2878042 455134 3333176 24.231979 23.81 2880246 453022 3333268 24.141338 23.81 2879677 453495 3333172 24.310986 23.81i want this data in XL file format and want that my linux system should send me that file on my mail.... (8 Replies)
Discussion started by: scriptor
8 Replies
DIALTEST(8)						      System Manager's Manual						       DIALTEST(8)

NAME
dialtest - HylaFAX dial string processing rules test program SYNOPSIS
/usr/sbin/dialtest [ options ] dialrules DESCRIPTION
dialtest is an interactive program for the testing and development of dial string processing rules used by HylaFAX. dialtest reads the file of rules specified on the command line and then prompts for a dial string. For each string typed in, dialtest prints the result of applying the canonicalization and dial-string preparation rule sets. A rule set whose name is setname can be specified by typing ``set- name(string)''; for example, ``CanonicalName(+1.415.965.7824)''. Note that dialtest reads the rules file only when it is initially started up. This means that dialtest must be restarted for it to apply changes to a rules file. OPTIONS
-a code Use code as the value of AreaCode in the rules file. The default area code is ``415''. -c code Use code as the value of CountryCode in the rules file. The default country code is ``1''. -i prefix Use prefix as the value of InternationalPrefix in the rules file. The default international dialing prefix is ``011''. -l prefix Use prefix as the value of LongDistancePrefix in the rules file. The default long distance dialing prefix is ``1''. -q Be quiet, suppressing most all output except for the actual results. This is useful for using dialtest in an automated fashion. -v Print each input line before displaying the results of applying the dial string rules to the input string. This can be useful, for example, in shell scripts. SEE ALSO
dialrules(5) March 3, 1995 DIALTEST(8)
All times are GMT -4. The time now is 08:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy