How to ignore comments at the end of the each line?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to ignore comments at the end of the each line?
# 1  
Old 11-12-2013
How to ignore comments at the end of the each line?

Hi All,

I am reading the host file by ignoring the comments and write it to the other file. I am reading with regular expression for IP address.

grep -E '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' $inputFile | awk '{for(i=2;i<=NF;i++)print $1,$i}'  > $DR_HOME/OS/temp

After that am reading each host name and pinging it and matching the IP address in the response with the IP address in the file.

while read ip host
ip1=$(ping -q -c 1 -W 1 "$host" |grep PING | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')

Now my host file contains the comment at the end of the line also For E.G.

Code:    SQL1 Delta ori                      #TIB Prod

How to ignore the comments in the end while reading the line
# 2  
Old 11-12-2013
 awk -F"#" '{print $1}' filename

# 3  
Old 11-12-2013
Or you can modify your awk statement to as below

awk '{for(i=2;i<=NF;i++) {if (substr($i,1,1)=="#") next; else print $1,$i}}'

This User Gave Thanks to krishmaths For This Post:
# 4  
Old 11-12-2013
Thanks Kris, Its working fine for me.
# 5  
Old 11-12-2013
Or remove the comment upfront so you don't need to check for it in every loop:
awk '{sub (/#.*$/,""); for(i=2;i<=NF;i++) print $1,$i}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. UNIX for Dummies Questions & Answers

Remove multi line and single line comments

Hi, I am trying to remove multi line and single line comments like examples below I have tried this pattern. it works fine for single line comments and multi line comments in a single line only. but this fails when the comments are extended in multiple lines as shown in the comment 2 of... (3 Replies)
Discussion started by: ahmedwaseem2000
3 Replies

3. Shell Programming and Scripting

Removing SAS multi line comments in UNIX

i have to remove the commented (/* . . . .*/) part which starts in one line and ends in me with generic code because i have 1000 to 10k lines code which i have to remove. data one; set work.temp; input name age; infile filename; /* dfsdf dsfs sdfdf dsdd sdfsf sdfsf sfs... (4 Replies)
Discussion started by: saaisiva
4 Replies

4. Shell Programming and Scripting

Help needed for diff to ignore a line with certain pattern

Hello Guys, I request anyone to do me a small help in using diff command for following. I am trying to compare two files for content and wish to keep the content after the comparison (The resultant file can't be blank) However, the first lines would be different in both files and I need diff... (2 Replies)
Discussion started by: rockf1bull
2 Replies

5. Post Here to Contact Site Administrators and Moderators

Comments on "How Will the World End?"

I have read the sun-expansion scenario numerous places but I've never read any suggestion that the earth's orbit would increase to avoid being scorched. What mechanism would push it out? As for creating a black hole by the LHC, the whole concept is silly so any number of reasons would rule it... (9 Replies)
Discussion started by: KenJackson
9 Replies

6. Shell Programming and Scripting

awk : ignore first 5 line and last line of a file

Hi All, I have text file like as below temp.txt 1 Line temp.txt 2 Line temp.txt 3 Line temp.txt 4 Line temp.txt 5 Line temp.txt 6 Line temp.txt 7 Line temp.txt 8 Line temp.txt N Line I expect the output like as below processing 6 ... processing 7 ... (6 Replies)
Discussion started by: k_manimuthu
6 Replies

7. Shell Programming and Scripting

Removing Multiple Line comments

:) Hi all, I would like to remove all multiple line comments, in between the pattern /*.....*/ from my file. The comments can be expected to be at any place in my file, like this: /*.... This multi line comment might stretch even the entire file..! ......*/ Some text Text1 /* Comment... (25 Replies)
Discussion started by: Jackuar
25 Replies

8. UNIX for Advanced & Expert Users

Script to ignore word from a line ...

Hi Gurus, I'm need of a script in which we are finding an independent word ‘boy' in a log file. We are using grep in order to do the same. Now in this log file there are some sentences where we see ‘This is a boy' and we do not want to count word ‘boy' from this sentence. So in other word we want... (2 Replies)
Discussion started by: heyitsmeok
2 Replies

9. Shell Programming and Scripting

How to filter only comments while reading a file including line break characters.

How do I filter only comments and still keep Line breaks at the end of the line!? This is one of the common tasks we all do,, How can we do this in a right way..!? I try to ignore empty lines and commented lines using following approach. # \040 --> SPACE character octal... (17 Replies)
Discussion started by: kchinnam
17 Replies

10. Shell Programming and Scripting

Sed script, changing all C-comments to C++-comments

I must write a script to change all C++ like comments: // this is a comment to this one /* this is a comment */ How to do it by sed? With file: #include <cstdio> using namespace std; //one // two int main() { printf("Example"); // three }//four the result should be: (2 Replies)
Discussion started by: black_hawk
2 Replies
Login or Register to Ask a Question