exclude lines in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting exclude lines in a loop
# 1  
Old 03-19-2009
exclude lines in a loop

I use while do - done loop in my shell script. It is working as per my expectations.
But I do not want to process all the lines. I am finding it difficult to exclude certain lines.

1) I do not want to process blank lines as well as lines those start with a space " "
2) I do not want to process the headings. The headlines start with the word AGENT or PRODUCT or TOTAL

Any help will be appreciated.
# 2  
Old 03-19-2009
You can try something like this (and maybe you can do the whole processing with awk):

Code:
awk '!/^ / && !/^$/ && !/^AGENT/ && !/^PRODUCT/ && !/^TOTAL/' file | 
while read line
do
  echo "$line"
  ....
done

Regards
# 3  
Old 03-19-2009
Quote:
Originally Posted by shantanuo
I use while do - done loop in my shell script. It is working as per my expectations.
But I do not want to process all the lines. I am finding it difficult to exclude certain lines.

1) I do not want to process blank lines as well as lines those start with a space " "
2) I do not want to process the headings. The headlines start with the word AGENT or PRODUCT or TOTAL

Any help will be appreciated.

Hello there,

I think the following KornShell script can do the job

Code:
#!/bin/ksh

IFS='
'

while read LINE
do
    
    for TOKEN in $LINE
    do
        UPPER=$(print $TOKEN | tr '[a-z]' '[A-Z]')
        if [[ ($UPPER = +([     ])*) ||
              ($UPPER = *([     ])+(TOTAL|AGENT|PRODUCT)*) ]]
        then
            continue
        else
            #     And here you put all
            #     instructions to process the input
            #     don't forget the following break
            break
        fi
    done
    
done < $1

Actually, because here you ignore also the space character, you will need to modify the default IFS. In fact, the space characters (and I suppose the tabs) must be omitted. The IFS containing only the new line character is defined in the following way

IFS='
'

If you just write something like $'\n' or "\n" it doesn't work. The same is true about tabs. If you want to define a pattern including one or several space/tab characters you have to write [ ], that is, after [ you put a space character and then you push the tab key before closing ] (if instead of doing that you write \t it will not work)

I hope this can help

Regards,
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to exclude some files from the loop on the directory?

I have one question. On the directory I have many files start with DB.DAILYxxxxxxx.YYYYMMDD.HHMMSS and I have several files with other format, like LET.20170310 daily.20170310 tba.20170310 How can I exclude from my loop DB.DAILY files? I tried ls *20170310* | while read... (4 Replies)
Discussion started by: digioleg54
4 Replies

2. Shell Programming and Scripting

Exclude multiple lines using grep

Hi, I'm working on a shell script that reports service status on a database server. There are some services that are in disabled status that the script should ignore and only check the services that are in Enabled status. I output the service configuration to a file and use that information to... (5 Replies)
Discussion started by: senthil3d
5 Replies

3. Shell Programming and Scripting

Exclude lines in a file with matches with multiple Strings using egrep

Hi I have a txt file and I would like to use egrep without using -v option to exclude the lines which matches with multiple Strings. Let's say I have some text in the txt file. The command should not fetch lines if they have strings something like CAT MAT DAT The command should fetch me... (4 Replies)
Discussion started by: Sathwik
4 Replies

4. Shell Programming and Scripting

File lines starts with # not processed or exclude that lines from processing

I have a file like below #Fields section bald 1234 2345 456 222 abcs dddd dddd ssss mmmm mmm mmm i need do not process a files stating with # I was written code below while read -r line do if then echo ${line} >> elif then ... (3 Replies)
Discussion started by: Chenchireddy
3 Replies

5. Shell Programming and Scripting

File lines starts with # not processed or exclude that lines

I have requirement in my every files starting lines have # needs to be not processing or exclude the that lines. I have written a code like below, but now working as expected getting ERROR" line 60: 1 #!/bin/sh 2 echo ======= LogManageri start ========== 3 4 #This directory is... (1 Reply)
Discussion started by: Chenchireddy
1 Replies

6. Shell Programming and Scripting

Git diff exclude swapped lines

Hi, I am doing aws security group auditing every day to find the difference. I am using git to find the difference. But some times some security group rules order is changing up and down(swapping lines). So 'git diff' command gives this as a difference which i dont want(i need only new lines... (2 Replies)
Discussion started by: jobycxa
2 Replies

7. Shell Programming and Scripting

Code to exclude lines with similar values

Hi!!! I have a problem with txt file. For example: File: CATEGORY OF XXX AAA 1 XXX BBB CCC AAA 1 XXX DDD EEE AAA 1 XXX FFF GGG AAA 1 XXX KKK LLL AAA 1 XXX MMM ... (4 Replies)
Discussion started by: Tzole
4 Replies

8. Ubuntu

[Solved] Using Find with an exclude/exclude file

I am familiar with using tar and exclude/include files: tar zcf backup.dirs.tgz --files-from=include.mydirs --exclude-from=exclude.mydirs --no-recursion but was wondering if I could use find in the same way. I know that you can just specify the directories to exclude but my list is... (2 Replies)
Discussion started by: metallica1973
2 Replies

9. UNIX for Dummies Questions & Answers

Exclude lines which have blanks at certain positions

Hi All, I am getting a input file which doesnt have a field seperator. The file is being sorted on certain positions say from 0.55 to 0.59. If there are any blanks from 0.55 to 0.59 they will be listed as first set of records. I am not sure abt the number of records which will have blanks at... (8 Replies)
Discussion started by: helper
8 Replies

10. Shell Programming and Scripting

grep - to exclude lines beginning with pattern

11132 13069 11137 11142 13070 Can I use grep command to exclude all lines beginning with 13? I dont want to use grep -v 13 as potentially there will be a number with something like 11013 that I would exclude in error.. (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question