Read file and skip the line starting with #


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Read file and skip the line starting with #
# 8  
Old 03-29-2013
Quote:
Originally Posted by hanson44
I think this will skip the servers with the # in front:
Code:
#!/usr/bin/sh
file2put=$1
if [ -f $file2copy ]; then
  echo "File found, preparing to transfer... \n"
  grep -v '^#' server.cfg > valid_servers
  while read line; do
    ssh $line < checkdirectory.sh #another script to check the existing of directory
    scp $file2put $line:~/folder_name/$file2copy
  done < valid_servers
  exit
else
  echo "File does not exist. Exiting..."
  exit 
fi

Hey!!!

Do you really want to create a physical file "valid_servers"? Instead, we can just redirect the output of grep to while loop.

Code:
egrep -v '^#|^$' file1|while read server_name
do
    echo "$server_name"
    # File transfer goes here
done

Cheers,
RangaSmilie
# 9  
Old 03-29-2013
Quote:
Originally Posted by rangarasan
Hey!!!

Do you really want to create a physical file "valid_servers"? Instead, we can just redirect the output of grep to while loop.

Code:
egrep -v '^#|^$' file1|while read server_name
do
    echo "$server_name"
    # File transfer goes here
done

Cheers,
RangaSmilie
Hi Ranga,
Thanks for your reply,
I had tried this before I post my problem here, too bad it doesn't skip any # in front of the line. Smilie

Last edited by beezy; 03-29-2013 at 05:59 AM.. Reason: typo
# 10  
Old 03-29-2013
It's good of you to point that out. I already knew that. I was just trying to present a simple solution.

Normally, I prefer to put the input to the while loop at the end, because it seems cleaner and easier to read. And making a separate file makes the intent more clear. It's a way of self-documentation.

Your suggestion related to the blank line is an improvement.
# 11  
Old 03-29-2013
Quote:
Originally Posted by beezy
Hi Ranga,
Thanks for your reply,
I had tried this before I post my problem here, too bad it doesn't skip any # in front of the line. Smilie
Hey!!!

This will work and look at the highlighted part. before that you have redirected the file to while loop instead of grep.


Code:
egrep -v '^#|^$' server.cfg|while read server_name
do
echo "$server_name"
# File transfer goes here
done

Cheers,
RangaSmilie
# 12  
Old 03-29-2013
Quote:
Originally Posted by rangarasan
Hey!!!

This will work and look at the highlighted part. before that you have redirected the file to while loop instead of grep.


Code:
egrep -v '^#|^$' server.cfg|while read server_name
do
echo "$server_name"
# File transfer goes here
done

Cheers,
RangaSmilie
Yes. I did try the same thing before.
Still the same. Smilie
# 13  
Old 03-29-2013
Quote:
Originally Posted by beezy
Yes. I did try the same thing before.
Still the same. Smilie
Please make a try with this now and then post us.

Cheers,
RangaSmilie

---------- Post updated at 04:15 AM ---------- Previous update was at 04:13 AM ----------

Quote:
Originally Posted by hanson44
It's good of you to point that out. I already knew that. I was just trying to present a simple solution.

Normally, I prefer to put the input to the while loop at the end, because it seems cleaner and easier to read. And making a separate file makes the intent more clear. It's a way of self-documentation.

Your suggestion related to the blank line is an improvement.
Yeah you are right. If the file size is biggy then, you should be rethink of your self documentation!!!Smilie

Cheers,
RangaSmilie
# 14  
Old 03-29-2013
Quote:
Originally Posted by beezy
Hi Ranga,
Thanks for your reply,
I had tried this before I post my problem here, too bad it doesn't skip any # in front of the line. Smilie
Quote:
Originally Posted by beezy
Yes. I did try the same thing before.
Still the same. Smilie
No, you did not. Your original attempt is not the same.

What you did:
Code:
grep -v '^#' | while read line; do ... done < server.cfg

What rangarasan suggested:
Code:
grep -v '^#' server.cfg | while read line; do ... done

There is a big difference. Pipe redirections occur before other redirection operations.

rangarasan is telling grep to read the file. Your original solution has grep reading something else (whatever standard input descriptor it inherited, which could be a teriminal or some other file).

You redirected the file directly to the while-read loop, bypassing grep. Your grep is doing nothing. Even if there is something for it to read on standard input (we can't tell from your script what stdin is, since it depends on how the script is called), there is nowhere for the grep data to go. Why? Because after the pipe connects the standard output of grep to the standard input of the while-read loop, the standard input of the while-read loop is overridden by the redirection operation immediately following the while-loop. So, even if grep does write to the pipe, there is nothing on the other end to read the data.

On a different note, there is no need for grep. Within the while-loop, a case statement can do the job:
Code:
case $line in
    \#*) continue ;;
esac

Or, if the shell supports substring parameter expansion:
Code:
[ "${line:0:1}" = '#' ] && continue

Regards,
Alister

Last edited by alister; 03-29-2013 at 11:43 AM..
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

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
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

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

5. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

6. Shell Programming and Scripting

want to skip a line in XML file using awk

HI All, I am trying to split a xml using awk. now the issue is i want to skip three lines from the xml file. first two and last one based on pattern. plz some one help. i am new to awk and struggling :wall: <?xml version="1.0"?> <notification> ..... ..... ..... ..... ........ (24 Replies)
Discussion started by: ganesan kulasek
24 Replies

7. UNIX for Dummies Questions & Answers

skip first line when doing a read of csv file

Folks, how do i skip the first line in a csv, while doing the read of a csv file in to a variable line by line. eg : do echo $line done < $rpt where rpt is path to csv file The initial 1st line is a garbage that i want to avoid, and start reading from 2nd line ... (2 Replies)
Discussion started by: venu
2 Replies

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

9. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

10. UNIX for Dummies Questions & Answers

How to skip first line from a file while manupulating the file

I need to put single quotes on the columns of a .csv file. The first row contains the column headers. I need to skip the first row and put quotes for rest of the rows. Would please someone help me with this. Thanks JP (4 Replies)
Discussion started by: JPalt
4 Replies
Login or Register to Ask a Question