Read .txt file and dropping lines starting with # | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Read .txt file and dropping lines starting with #

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-02-2011
bghosh bghosh is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 30 September 2013, 6:28 AM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Read .txt file and dropping lines starting with #

Hi All,

I have a .txt file with some contents as below:


Code:
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.
Moderator's Comments:
Please use code tags when posting data and code samples!

Last edited by vgersh99; 11-02-2011 at 08:06 AM.. Reason: code tags, please!
Sponsored Links
    #2  
Old 11-02-2011
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 23 July 2014, 1:20 AM EDT
Location: Foxborough, MA
Posts: 7,644
Thanks: 150
Thanked 577 Times in 542 Posts

Code:
sed '/^#/d' myFile.txt

Sponsored Links
    #3  
Old 11-02-2011
greet_sed greet_sed is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 5 July 2014, 5:50 AM EDT
Posts: 124
Thanks: 20
Thanked 33 Times in 32 Posts
Another way:

Code:
sed -n '/^#/!p' infile


Code:
grep -v '^#' infile

    #4  
Old 11-02-2011
bghosh bghosh is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 30 September 2013, 6:28 AM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
code to delete lines starting with any special characters or blank lines

Hi,

Thanks for the quick reply.
As posted earlier,
I need to delete all the blanks lines and any line staring with a special character such as # or , or _
Sponsored Links
    #5  
Old 11-02-2011
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 23 July 2014, 1:20 AM EDT
Location: Foxborough, MA
Posts: 7,644
Thanks: 150
Thanked 577 Times in 542 Posts
Quote:
Originally Posted by bghosh View Post
Hi,

Thanks for the quick reply.
As posted earlier,
I need to delete all the blanks lines and any line staring with a special character such as # or , or _
Where was it posted earlier? I don't see it.
You can modify any of the solutions to adjust for your new requirements.
Sponsored Links
    #6  
Old 11-02-2011
rbatte1 rbatte1 is offline Forum Staff  
Moderator
 
Join Date: Jun 2007
Last Activity: 22 July 2014, 4:22 PM EDT
Location: Lancashire, UK
Posts: 1,600
Thanks: 508
Thanked 271 Times in 243 Posts
How about this:-
Code:
egrep -v "^#|^_|^$|^ *$" filename

To explain, this will get all lines that do not match any of the expression. The expression is explained as:-

Quote:
" - start of expression
^# - match lines starting with #
| - or
^_ - match lines starting with _
| - or
^$ - match empty lines
| - or
^ *$ - lines contianing only spaces
" - end of expression
You could then append either a redirect to create a new file (don't try to overwrite the source file) or perhaps a pipe to another process, e.g.

Code:
egrep -v "^#|^_|^$|^ *$" filename|while read line
do
   echo "I've got a line to do something whizzy with"
done


I hope that this helps, but please post back if I've missed the point.




Robin
Liverpool/Blackburn
UK
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Concatenate lines between lines starting with a specific pattern s052866 Shell Programming and Scripting 9 10-24-2010 12:09 PM
Read the lines without starting with # Kattoor Shell Programming and Scripting 5 07-23-2010 05:24 AM
How to print the number of lines from a file, the starting string should be passed` little_wonder Shell Programming and Scripting 3 05-13-2009 01:24 PM
shell script to remove all lines from a file before a line starting with pattern raksha.s Shell Programming and Scripting 2 03-29-2009 07:13 AM
Sed command dropping last record in File bheeke Shell Programming and Scripting 5 09-11-2008 03:41 PM



All times are GMT -4. The time now is 01:35 AM.