Split file by number of words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split file by number of words
# 1  
Old 09-23-2010
Split file by number of words

Dear all
I am trying to divide a file using the number of words as a condition. Alternatively, I would at least like to be able to retrieve the first x words of a given file. Any tips?
Thanks in advance.
# 2  
Old 09-23-2010
get a fixed number of words:
Code:
wcnt=200 # get first 200 words
awk -v w=$wcnt  '{for(i=1;i<=NF && cnt<w; i++) 
                                    {printf("%s ", $i); cnt++}   look here 
                           print
                          if(cnt>=w) {exit} } '  inputfile > outputfile


Last edited by jim mcnamara; 09-23-2010 at 11:41 AM..
# 3  
Old 09-23-2010
Thanks for the reply. I got an error:

awk: syntax error at source line 1
context is
{for(i=1;i<=NF && cnt<w; >>> i++, <<<
awk: illegal statement at source line 1
awk: illegal statement at source line 1


Thank you
# 4  
Old 09-23-2010
Made a change - my awk liked what I did, I forgot other awks may not. My bad. See old post above

If you are on Solaris use nawk not awk.
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 09-23-2010
bash code:
  1. N=200
  2. while read L
  3. do
  4.    for W in $L
  5.    do
  6.       ((i++))
  7.       echo $W
  8.       ((i>=N)) && break 2
  9.    done
  10. done <file.txt
This User Gave Thanks to frans For This Post:
# 6  
Old 09-23-2010
Thank you for your replies. jim mcnamara's suggestion worked best, as frans way of doing it works, but unfortunately messes up the original text format (writes one word in each line), which I'd like to retain. Thank you both, though.
# 7  
Old 09-23-2010
just modified line 7. as follows
Code:
      echo -n "$W "

and added an echo between lines 9 and 10.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split File based on number of rows

Hi I have a requirement, where i will receive multiple files in a folder (say: /fol1/fol2/). There will be at least 14 to 16 files. The size of the files will different, some may be 80GB or 90GB, some may be less than 5 GB (and the size of the files are very unpredictable). But the names of the... (10 Replies)
Discussion started by: kpk_ds
10 Replies

2. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

3. Shell Programming and Scripting

problem to count number of words from file

hi every one i have written this simple shell for counting number of word that user need to find from file but i have get several error when run it. can someone tell me the problem ? echo "Enter the file name" read file echo "enter word" read word for i in \`cat $file` do if then... (1 Reply)
Discussion started by: nimafire
1 Replies

4. UNIX for Dummies Questions & Answers

Split single file into n number of files

Hi, I am new to unix. we have a requirement here to split a single file into multiples files based on the number of people available for processing. So i tried my hand at writing some code as below. #!/bin/bash var1=`wc -l $filename` var2=$var1/$splitno split -l $var2 $1 Please help me... (6 Replies)
Discussion started by: quirkguy
6 Replies

5. Shell Programming and Scripting

Count the number of words in some subset of file and disregard others

Hi All, I have some 6000 text files in a directory. My files are named like 1.txt, 2.txt 3.txt and so on until 6000.txt. I want to count the "number of words" in only first 3000 of them. Any suggestions? I know wc -w can count the number of words in a text file. I am using Red Hat Linux. (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

6. Shell Programming and Scripting

Counting number of files that contain words stored in another file

Hi All, I have written a script on this but it does not do the requisite job. My requirement is this: 1. I have two kinds of files each with different extensions. One set of files are *.dat (6000 unique DAT files all in one directory) and another set *.dic files (6000 unique DIC files in... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

7. Shell Programming and Scripting

Finding the number of unique words in a file

find the number of unique words in a file using sort com- mand. (7 Replies)
Discussion started by: abhikamune
7 Replies

8. Shell Programming and Scripting

Split File of Number with spaces

How do i split a variable of numbers with spaces... for example echo "100 100 100 100" > temp.txt as the values can always change in temp.txt, i think it will be feasible to split the numbers in accordance to column. How is it possible to make it into $a $b $c $d? (3 Replies)
Discussion started by: dplate07
3 Replies

9. UNIX for Dummies Questions & Answers

split a file into a specified number of files

I have been googling on the 'split' unix command to see if it can split a large file into 'n' number of files. Can anyone spare an example or a code snippet? Thanks, - CB (2 Replies)
Discussion started by: ChicagoBlues
2 Replies

10. Shell Programming and Scripting

To read and separate number and words in file and store to two new file using shell

hi, I am a begginer in unix and i want to know how to open a file and read it and separate the numbers & words and storing it in separate files, Using shell scripting. Please help me out for this. Regards S.Kamakshi (2 Replies)
Discussion started by: kamakshi s
2 Replies
Login or Register to Ask a Question