what does each of these lines mean?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting what does each of these lines mean?
# 1  
Old 11-05-2010
what does each of these lines mean?

Hello everyone

I am a recent graduate and am totally new to linux. Could anyone please tell me the meaning of each line.

Code:
#!/bin/bash

counter=1
exec 3< /usr/local/test1.txt

while read -u3 line
do
grep "$line" /usr/local/test1.txt2>/dev/null
if [[ $? -eq 1 ]]
then
echo "Line $counter is not present in Naginas_Exp_0206_non_dupCME file!!!"
fi
(( counter += 1 ))
done


Thanks in advance Image
# 2  
Old 11-05-2010
Quote:
Originally Posted by heidik
Hello everyone

I am a recent graduate and am totally new to linux. Could anyone please tell me the meaning of each line.

Code:
#!/bin/bash
this is the shell used to execute the script

counter=1
a variable initialized to value 1

exec 3< /usr/local/test1.txt
open an input steam file descriptor sourced by the file /usr/local/test1.txt

while read -u3 line
loop by reading line by line what comes from the file descriptor 3 that has just been opened previously and put it in a variable called "line"

do
start of the loop

grep "$line" /usr/local/test1.txt 2>/dev/null
get the line of the file /usr/local/test1.txt that match the value of the variable $line
the 2>/dev/null means that if nothing if found, the error message will be ignored (like sent to trash) instead of being displayed

if [[ $? -eq 1 ]]
if the retrun code of the last command equals 1 (if the command did not find any line matching the $line value)

then
keyword mandatory with "if" conditions

echo "Line $counter is not present in Naginas_Exp_0206_non_dupCME file!!!"
then display the message with the value of the counter variable

fi
end of the if condition

(( counter += 1 ))
increment the counter (counter=counter+1)

done

end of the loop

Thanks in advance Image
# 3  
Old 11-05-2010
The whole could have been written simplier :
bash code:
  1. #!/bin/bash
  2. counter=1
  3. while read line
  4. do
  5.    if ! grep -q "$line" /usr/local/test1.txt
  6.    then
  7.       echo "Line $counter is not present in Naginas_Exp_0206_non_dupCME file!!!"
  8.    fi
  9.    ((counter++))
  10. done </usr/local/test1.txt
No need to use file descriptor with the syntax in line 10.
No need to check the value of $?, return code of grep can be used directly.
# 4  
Old 11-05-2010
Yep and ... by the way, in the initial post, a space is missing before the 2>/dev/null

---------- Post updated at 06:55 PM ---------- Previous update was at 06:46 PM ----------

Code:
#!/bin/bash
while read line
do
let counter++
! ( grep -q "$line" /usr/local/test1.txt ) && echo "Line $counter is not present in Naginas_Exp_0206_non_dupCME file!!!"
done </usr/local/test1.txt

# 5  
Old 11-08-2010
Thanks both of you Smilie
# 6  
Old 11-08-2010
frans and ctsgnb both fail (they are comparing a file to it's self Smilie). Program only makes sense if the space was missing after 2 and before >

Code:
grep "$line" /usr/local/test1.txt2 > /dev/null

Code:
#!/bin/bash
while read line
do
    ((counter++))
    grep -q "$line" test1.txt2 || echo "Line $counter is not persent in Naginas_Exp_0206_non_dupCMD file!!!"
done < test1.txt


Last edited by Chubler_XL; 11-08-2010 at 06:09 PM.. Reason: Formatting
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

3. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

4. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

5. UNIX for Advanced & Expert Users

SQL script with 86000 lines: new files with only 10000 lines (per file)

Hi this is my SQL script $ wc -l insert_into_customers.sql 85601 insert_into_customers.sqlI wish to cut this file into 9 files each 10000 lines (the last one less) $ wc -l insert_into_customers_00*.sql 10000 insert_into_customers_001.sql 10000 insert_into_customers_002.sql ... (1 Reply)
Discussion started by: slashdotweenie
1 Replies

6. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

7. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

8. Shell Programming and Scripting

merging two .txt files by alternating x lines from file 1 and y lines from file2

Hi everyone, I have two files (A and B) and want to combine them to one by always taking 10 rows from file A and subsequently 6 lines from file B. This process shall be repeated 40 times (file A = 400 lines; file B = 240 lines). Does anybody have an idea how to do that using perl, awk or sed?... (6 Replies)
Discussion started by: ink_LE
6 Replies

9. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

10. UNIX for Dummies Questions & Answers

How to count lines - ignoring blank lines and commented lines

What is the command to count lines in a files, but ignore blank lines and commented lines? I have a file with 4 sections in it, and I want each section to be counted, not including the blank lines and comments... and then totalled at the end. Here is an example of what I would like my... (6 Replies)
Discussion started by: kthatch
6 Replies
Login or Register to Ask a Question