Sponsored Content
Full Discussion: exclude lines in a loop
Top Forums Shell Programming and Scripting exclude lines in a loop Post 302299153 by dariyoosh on Thursday 19th of March 2009 10:13:18 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
GREP(1) 						      General Commands Manual							   GREP(1)

NAME
grep - search a file for a pattern SYNOPSIS
grep [ option ... ] pattern [ file ... ] DESCRIPTION
Grep searches the input files (standard input default) for lines (with newlines excluded) that match the pattern, a regular expression as defined in regexp(6). Normally, each line matching the pattern is `selected', and each selected line is copied to the standard output. The options are -c Print only a count of matching lines. -h Do not print file name tags (headers) with output lines. -i Ignore alphabetic case distinctions. The implementation folds into lower case all letters in the pattern and input before interpre- tation. Matched lines are printed in their original form. -l (ell) Print the names of files with selected lines; don't print the lines. -L Print the names of files with no selected lines; the converse of -l. -n Mark each printed line with its line number counted in its file. -s Produce no output, but return status. -v Reverse: print lines that do not match the pattern. Output lines are tagged by file name when there is more than one input file. (To force this tagging, include /dev/null as a file name argument.) Care should be taken when using the shell metacharacters $*[^|()= and newline in pattern; it is safest to enclose the entire expression in single quotes '...'. SOURCE
/sys/src/cmd/grep.c SEE ALSO
ed(1), awk(1), sed(1), sam(1), regexp(6) DIAGNOSTICS
Exit status is null if any lines are selected, or non-null when no lines are selected or an error occurs. GREP(1)
All times are GMT -4. The time now is 06:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy