Scripting Help needed with a text file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scripting Help needed with a text file.
# 1  
Old 07-27-2012
Scripting Help needed with a text file.

Hi,
Iam a novice to unix shell scripting, need a help from you guys.The scenario is as follows, i have the following Text file.
Code:
order No    Company    Category
21    aaa    A
24    aaa    A
87    aaa    B
98    aaa    B
23    abc    A
45    abc    B
25    bbb    A
76    wes    A
66    wes    B
44    wes    B
35    wes    B
39    wes    B
90    esd    B
99    esd    B
109    esd    B
26    esd    B
58    esd    A
76    tre    B
75    tre    B

Desired Output is as follows(columns Count, A, B are the no of times they are repeated):

HTML Code:
Company    Count    A    B
aaa    4    2    2
abc    2    1    1
bbb    1    1    0
wes    5    1    4
esd    5    4    1
tre    2    0    2
The output should be in a different file. Appreciate your help.
# 2  
Old 07-27-2012
Hi,

Code:
awk '/^[0-9]/ { print $2,$3}' input-file | sort | uniq -c | awk 'BEGIN{print "Company    Count   A   B"} {if (comp && comp != $2) { printf("%-10s %5d %3d %3d\n",comp,a["A"]+a["B"],a["A"],a["B"]); comp=""; a["A"]=a["B"]=0; } comp=$2; a[$3]=$1; } END{if (comp) printf("%-10s %5d %3d %3d\n",comp,a["A"]+a["B"],a["A"],a["B"]);}'


Last edited by Chirel; 07-27-2012 at 07:55 PM.. Reason: oops forget END part of awk script
# 3  
Old 07-27-2012
Quote:
Originally Posted by Chirel
Code:
<very long line of code here>

That's a one-liner in name only. In the future, please use a reasonable coding style instead of such a long line. It will make your code easier for novices to understand, and members not using a 2560 pixel wide display will be able to read your post (and posts which quote your post) without having to resort to tedious horizontal scrolling.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 07-28-2012
Hi,

Alister you are right, even if this solve the problem it's not user friendly, so here is the readable version Smilie

First take only company name and the A/B status and we sort them

Code:
# awk '/^[0-9]/ { print $2,$3}' input-file | sort > sorted-file

Then we process the sorted-file by counting duplicates and re-arrange output

Code:
# uniq -c sorted-file | awk -f doit.awk
Company    Count   A   B
aaa            4   2   2
abc            2   1   1
bbb            1   1   0
esd            5   1   4
tre            2   0   2
wes            5   1   4

here is the content of the file doit.awk :
Code:
BEGIN { 
  print "Company    Count   A   B"
}

{
  if (comp && comp != $2) {
    printf("%-10s %5d %3d %3d\n",comp,a["A"]+a["B"],a["A"],a["B"]);
    comp="";
    a["A"]=a["B"]=0;
  }
  comp=$2;
  a[$3]=$1;
}

END {
  if (comp) printf("%-10s %5d %3d %3d\n",comp,a["A"]+a["B"],a["A"],a["B"]);
}

This User Gave Thanks to Chirel For This Post:
# 5  
Old 07-28-2012
awk

Hi,

Try this one,
Shorter version of untested code,
Code:
awk '/^[0-9]/ {a[$2]=a[$2]+1;b[$2" "$3]=b[$2" "$3]+1;}END{print"Company    Count   A   B"; for(i in a){printf("%-10s %5d %3d%3d\n",i,a[i],b[i" A"],b[i" B"]);}}' inputfile

if you have GNU awk you can sort the array a using asort function.
Cheers,
Ranga:-)

Last edited by rangarasan; 07-28-2012 at 05:50 AM..
This User Gave Thanks to rangarasan For This Post:
# 6  
Old 07-28-2012
Well done Ranga Smilie

ps for fun :

Code:
awk '/^[0-9]/ {a[$2]++;b[$2" "$3]++;}END . . . .

# 7  
Old 07-28-2012
awk

Hi,
I am unable to put double plus sign through my mobile thats why i used that. I should change my mobile.;-)
Happy weekend:-)
Cheers,
Ranga:-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

2. Shell Programming and Scripting

FTP a file using Shell Scripting (Help needed)

the requirements is to have a linux script which connects to a windows machine using ftp command and check for a flag file if found copy a .csv file into current machine. (3 Replies)
Discussion started by: tradingspecial
3 Replies

3. Shell Programming and Scripting

Help needed editing text file using the terminal

Hi, I have text file with the header like this tracking_id condition replicate FPKM XLOC_000001 alpha 1 10.3199 XLOC_000001 alpha 0 10.3686 XLOC_000001 alpha 2 15.5619 ... With the first column being genes, the second being the condition, the third... (5 Replies)
Discussion started by: 4galaxy7
5 Replies

4. Shell Programming and Scripting

Needed shell script to append desired text to each line in a file

Hi, I had generated a report in my tool as followsoutput.txt 43.35 9 i needed the script to generate a new file like below i want to append the text to each of these lines of my filenewoutputfile.txt should be Total Amount : 43.35 Record Count:9 Regards, Vasa Saikumar. ... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

5. Shell Programming and Scripting

KSH - help needed for creating a script to generate xml file from text file

Dear Members, I have a table in Oracle DB and one of its column name is INFO which has data in text format which we need to fetch in a script and create an xml file of a new table from the input. The contents of a single cell of INFO column is like: Area:app - aam Clean Up Criteria:... (0 Replies)
Discussion started by: Yoodit
0 Replies

6. Shell Programming and Scripting

shell or perl script needed for ldif file to text file conversion

This is the ldf file dn: sdcsmsisdn=1000000049,sdcsDatabase=subscriberCache,dc=example,dc=com objectClass: sdcsSubscriber objectClass: top postalCode: 29600 sdcsServiceLevel: 10 sdcsCustomerType: 14 givenName: Adelia sdcsBlackListAll: FALSE sdcsOwnerType: T-Mobile sn: Actionteam... (1 Reply)
Discussion started by: LinuxFriend
1 Replies

7. Shell Programming and Scripting

search needed part in text file (awk?)

Hello! I have text file: From aaa@bbb Fri Jun 1 10:04:29 2010 --____OSPHWOJQGRPHNTTXKYGR____ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline My code '234565'. ... (2 Replies)
Discussion started by: candyme
2 Replies

8. Shell Programming and Scripting

Scripting change of text in another file

Hello, I am pretty new to UNIX/bash scripting, so this question may seem obvious. My experience is simply stringing commands together in a script, maybe doing some if/then testing and such, so I haven't gotten into anything too heavy... I have a shell script that I use as a template to create... (7 Replies)
Discussion started by: vwgtiturbo
7 Replies

9. Shell Programming and Scripting

Help needed in extracting text present between two headers in .txt file

Hi All, Please help me out in fllowing problem. I have text file which contains the data in following format. Contents of file.txt are setregid02 Test that setregid() fails and sets the proper errno values when a non-root user attemps to change the real or effective... (2 Replies)
Discussion started by: varshit
2 Replies

10. UNIX for Dummies Questions & Answers

Urgent help needed to delete some text without opening the file in unix

Hi To delete some text in 2 files in line1 ( not complete line) in unix without opening the files. For example: source file is like this <?xml version="1.0"... (5 Replies)
Discussion started by: pyaranoid
5 Replies
Login or Register to Ask a Question