Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-16-2012
A-V A-V is offline
Registered User
 
Join Date: May 2012
Posts: 103
Thanks: 54
Thanked 2 Times in 2 Posts
Data Problem with grep from pattern file with if

I have two sets of data

accept.txt is the list of 50 words

Code:
Tom
Anne
James
...

and I have a file01.txt which has the frequency of words which I got it using

Code:
cat main.file | tr -d '[:punct:]' | tr ' ' '\n' | tr 'A-Z' 'a-z' | sort | uniq -c | sort -n -r > file01.txt

so my file now looks like this

Code:
155 the
100 a
55  this
34  Anne
5   James

I need the output to look like this

Code:
0
34
5

I tried using grep but dont know how to replace things with 0 and just print one column

Code:
for name in `cat accept.txt ` 
do 
grep $name file01.txt;
done

I have to do this for 20 files in the same folder and would like to eventually print it as

file01 file 02 .... file20
Tom 0 54 .... 0
Anne 34 0 ... 43
James 0 43 .... 12

can someone please help me?
Thank you in advance for your time
Sponsored Links
    #2  
Old 06-17-2012
Registered User
 
Join Date: May 2012
Posts: 58
Thanks: 5
Thanked 9 Times in 9 Posts
Try this script:


Code:
#!/bin/ksh

ls file* | sort | tr '\n' '\t' | sed 's/^/\t/g'
echo ""

for name in `cat accept.txt`
do
printf "${name}"
  for i_file in `ls file* | sort`
  do  
    count=`grep -c "${name}" "${i_file}"`
    printf "\t${count}"
  done
echo""
done

Sponsored Links
    #3  
Old 09-03-2012
A-V A-V is offline
Registered User
 
Join Date: May 2012
Posts: 103
Thanks: 54
Thanked 2 Times in 2 Posts
Thanks for the help... I find a solution for if but yours look much more pro
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script to grep a pattern from file compare contents with another file and replace namitai Shell Programming and Scripting 2 08-30-2011 01:31 PM
Grep a pattern given in one file at other file and display its corresponding contents as output. abinash Shell Programming and Scripting 7 02-23-2011 01:51 AM
Grep pattern from different file and display if it exists in the required file abinash Shell Programming and Scripting 3 12-08-2010 08:20 AM
Grep a pattern in gz file rprajendran UNIX for Dummies Questions & Answers 2 12-01-2008 01:27 PM
Grep pattern problem mojoman UNIX for Dummies Questions & Answers 1 11-26-2008 10:50 AM



All times are GMT -4. The time now is 06:36 PM.