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


View Poll Results: Is this useful ?
no 2 100.00%
yes 0 0%
Voters: 2. This poll is closed

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to filter only comments while reading a file including line break characters.
# 15  
Old 05-04-2010
Quote:
Originally Posted by kchinnam
Dan is it possible to trim suffix spaces at the end of the line, i.e after --> 55 ?
Code:
# cat file
# Need to ignore this line
                  # need to ignore this line as well
one='1111111' # we want anthing after # ignored,, but stll keep line break.
two = '22222222'
# Need to remove empty lines && lines with only spaces


         three=333333333
                 four=4444444
  five=5555555  555     55              # Preserve spaces between words, but remove prefix, suffix spaces && comment at the end

# od -c < file
0000000    #       N   e   e   d       t   o       i   g   n   o   r   e
0000020        t   h   i   s       l   i   n   e  \n
0000040                                                        #       n
0000060    e   e   d       t   o       i   g   n   o   r   e       t   h
0000100    i   s       l   i   n   e       a   s       w   e   l   l  \n
0000120    o   n   e   =   '   1   1   1   1   1   1   1   '       #
0000140    w   e       w   a   n   t       a   n   t   h   i   n   g
0000160    a   f   t   e   r       #       i   g   n   o   r   e   d   ,
0000200    ,       b   u   t       s   t   l   l       k   e   e   p
0000220    l   i   n   e       b   r   e   a   k   .  \n   t   w   o
0000240    =       '   2   2   2   2   2   2   2   2   '  \n   #       N
0000260    e   e   d       t   o       r   e   m   o   v   e       e   m
0000300    p   t   y       l   i   n   e   s       &   &       l   i   n
0000320    e   s       w   i   t   h       o   n   l   y       s   p   a
0000340    c   e   s  \n                                          \n
0000360                                   \n  \t       t   h   r   e   e
0000400    =   3   3   3   3   3   3   3   3   3  \n  \t  \t       f   o
0000420    u   r   =   4   4   4   4   4   4   4  \n           f   i   v
0000440    e   =   5   5   5   5   5   5   5           5   5   5  \t   5
0000460    5  \t      \t   #       P   r   e   s   e   r   v   e       s
0000500    p   a   c   e   s       b   e   t   w   e   e   n       w   o
0000520    r   d   s   ,       b   u   t       r   e   m   o   v   e
0000540    p   r   e   f   i   x   ,       s   u   f   f   i   x       s
0000560    p   a   c   e   s       &   &       c   o   m   m   e   n   t
0000600        a   t       t   h   e       e   n   d  \n
0000614

Code:
# awk '$1!="#" && NF{gsub(/^[\t ]*/,x);sub("#.*",x);print}' file
one='1111111'
two = '22222222'
three=333333333
four=4444444
five=5555555  555       55

# awk '$1!="#" && NF{gsub(/^[\t ]*/,x);sub("#.*",x);print}' file | od -c
0000000    o   n   e   =   '   1   1   1   1   1   1   1   '      \n   t
0000020    w   o       =       '   2   2   2   2   2   2   2   2   '  \n
0000040    t   h   r   e   e   =   3   3   3   3   3   3   3   3   3  \n
0000060    f   o   u   r   =   4   4   4   4   4   4   4  \n   f   i   v
0000100    e   =   5   5   5   5   5   5   5           5   5   5  \t   5
0000120    5  \t      \t  \n
0000125

Code:
# awk '$1!="#" && NF{gsub(/^[\t ]*/,x);sub("#.*",x);gsub(/[ \t]*$/,x);print}' file
one='1111111'
two = '22222222'
three=333333333
four=4444444
five=5555555  555       55

# awk '$1!="#" && NF{gsub(/^[\t ]*/,x);sub("#.*",x);gsub(/[ \t]*$/,x);print}' file | od -c
0000000    o   n   e   =   '   1   1   1   1   1   1   1   '  \n   t   w
0000020    o       =       '   2   2   2   2   2   2   2   2   '  \n   t
0000040    h   r   e   e   =   3   3   3   3   3   3   3   3   3  \n   f
0000060    o   u   r   =   4   4   4   4   4   4   4  \n   f   i   v   e
0000100    =   5   5   5   5   5   5   5           5   5   5  \t   5   5
0000120   \n
0000121

Quote:
Originally Posted by kchinnam
Can we adjust the command a little to get that perfection !?
I just ask myself why somebody will ever want to ripoff "perfectly" the comments from a script file Smilie
# 16  
Old 05-04-2010
"alister" you are correct. Thanks for bringing that up..
I know parsing a script file and filtering only comment strings from it would be much more complicated, I will not go there..

Let me make it clear, my requirement is to parse only property files with possible formats like -->

Code:
input.txt
# Syntax1
var_name=var_value
or 
# Syntax2
[([<var_value>][<Field_Seperator>])...]
Ex: <source_path>#<destination_path>#<days_to_archive>#<days_to_purge>#<ignore>..

From what I know so far, if I choose to use Syntax2 from above, I should have a self imposed restriction of

Do not use block comments
Do not use inline comments

Do you agree it would meet a 99% satisfactory solution ?

---------- Post updated at 11:32 PM ---------- Previous update was at 11:12 PM ----------



Quote:
I just ask myself why somebody will ever want to ripoff "perfectly" the comments from a script file
Sorry, my input file is never going to be a script file. Its going to be a file with mostly user defined parameters.

Most of my scripts are either interactive or batch,, Almost all of them with cusomizable values for pre-defined parameters. With '0' hardcoded values in my scripts.

Code:
Ex: JMS QMgr, MQ QMgr Monitoring, CVS interactive, Application deploying, daily archiving, log mointoring etc..

Ex1:
egrep -v "$IN_FILE_FILTER" $JMSPORTS | while IFS="," read PORT QMGR
do
   .....
done

Ex2:
egrep -v "$IN_FILE_FILTER" $LIST | while IFS="#" read SOURCE_DIR DEST_DIR IGNORE_REGEX SRC_KEEP_AGE ARCH_PURGE_AGE; do
 ....
done

If I do not remove comments at the end of line, it may go into my last variable, in case if I want to use --> $@
same is true with spaces at the front or end of line.

Where IN_FILE_FILTER=$(echo "^([\040|\t]*(#|$))").
I am trying to find a better way of filtering comments from my input property files..

---------- Post updated 05-04-10 at 12:51 AM ---------- Previous update was 05-03-10 at 11:32 PM ----------

"danmero",, I just checked your latest command,, its working as I wanted. Many Thanks to you. Thanks to everyone that helped.

Last edited by kchinnam; 05-04-2010 at 12:46 AM.. Reason: formatting again
# 17  
Old 05-04-2010
Quote:
Originally Posted by kchinnam
"danmero",, I just checked your latest command,, its working as I wanted. Many Thanks to you. Thanks to everyone that helped.
Perfect, you can edit the first post in thread and add [Solved] in title.
# 18  
Old 05-04-2010
MySQL

Sed method
Code:
[root@sistem1lnx ~]# cat input
# Need to ignore this line
                  # need to ignore this line as well
one='1111111' # we want anthing after # ignored,, but stll keep line break.
two = '22222222'
 
         three=333333333
                   four=4444444
  five=5555555

Code:
[root@sistem1lnx ~]# sed -e 's/\(.*\)\#.*#.*/\1/' -e '/#/d' -e 's/^  *//g' -e '/^$/d' input
one='1111111'
two = '22222222'
three=333333333
four=4444444
five=5555555

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Including Hash / in sed command filter

Hello All, I want to print data in between two lines in a file sample.txt through more or cat command on the screen. For that I am using below sed command to give the BEGIN and END text. Content of sample.txt server01:~ # cat /proc/mdstat Hello this is a text message 1 Hello this is a... (5 Replies)
Discussion started by: Xtreme
5 Replies

2. UNIX for Dummies Questions & Answers

add a string to a file without line break

I searched and found "echo -n" and "printf" are solution for this, but they are not here: $ echo "hello" >> test $ cat test hello $ echo -n "world" >> test $ cat test hello world $ echo -n " seriously?" >> test $ cat test hello world seriously? This is not successful... (15 Replies)
Discussion started by: stunn3r
15 Replies

3. Shell Programming and Scripting

Add line break for each line in a file

I cannot seem to get this to work.. I have a file which has about 100 lines, and there is no end of line (line break \n) at the end of each line, and this is causing problem when i paste them into an application. the file looks like this this is a test that is a test balblblablblhblbha... (1 Reply)
Discussion started by: fedora
1 Replies

4. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

5. Shell Programming and Scripting

filter record from a file reading another file

Hi, I want to filter record from a file if the records in the second column matches the data in another file. I tried the below awk command but it filters the records in the filter file. I want the opposite, to include only the records in the filter file. I tried this: awk -F'|'... (8 Replies)
Discussion started by: gpaulose
8 Replies

6. Shell Programming and Scripting

Break line after last "/" if length > X characters

Hello. I am a french newbie in unix shell scripting (sorry if my english speaking is wrong). I have a file with path and filenames in it. I want to limit the number of characters on each line and break the line if necessary. But the "break" should occur after a slash caracter "/". Example of... (9 Replies)
Discussion started by: SportBilly
9 Replies

7. UNIX for Dummies Questions & Answers

Reading a line including spaces

Hi All, I have a script that reads a file and echo it back to std out. Test.txt 1aaaaaaaaaaa . The script is ReadLine.sh #!/bin/ksh cat $1 | while read file do echo $file done I invoke the script as ReadLine.sh Test.txt The output that I get is (1 Reply)
Discussion started by: aksarben
1 Replies

8. Shell Programming and Scripting

Replacing characters in file with line break

Hi, Apologies if this has been asked before, but I searched and was not able to find an answer. It's probably a simple question to answer for those of you with some experience, though... I have a relatively long string where tokens are separated by the colon (':') character. Let's say the... (10 Replies)
Discussion started by: johnemb
10 Replies

9. Shell Programming and Scripting

Reading a path (including ref to shell variable) from file

Hi! 1. I have a parameter file containing path to log files. For this example both paths are the same, one is stated directly and the second using env variables. /oracle/admin/orcl/bdump/:atlas:trc:N ${ORACLE_BASE}/admin/${ORACLE_SID}/bdump/:${ORACLE_SID}:trc:N 2. I try to parse the path... (1 Reply)
Discussion started by: lojzev
1 Replies

10. Programming

Reading special characters while converting sequential file to line sequential

We have to convert a sequential file to a 80 char line sequential file (HP UX platform).The sequential file contains special characters. which after conversion of the file to line sequential are getting coverted into "new line" or "tab" and file is getting distorted. Is there any way to read these... (2 Replies)
Discussion started by: Rajeshsu
2 Replies
Login or Register to Ask a Question