Appending code in a directory recursively based on a certain criteria


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending code in a directory recursively based on a certain criteria
# 8  
Old 11-29-2012
Hi Bakunin,

Here is the modified script that i come up with your suggestion:
Code:
#!/bin/bash
typeset -i lines_in_primery=0

while IFS=',' read name id_number ; do
   name_directory=/usr/myproject/content_to_add/config/$name
   if [ -d $name_directory ] ; then
      lines_in_primery=$(sed -n '$ =' $name_directory/primery.txt)
      if [ $lines_in_primery -lt 5 ] ; then
         echo $id_number >> $name_directory/primery.txt
      else
         echo $id_number >> $name_directory/secondry.txt
      fi
   else
      echo directory $name does not exit >>logs ;
   fi
done < /usr/myproject/content_to_add/master_file

I have done some initial round of tests and its working fine, will check the complete functionality.

I think the error
Quote:
:[: -lt: unary operator expected
was because i was not setting "lines_in_variable" to integer. whats your opinion?

Thanks again for your help.
# 9  
Old 11-29-2012
I'm glad it works. Why your error occurred i have no idea. The shell soes not really differentiate between data types like high-level-languages and even if a string variable contains a numeric value it should work:

Code:
x="5"
if [ $x -lt 10 ] ; then
     echo "it works"
fi

Will work. It would only throw an error when "$x" contains something which can't be interpreted as integer, like "blabla".

The error message suggests that the variable was completely empty, so probably something went wrong when you assigned it a value.

A final suggestion: quote variable contents! For instance, try the following lines:

Code:
name="abc"
name_directory=/usr/myproject/content_to_add/config/$name
if [ -d $name_dir ] ; then
     echo "It works."
fi

If you create the directory "/usr/myproject/content_to_add/config/abc" this will produce the message as expected. Now create the directory "/usr/myproject/content_to_add/config/ab c" (it is possible if you enclose it in double quotes) and try the above code again, replacing "abc" with "ab c". It will throw an error.

The reason is the shell has to somehow know where a word ends and another starts. Every time it encounters a space char it declares the word it is reading at that moment to be finished. If you type "mv a b" it this leads to "mv", "a" and "b" being interpreted as different words (the first being a command, the two others file names) and this is like one would expect things to work. But suppose a file is named "a b": how would you tell the shell to move that? This is called "word splitting" and it is performed on every line during the parsing process. Enclosing something in double quotes is to protect that part from this word splitting and therefore it is a good idea to write:

Code:
name="abc"
# name="ab c"

name_directory="/usr/myproject/content_to_add/config/$name"
if [ -d "$name_dir" ] ; then
     echo "It works."
fi

Which you will notice will work even with a file with spaces in its name. Try it out!

I hope this helps.

bakunin
# 10  
Old 11-30-2012
Thanks Bakunin,

I will take care of this in future. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Merge two files based on matching criteria

Hi, I am trying to merge two csv files based on matching criteria: File description is as below : Key_File : 000|ÇÞ|Key_HF|ÇÞ|Key_FName 001|ÇÞ|Key_11|ÇÞ|Sort_Key22|ÇÞ|Key_31 002|ÇÞ|Key_12|ÇÞ|Sort_Key23|ÇÞ|Key_32 003|ÇÞ|Key_13|ÇÞ|Sort_Key24|ÇÞ|Key_33 050|ÇÞ|Key_15|ÇÞ|Sort_Key25|ÇÞ|Key_34... (3 Replies)
Discussion started by: PK29
3 Replies

2. Shell Programming and Scripting

Delete duplicate row based on criteria

Hi, I have an input file as shown below: 20140102;13:30;FR-AUD-LIBOR-1W;2.495 20140103;13:30;FR-AUD-LIBOR-1W;2.475 20140106;13:30;FR-AUD-LIBOR-1W;2.495 20140107;13:30;FR-AUD-LIBOR-1W;2.475 20140108;13:30;FR-AUD-LIBOR-1W;2.475 20140109;13:30;FR-AUD-LIBOR-1W;2.475... (2 Replies)
Discussion started by: shash
2 Replies

3. Shell Programming and Scripting

Match based on criteria to file

Trying to match $1 of target.txt to $5 of file.txt. If there is a match then in an output.txt file $1,$1 (row underneath),$6,$4,$7 from file.txt are printed on the same line as $1 of target.txt. The input is from excel and the output should be tab-deliminated. Thank you :). target.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

Need To Delete Lines Based On Search Criteria

Hi All, I have following input file. I wish to retain those lines which match multiple search criteria. The search criteria is stored in a variable seperated from each other by comma(,). SEARCH_CRITERIA = "REJECT, DUPLICATE" Input File: ERROR,MYFILE_20130214_11387,9,37.75... (3 Replies)
Discussion started by: angshuman
3 Replies

5. UNIX for Dummies Questions & Answers

How to fetch files right below based on some matching criteria?

I have a requirement where in i need to select records right below the search criteria qwertykeyboard white 10 20 30 30 40 50 60 70 80 qwertykeyboard black 40 50 60 70 90 100 qwertykeyboard and white are headers separated by a tab. when i execute my script..i would be searching... (4 Replies)
Discussion started by: vinnu10
4 Replies

6. UNIX for Dummies Questions & Answers

How to select files based on a criteria?

I have a file..... xxx 2345 455 abc 345 555 cdf 456 777 fff 555 888 Now my requirement is, Say if, i want to select only those records prior to the record fff 555 888... how do i go about doing this in unix.... The fff would be hardcoded as it wud be fixed and everytime when i... (7 Replies)
Discussion started by: saggiboy10
7 Replies

7. Shell Programming and Scripting

Merging Lines based on criteria

Hello, Need help with following scenario. A file contains following text: {beginning of file} New: This is a new record and it is not on same line. Since I have lost touch with script take this challenge and bring all this in one line. New: Hello losttouch. You seem to be struggling... (4 Replies)
Discussion started by: losttouch
4 Replies

8. Shell Programming and Scripting

substract column based on some criteria

Please guide if you know how to solve this. I have a tab delimited INPUT FILE where each record is separated by ----- ----- ABC 4935402 4936680 Pattern=Cheers07080.1 ABC 4932216 4932368 Pattern=Cheers07080.1 ABC 4931932 4932122 ... (8 Replies)
Discussion started by: sam_2921
8 Replies

9. Shell Programming and Scripting

Delete new lines based on search criteria

Hi all! A bit of background: I am trying to create a script that formats SQL statements. I have gotten so far as to add new lines based on certain match criteria like commas, keywords etc. In the process, I end up adding newlines where I don't want. For example: substr(colName, 1, 10)... (3 Replies)
Discussion started by: jayarkay
3 Replies

10. Shell Programming and Scripting

remove lines based on score criteria

Hi guys, Please guide for Solution. PART-I INPUT FILE (has 2 columns ID and score) TC5584_1 93.9 DV161411_2 79.5 BP132435_5 46.8 EB682112_1 34.7 BP132435_4 29.5 TC13860_2 10.1 OUTPUT FILE (It shudn't contain the line ' BP132435_4 29.5 ' as BP132435 is repeated... (2 Replies)
Discussion started by: smriti_shridhar
2 Replies
Login or Register to Ask a Question