Read the lines without starting with #


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read the lines without starting with #
# 1  
Old 07-22-2010
Read the lines without starting with #

Hi,

I need to read one file which excludes the line starting with #.

example file:
Code:
#Working directory 
WORK_DIR|/home/mypath
#Remote directory
REMOTE_DIR|/home/remote

I am reading this file with the following code,
Code:
 while read line;
 do
    KEY=`echo "$line" | cut -d "|" -f 1`
    if [ $KEY = WORK_DIR ]
    then
          WORK_DIR=`echo "$line" | grep $KEY | cut -d "|" -f 2`
    elif [ $KEY =  REMOTE_DIR ]
    then
          REMOTE_DIR=`echo "$line" | grep $KEY | cut -d "|" -f 2`
    fi

But here how can I excludes the lines starting with "#".

Can anybody help me out.

Thanks in advance.

Last edited by Franklin52; 07-22-2010 at 10:04 AM.. Reason: Please use code tags
# 2  
Old 07-22-2010
You can exclude those lines with:
Code:
grep -v "^#" file | while read line
do
  ...
done

# 3  
Old 07-22-2010
Try:

Code:
awk -F"|" '!/^#/ { print $2}' < file

# 4  
Old 07-23-2010
Thanks a lot for your valuable reply..!! Smilie
# 5  
Old 07-23-2010
Code:
sed -e '/^#/d' -e 's/.*|//' file

# 6  
Old 07-23-2010
Code:
#!/bin/bash

while read LINE
do
  case ${LINE:0:1} in
    "W") WD=$( echo $LINE | cut -d'|' -f2 ) ;;
    "R") RD=$( echo $LINE | cut -d'|' -f2 ) ;;
    "#") continue ;;
  esac
done < data

exit 0
#finis

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Extract between lines starting with and

I have a combination of patterns to search. file.txt contains below: H2016-02-10 A74867712 I1556539758 Xjdflk534jfl W0000055722327732 W0000056029009389 A74867865 I1556536434 W0000055822970840 W0000055722325916 A74868015 I1556541270 C0000055928920421 E lines starting with A are... (5 Replies)
Discussion started by: chakrapani
5 Replies

3. Shell Programming and Scripting

How to read a file starting at certain line number?

I am new to ksh scripts. I would like to be able to read a file line by line from a certain line number. I have a specific line number saved in a variable, say $lineNumber. How can I start reading the file from the line number saved in $lineNumber? Thanks! (4 Replies)
Discussion started by: dcowboys13
4 Replies

4. Shell Programming and Scripting

Return lines except those with starting with #, A and *

OS: RHEL 5.8 I have a text file like below $ cat mylist.txt # Multiple entries starting June 1, 2013 # # #AGENT TOUK1TOY1 TOUK1GTF1 ROUK1MET1 TOUK1GTF TOUK2TOY1 TOUK2GTF EOUK2GTF1 WOUK1RTO TOUK1RTO1 GOUK2RTO KOUK2RTO1 (2 Replies)
Discussion started by: John K
2 Replies

5. UNIX for Advanced & Expert Users

Read file and skip the line starting with #

Hi all, I'm new in unix. Need some help here. I have a file called server.cfg which contains the servers name, if I don't want to run on that server, I'll put a "#" infront it. username1@hostname.com username2@hostname.com #username3@hostname.com #username4@hostname.com... (17 Replies)
Discussion started by: beezy
17 Replies

6. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

7. Shell Programming and Scripting

Read .txt file and dropping lines starting with #

Hi All, I have a .txt file with some contents as below: Hi How are you? # Fine and you? I want a script file which reads the .txt file and output the lines which does not start with #. Hi How are you? Help is highly appreciated. Please use code tags when posting data and... (5 Replies)
Discussion started by: bghosh
5 Replies

8. Shell Programming and Scripting

Remove all lines except lines starting with [

Hello, I am trying to remove all the lines in file except lines starting with [ How can i accomplish this? Thank you very much in advance. (4 Replies)
Discussion started by: maxo
4 Replies

9. Shell Programming and Scripting

Concatenate lines between lines starting with a specific pattern

Hi, I have a file such as: --- >contig00001 length=35524 numreads=2944 gACGCCGCGCGCCGCGGCCAGGGCTGGCCCA CAGGCCGCGCGGCGTCGGCTGGCTGAG >contig00002 length=4242 numreads=43423 ATGCCGAAGGTCCGCCTGGGGCTGG CGCCGGGAGCATGTAGCG --- I would like to concatenate the lines not starting with ">"... (9 Replies)
Discussion started by: s052866
9 Replies

10. Shell Programming and Scripting

Delete lines starting with XX or YY or ZZ or ....

Hi There! My final task for today is to delete lines starting with certain numbers for e.g., my text block is and i want to delete all lines starting with 11 or 17 or 21 I know i can use multiple sed commands like sed '/^11,/d' <filename> sed '/^17,/d' <filename> sed '/^21,/d'... (2 Replies)
Discussion started by: orno
2 Replies
Login or Register to Ask a Question