Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Read .txt file and dropping lines starting with #

Shell Programming and Scripting


Closed    
 
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: 20 May 2016, 10:26 AM EDT
Posts: 12
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:
Read .txt file and dropping lines starting with # Please use code tags when posting data and code samples!

Last edited by vgersh99; 11-02-2011 at 09: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 offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 10 December 2016, 3:50 PM EST
Location: Foxborough, MA
Posts: 7,989
Thanks: 195
Thanked 699 Times in 655 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: 10 December 2016, 4:49 PM EST
Posts: 250
Thanks: 46
Thanked 80 Times in 76 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: 20 May 2016, 10:26 AM EDT
Posts: 12
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 offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 10 December 2016, 3:50 PM EST
Location: Foxborough, MA
Posts: 7,989
Thanks: 195
Thanked 699 Times in 655 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: 9 December 2016, 9:44 AM EST
Location: Lancashire, UK
Posts: 2,825
Thanks: 1,110
Thanked 511 Times in 463 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 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 01:09 PM
Read the lines without starting with # Kattoor Shell Programming and Scripting 5 07-23-2010 06: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 02: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 08:13 AM
Sed command dropping last record in File bheeke Shell Programming and Scripting 5 09-11-2008 04:41 PM



All times are GMT -4. The time now is 06:13 PM.