Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Read .txt file and dropping lines starting with #

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 11-02-2011
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is online now Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 4 July 2015, 2:56 PM EDT
Location: Foxborough, MA
Posts: 7,825
Thanks: 174
Thanked 653 Times in 610 Posts

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

Sponsored Links
    #3  
Old Unix and Linux 11-02-2011
greet_sed greet_sed is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 6 April 2015, 4:47 PM EDT
Posts: 124
Thanks: 20
Thanked 33 Times in 32 Posts
Another way:

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


Code:
grep -v '^#' infile

    #4  
Old Unix and Linux 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 Unix and Linux 11-02-2011
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is online now Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 4 July 2015, 2:56 PM EDT
Location: Foxborough, MA
Posts: 7,825
Thanks: 174
Thanked 653 Times in 610 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 Unix and Linux 11-02-2011
rbatte1 rbatte1 is offline Forum Staff  
Moderator
 
Join Date: Jun 2007
Last Activity: 3 July 2015, 9:24 AM EDT
Location: Lancashire, UK
Posts: 2,334
Thanks: 783
Thanked 410 Times in 367 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 03:00 PM.