Using awk to isolate specific rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk to isolate specific rows
# 15  
Old 10-09-2011
Thanks, nice explanation.
# 16  
Old 10-09-2011
Hey thanks a lot for your help.. Works like a charm now.. I'm now learning how to place in a loop and array a specific list of lecturers to be extracted from the code that you've provided me.. I'll post it here if it works right, or yelp for help if it doesn't work right..

The second input file contains data which is similar to savedrecs.txt.. It's because the ISI Web of Science only allows 500 articles per text file so it has to be broken up to two as there are almost 900 articles..
# 17  
Old 10-31-2011
Update

Hi, sorry for the late reply.. I couldn't set out to complete what I was going to do, so I took the easy way out and made a clunky script.. I'm just posting it to have somewhat completeness to the posts..

A. I create a directory called nominal, put the input file in it and get rid of records starting with AU, ED and BE. These records are inconsequential and would create problems later if included.

Code:
 rm -r nominal
mkdir nominal
cd nominal
cp ../savedrecs.txt ../savedrecs2.txt .
cat savedrecs2.txt >> savedrecs.txt
rm savedrecs2.txt
sed 's/AU/ /g' savedrecs.txt > savedrecsa.txt
sed /ED/d savedrecsa.txt > savedrecsb.txt
sed /BE/d savedrecsb.txt > savedrecsc.txt
mv savedrecsc.txt savedrecs.txt
rm savedrecsa.txt savedrecsb.txt

B. I make a directory for each professor, dump all articles with their names into the file AllPubs, you have to be careful of the syntax though.

Code:
 mkdir Arof.AK
cd Arof.AK
awk '/Arof, A.*K.*$/ { print > "AllPubs" }' RS= ORS='\n\n' ../savedrecs.txt 
awk '/AROF, A.*K.*$/ { print >> "AllPubs" }' RS= ORS='\n\n' ../savedrecs.txt

C. From AllPubs, sort all articles by publication year..

Code:
 gawk '{ 
  /^PY/ && y=$2
  r = r ? r RS $0 : $0  
 }
/^ER/ {
  print r > y
  r = x;
  }' AllPubs

D. Counts the number of publications each year by the number of times the professors name is mentioned.

Code:
 i=1980
while [ "$i" -lt 2012 ]
do
    grep "Arof, A.*K.*$" "$i" > names
    grep "AROF, A.*K.*$" "$i" >> names
    gawk '{nama[$1 $2 $3]++}
    END {for (name in nama) print name, nama[name]}
    ' names > counted
    rm names

    gawk '
    BEGIN   {
        secondcol=0;
        }
        {
        secondcol+=$2;
        }
    END     {
        printf "Arof, AK %d\n",secondcol;
        }
    ' counted > "counted$i"
    rm counted

E. This dumps the number of publication for a certain year into a file called ByYear

Code:
     gawk '{print '"$i"', $3}' counted$i >> ByYear
    rm "counted$i"
    ((i+=1))
done

cd ..

F. And so on..

Code:
 mkdir Shrivastava.KN
cd Shrivastava.KN
awk '/Shrivastava, K.*N.*$/ { print > "AllPubs" }' RS= ORS='\n\n' ../savedrecs.txt

gawk '{ 
  /^PY/ && y=$2
  r = r ? r RS $0 : $0  
 }
/^ER/ {
  print r > y
  r = x;
  }' AllPubs

i=1980
while [ "$i" -lt 2012 ]
do
    grep "Shrivastava, K.*N.*$" "$i" > names
    gawk '{nama[$1 $2 $3]++}
    END {for (name in nama) print name, nama[name]}
    ' names > counted
    rm names
    
    gawk '
    BEGIN   {
        secondcol=0;
        }
        {
        secondcol+=$2;
        }
    END     {
        printf "Shrivastava, KN %d\n",secondcol;
        }
    ' counted > "counted$i"
    rm counted
    
    gawk '{print '"$i"', $3}' counted$i >> ByYear
    rm "counted$i"
    ((i+=1))
done

cd ..


mkdir Kwek.KH
cd Kwek.KH
awk '/Kwek, K.*H.*$/ { print > "AllPubs" }' RS= ORS='\n\n' ../savedrecs.txt 
awk '/KWEK, K.*H.*$/ { print >> "AllPubs" }' RS= ORS='\n\n' ../savedrecs.txt 

gawk '{ 
  /^PY/ && y=$2
  r = r ? r RS $0 : $0  
 }
/^ER/ {
  print r > y
  r = x;
  }' AllPubs

i=1980
while [ "$i" -lt 2012 ]
do
    grep "Kwek, K.*H.*$" "$i" > names
    grep "KWEK, K.*H.*$" "$i" >> names
    gawk '{nama[$1 $2 $3]++}
    END {for (name in nama) print name, nama[name]}
    ' names > counted
    rm names

    gawk '
    BEGIN   {
        secondcol=0;
        }
        {
        secondcol+=$2;
        }
    END     {
        printf "Kwek, KH %d\n",secondcol;
        }
    ' counted > "counted$i"
    rm counted
    
    gawk '{print '"$i"', $3}' counted$i >> ByYear
    rm "counted$i"
    ((i+=1))
done

cd ..

I guess it would be much more elegant if it all professors initials were to be in an array and just using a while loop.. I'll try to learn how to do this later.. Thanks..
# 18  
Old 10-31-2011
Code:
...
/"Yousefi.*$"/ && y = "yousefi"
...

"y" is the filename here. Since now you are extracting the data by author, you can either hardcode the file name as shown above or extract the author dynamically and use it as a filename in which case you will get all the entries in separate file with author's name.
--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

extract rows that have a specific name

Hi I want to extract rows in a large files that have a specific name. The name can be "starved and rich", "rich", "starved" or " ". Heres an example: bob starved and rich jack rich joey starved mike so it can have either 3 names or no name. I want to put the names into a... (4 Replies)
Discussion started by: phil_heath
4 Replies

2. Shell Programming and Scripting

Grep to isolate a text file line and Awk to select a word?

I am looking at using grep to locate the line in the text file and them use awk to select a word or words out of it. I know awk needs -v to allow a variable to be used, but also needs -F to allow the break up of the sentence and allow the location of separate variables. $line = grep "1:" File |... (8 Replies)
Discussion started by: Ironguru
8 Replies

3. Shell Programming and Scripting

Cutting rows at specific length

Hi, i have a file containing nrows and 3cols. i want to cut it in specific length and save output to individual files. 1 2 3 4 5 6 5 8 9 10 11 12 13 14 15 16 17 18 i need to cut the file say every 2 rows and save it in individual file. 01.dat contains 1 2 3 4 5 6 02.dat 7 8 9... (10 Replies)
Discussion started by: ida1215
10 Replies

4. Shell Programming and Scripting

Extracting specific rows

Hi all..... I have a file which contains large data...like I want to print the rows starting from "pixel" till the file read the letter "TER" into a new output file.... can anyone plz help in doing this ?? (5 Replies)
Discussion started by: CAch
5 Replies

5. UNIX for Dummies Questions & Answers

extract specific rows

Hi I have a file that looks like the one below. For the same 'TCONS' in the second column, I would like to extract the row that has the highest (or last) number in the fourth column. Any kind of help will be appreciated. input transcript_id "TCONS_00000051"; exon_number "1"; transcript_id... (4 Replies)
Discussion started by: jdhahbi
4 Replies

6. Shell Programming and Scripting

Counting rows line by line from a specific column using Awk

Dear UNIX community, I would like to to count characters from a specific row and have them displayed line-by-line. I have a file called testAwk2.csv which contain the following data: rabbit penguin goat giraffe emu ostrich I would like to count in the middle row individually... (4 Replies)
Discussion started by: vnayak
4 Replies

7. Shell Programming and Scripting

awk: isolate a part of a file name

hi there, i have a file named 'x20080613_x20100106.pwr1.gc', i want to isolate the part 'x20080613_x20100106' but by using the following line i isolate the part '.pwr1.gc': `awk '$0=substr($0, length($0)-7)' $temp` how can i reverse that? thank you! (3 Replies)
Discussion started by: friend
3 Replies

8. Shell Programming and Scripting

Deleting of Specific Rows.

Fruit : Price : Quantity apple : 20 : 40 chiku : 40 :30 Hey guys, i have written a code using sed to delete a specific char which is being typed in. But the problem i am having is , how can i expand my coding to actually allow it do delete the whole row. For example,... (21 Replies)
Discussion started by: gregarion
21 Replies

9. Shell Programming and Scripting

Deleting specific rows in large files having rows greater than 100000

Hi Guys, I need help in modifying a large text file containing more than 1-2 lakh rows of data using unix commands. I am quite new to the unix language the text file contains data in a pipe delimited format sdfsdfs sdfsdfsd START_ROW sdfsd|sdfsdfsd|sdfsdfasdf|sdfsadf|sdfasdf... (9 Replies)
Discussion started by: manish2009
9 Replies

10. UNIX for Dummies Questions & Answers

how can i isolate the random sequence of numbers using awk?

as you can see there is a delimiter after c8 "::". Awk sees the rest as fields because it doesn't recognize spaces and tabs as delimiters. So i am basically looking to isolate 20030003ba13f6cc. Can anyone help? c8::20030003ba13f6cc disk connected configured unknown (2 Replies)
Discussion started by: rcon1
2 Replies
Login or Register to Ask a Question