Do nothing if column1 is found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Do nothing if column1 is found
# 1  
Old 10-30-2015
Do nothing if column1 is found

Hello,
I have stucked at one point.
When I run the script, I am asking the script to search in database file and if it's found, do nothing and proceed to next line in database file.

Code:
#!/bin/bash
./extract_email.pl output2 > database
while read -r COL1
    X=$(grep $COL1 database )
    if [[ -z $X ]] ; then
        echo "false"
        exit
else 
do
sed -n '1,2p' /var/test/test.txt > $COL1
sed -e '1,2d' /var/test/test.txt > /var/test/test2.txt
mail -s "mail subject is here" -a "From: info@mymail.net" $COL1 < $COL1
mv $COL1 /var/test/database_pool/
done < database
rm output*
rm *.host
exit 0

when i run the script, it says " syntax error near unexpected token `do' "

Could you please let me know how "do nothing" if col1 is already in my database.

Thanks in advance
Boris

Last edited by baris35; 10-30-2015 at 05:23 PM.. Reason: [solved]
# 2  
Old 10-30-2015
First of all, let me ask you something:

Why are you trying to grep something that you read from same input file?
Code:
X=$(grep $COL1 database )

done < database

This User Gave Thanks to Yoda For This Post:
# 3  
Old 10-30-2015
You've got several syntactical and semantical errors, e.g.
- while read; do ... done is not met
- if ... fi is not met
- mail ... $COL1 < $COL1 uses $COL as the URL and tries to read from it at the same time

But, in the first place, as Yoda pointed out, the entire if branch will never be executed as COL1 read from database will always be found in database...

Last edited by RudiC; 10-30-2015 at 03:33 PM..
This User Gave Thanks to RudiC For This Post:
# 4  
Old 10-30-2015
Hello Yoda,
Thanks for your reply.
You are right but normally if there was no error with script, should it be working?

I have changed it as follows:

Code:
#!/bin/bash
./extract_email.pl output2 > database2
while read -r COL1
    X=$(grep $COL1 database )
    if [[ -z $X ]] ; then
        echo "false"
        exit
else 
do
sed -n '1,2p' /var/test/test.txt > $COL1
sed -e '1,2d' /var/test/test.txt > /var/test/test2.txt
mail -s "mail subject is here" -a "From: info@mymail.net" $COL1 < $COL1
mv $COL1 /var/test/database_pool/
done < database2
rm output*
rm *.host
exit 0

I already have database and database2 files in the folder


Thanks in advance
Boris

---------- Post updated at 02:22 PM ---------- Previous update was at 12:27 PM ----------

Thanks for your help Rudic and Yoda.
Code:
grep -x -f

I have sorted out the problem without using if circle.

b.regards
Boris
# 5  
Old 10-30-2015
Did you resolve all the concerns? Brilliant. Why don't you post your resulting script as a reference for others?
This User Gave Thanks to RudiC For This Post:
# 6  
Old 10-30-2015
I know this code is not good but works for me:

Code:
#!/bin/bash
cd /home/info/Maildir/new/
sed -n '/Return/p' *.host > output
sed 's/</ /' output > output1
sed 's/>/ /' output1 > output2
./extract_email.pl output2 > database
diff -U $(wc -l < database) database emails | grep '^-' | sed 's/^-//g' > database2
while read -r COL1
        do
mail -s "mail subject is here" -a "From: info@myemail.net" $COL1 < $COL1
done < database2
exit 0

thanks for your help
Boris
This User Gave Thanks to baris35 For This Post:
# 7  
Old 10-30-2015
You know you can do several replacements with one sed command:
Code:
sed -n '/Return/{ s/<|>/ /g; p}' *.host > output

(untested)? Same holds true for the grep ... | sed ... pipe. And still I don't understand the mail ... $COL1 < $COL1 construct...
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

CSV File:Filter duplicate records from column1 & another column having unique record

Hi Experts, I have csv file with 30, 40 columns Pasting just 2 column for problem description. Need to print error if below combination is not present in file check for column-1 (DocumentNumber) and filter columns where value in DocumentNumber field is same. For all such rows, the field... (7 Replies)
Discussion started by: as7951
7 Replies

2. Programming

Perl script to merge cells in column1 which has same strings, for all sheets in a excel workbook

Perl script to merge cells ---------- Post updated at 12:59 AM ---------- Previous update was at 12:54 AM ---------- I am using below code to read files from a dir and print to excel. open(my $in, '<', $file) or die "Could not open file: $!"; my $rowCount = 0; my $colCount = 0;... (11 Replies)
Discussion started by: Jack_Bruce
11 Replies

3. Shell Programming and Scripting

Help with Perl script for identifying dupes in column1

Dear all, I have a large dictionary database which has the following structure source word=target word e.g. book=livre Since the database is very large in spite of all the care taken, it so happens that at times the source word is repeated e.g. book=livre book=tome Since I want to... (7 Replies)
Discussion started by: gimley
7 Replies

4. Shell Programming and Scripting

Replace text in column1 of a file matching columns of another file

Hi all, I have 2 files: species-names.txt Abaca-bunchy-top-virus ((((Abaca-bunchy-top-virus((Babuvirus((Unassigned((Nanoviridae((Unassigned)))) Abutilon-mosaic-virus ((((Abutilon-mosaic-virus((Begomovirus((Unassigned((Geminiviridae((Unassigned))))... (2 Replies)
Discussion started by: thienxho
2 Replies

5. Shell Programming and Scripting

How to change value in CSV columns and compare two files where Column1 is identical

Hi all, Could someone help me with the following issue: 1st I have an CSV file delimiter is ";" I I have a column 7 where I need to do some multiple mathem. operation, I need all values in this columns to be multiplied by 1.5 and create a new CSV file with the replaced values. 2nd. I... (3 Replies)
Discussion started by: kl1ngac1k
3 Replies

6. Shell Programming and Scripting

awk : Remove column1 and last column in a line

Hi All, How to remove col1 and last column in a line. Please suggest some awk stuffs. Input col1 col2 col3 col4 col1 col2 col3 col4 col5 col1 col2 col3 col4 col1 col2 col3 Output Processing col2 col3 ... Processing col2 col3 col4 ... Processing col2 col3 ... Processing... (5 Replies)
Discussion started by: k_manimuthu
5 Replies

7. Shell Programming and Scripting

Getting rip of multiple rows based on column1

Hi, I want to get rid of multiple rows (duplicate, triplicate etc..) for only column 1. e.g. iu 2 iu 1 iu 3 k 4 jk 3 nm 4 nm 2 output k 4 jk 3 thanks (7 Replies)
Discussion started by: phil_heath
7 Replies

8. Shell Programming and Scripting

delete a string on column1

Hi I have a file with multiple columns. But there is something weird on column one that is attached to the name. The good thing is that its a consistent pattern so there should be a way to remove it. So the first column looks something like this: name_345.4ml date_3456.4ml year_12.4ml... (3 Replies)
Discussion started by: kylle345
3 Replies

9. Shell Programming and Scripting

awk - replacing stings in file1 with column1 in file2

Hello, I've never used awk before, but from what I've read, it will best suit what I'm trying to do. I have 2 files. I need to replace strings in file1 with the first column of a matching string in file2. Below are examples: File1: random-string1 1112 1232 3213 2131 random-string2... (7 Replies)
Discussion started by: upstate_boy
7 Replies

10. Shell Programming and Scripting

only if column1 equals this print that

I have text file with hundreds of lines, space delimited, each line has the same amount of "columns" and the same amount of characters in each, Column 1, Column 2, and Column 3. I need a script that will print all columns of the "current" line along with the last two columns of the next line ONLY... (3 Replies)
Discussion started by: ajp7701
3 Replies
Login or Register to Ask a Question

Featured Tech Videos