Grep command to ignore line starting with hyphen


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep command to ignore line starting with hyphen
# 1  
Old 06-18-2015
Grep command to ignore line starting with hyphen

Hi,

I want to read a file line by line and exclude the lines that are beginning with special characters. The below code is working fine except when the line starts with hyphen (-) in the file.

Code:
for TEST in `cat $FILE | grep -E -v '#|/+' | awk '{FS=":"}NF > 0{print $1}'`
do
.
.
done

How should the grep statement be modified to ignore the line starting with hyphen?

Thanks
Srinraj

Last edited by Don Cragun; 06-19-2015 at 02:32 AM.. Reason: Add CODE tags.
# 2  
Old 06-18-2015
Code:
for TEST in `cat $FILE | grep -E -v '#|/+' | awk '{FS=":"}NF > 0{print $1}'`

The thing is that grep does not need cat.
grep pattern $FILE can read the file for itself.
awk does not need cat or grep. awk can exclude as well on its own.
Code:
awk '$0 !~ /pattern to exclude/ {action}' $FILE

As an example if you want to exclude all these character in the beginning of a line: `#/-+'
Code:
awk '$1 ~ /^[^-#/+]/' $FILE


Last edited by Aia; 06-18-2015 at 08:12 PM.. Reason: Adds beginning of the line anchor
# 3  
Old 06-18-2015
Thanks for your quick response. My requirement is different.

The file contains text as follows.
Code:
#test
+abc
-xyz
real

The for loop has to read the file line by line and pass the string in each line to the do loop. While reading each line in the file, if the line starts with # or + or - that line should be ignored.

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, AND code segments.

Last edited by Don Cragun; 06-19-2015 at 02:30 AM.. Reason: Add CODE and ICODE tags.
# 4  
Old 06-18-2015
Quote:
Originally Posted by Srinraj Rao
Thanks for your quick response. My requirement is different.

The file contains text as follows.
#test
+abc
-xyz
real

The for loop has to read the file line by line and pass the string in each line to the do loop. While reading each line in the file, if the line starts with # or + or - that line should be ignored.
And still applies, if you care to continue using the same script model.
Code:
for TEST in $(awk '$1 ~ /^[^-#/+]/' "$FILE"); do
     something "$TEST"
done

However, the shell doesn't need awk or any external program in this case to do what you want.

Code:
while read line; do
   if [[ "$line" =~ ^[^-+#] ]]; then
        echo "Do something with $line"
   fi
done < "$FILE"

# 5  
Old 06-19-2015
Quote:
Originally Posted by Srinraj Rao
Thanks for your quick response. My requirement is different.

The file contains text as follows.
Code:
#test
+abc
-xyz
real

The for loop has to read the file line by line and pass the string in each line to the do loop. While reading each line in the file, if the line starts with # or + or - that line should be ignored.

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, AND code segments.
Is this a homework assignment?

Homework questions must be posted in the homework & coursework questions forum and must include a completely filled out homework template.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep file starting from pattern matching line

I have a file with a list of references towards the end and want to apply a grep for some string. text .... @unnumbered References @sp 1 @paragraphindent 0 2017. @strong{Chalenski, D.A.}; Wang, K.; Tatanova, Maria; Lopez, Jorge L.; Hatchell, P.; Dutta, P.; @strong{Small airgun... (1 Reply)
Discussion started by: kristinu
1 Replies

2. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

3. SCO

Grep to ignore suffix & find end of line

In COBOL, a hyphen can be used in a field name and in a specific program some field names would be identical to others except a suffix was added--sometimes a suffix to a suffix was used. For example, assume I am looking for AAA, AAA-BBB, and AAA-BBB-CCC and don't want to look at AAA-BBB-CCC... (7 Replies)
Discussion started by: wbport
7 Replies

4. UNIX for Dummies Questions & Answers

Grep only line starting with....

Hello, I have a command that show some application information. Now, I have to grep there informations, like: # showlog | grep 1266 1266.1369866124 :: 1266.1304711286 :: 41031.1161812668 :: 41078.1301266480 :: 41641.712662564 :: 1266.333792515 :: 41462.1512661988 :: 1266.54932671... (5 Replies)
Discussion started by: Lord Spectre
5 Replies

5. Shell Programming and Scripting

Ignore the 255 character limit of command line

Hi I would just like to ask if there is a way for UNIX to ignore/overcome the 255 character limit of the command line? My problem is that I have a really long line of text from a file (300+ bytes) which i have to "echo" and process by adding commands like "sed" to the end of the line, like... (5 Replies)
Discussion started by: agentgrecko
5 Replies

6. Ubuntu

starting Places->Networks at command line

Hi All I have UBUNTU 10.04 I would like to run at command line the gui application that I use for finding network places and navigate shared folders or network driver. I mean the one located under menu PLACES->NETWORK I tried using "nautilus" but you need to know in advance which IP to give and... (2 Replies)
Discussion started by: manustone
2 Replies

7. UNIX Desktop Questions & Answers

How to remove text in each line after hyphen?

Hi, I'm trying to do something relatively simple. I have a txt file that has the following kinds of lines (and many more lines): CP19 Oahu - Maunawili Falls CP20 Oahu - Maunawili Falls AG12 Oahu - Maunawili Falls CP22 Oahu - Maunawili Falls, Local area AG14 Oahu CP141 KZ102 Kauai -... (7 Replies)
Discussion started by: euspilapteryx
7 Replies

8. Shell Programming and Scripting

Grep from a starting line till the end of the file

Hi Folks, I got to know from this forums on how to grep from a particular line say line 6 awk 'NR==6 {print;exit}' But how do i grep from line 6 till the end of the file or command output. Thanks, (3 Replies)
Discussion started by: Mr. Zer0
3 Replies

9. Shell Programming and Scripting

add a hyphen every 2 characters of every line

I have a text file like this with hundreds of lines: >cat file1.txt 1027123000 1027124000 1127125000 1128140000 1228143000 > all lines are very similar and have exactly 10 digits. I want to separate the digits by twodigit and hyphens....like so, > 10-27-12-30-00 10-27-12-40-00... (7 Replies)
Discussion started by: ajp7701
7 Replies

10. Shell Programming and Scripting

use of hyphen in #! line

In one script i have seen - in #! line can somebody explain the meaning of -(hyphen) here #! /bin/sh - (7 Replies)
Discussion started by: Dhruva
7 Replies
Login or Register to Ask a Question