Grep after - til the end of the line

Hi,

i need to cat a file after # till the end of the file
usually ill do cat /etc/somthing | grep -A999999 #

but its not that professional
thanks

The problem is that grep is a line filter: it filters lines with a certain attribute from others. If you want to work context-oriented like in your case, where a line is printed or not depending on where in the file (before or after something) it is, you need another tool: sed for instance.

Try this:

sed -n '/<some-regexp>/,$ p' /path/to/file

Your example above would be:

sed -n '/#/,$ p' /etc/somthing

I hope this helps.

bakunin

it gives me all the # lines and after
i need after # (without showing the #)
only after

thanks you

Hello batchenr,

Please always mention sample Input_file into your posts into code tags so that we could understand the requirement clearly. So I am assuming this could be your possible Input_file by taking some test sample.

cat Input_file
#test1 test2 test3 bla bla
test2 test3 chumma chuma1
##test2 test4 test5 test6
chumma1 test2 chumma3

Now I am running following code.

awk '/^#/{sub(/^#*/,"");print}'   Input_file

Output will be as follows.

test1 test2 test3 bla bla
test2 test4 test5 test6

Now if above code is NOT meeting your requirements then kindly post sample Input_file in code tags and show us sample output too in code tags so that we could understand the question properly. I hope this helps you.

Thanks,
R. Singh

Hey thanks
my output file looks like this :

it is fstab

#
# /etc/fstab
# Created by anaconda on Sun Sep 11 13:08:43 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=39e63188-400e-40ac-9190-a0b4f807db97 /                       ext4    defaults        1 1
UUID=dc97fda0-d440-4a21-af3d-e1b62d339409 /boot                   ext4    defaults        1 2
UUID=f9c167fb-3c4e-429e-94c5-12e55904f01a swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/mnt/pbxarchive/PBXIL-Managment    /var/spool/asterisk/monitor    nfs     defaults        0 0

i ended up using this :

cat /etc/fstab  | awk '{print$1,$2}' | sed '/^#.*/d'| grep -v "devpts\|sysfs\|proc" | awk '{print$2}' | grep ^/

for seeing this :

/
/boot
/dev/shm
/var/spool/asterisk/monitor

thanks anyways!

Hello batchenr,

You could use following command to achieve the same in a single awk , let meknow if this helps you.

awk '!/^#/ && !/^devpts/ && !/^sysfs/ && !/^proc/ && !/^$/ && !/^\//{print $2}'   /etc/fstab

Thanks,
R. Singh

almost :slight_smile:
yours give extra file that i dont need - swap

/
/boot
swap
/dev/shm
/var/spool/asterisk/monitor

my output :

/
/boot
/dev/shm
/var/spool/asterisk/monitor

Hello batchenr,

Could you please try following and let me know if this helps.

awk '!/^#/ && !/^devpts/ && !/^sysfs/ && !/^proc/ && !/^$/ && !/^\// && !/swap/{print $2}'   Input_file

Thanks,
R. Singh

1 Like

Yes! thanks :slight_smile:

OK, now, that we finally have established what exactly you want (a files content with all comments removed), try this, which i use to strip config files before reading:

sed 's/#.*$//;s/^[[:blank:]]*//;s/[[:blank:]]*$//;/^$/d' /input/file

The functionality is:

s/#.*$// remove comment signs and everything after
s/^[[:blank:]]*// remove leading whitespace
s/[[:blank:]]*$// remove trailing whitespace
/^$/d remove empty lines

Note that lines with comments are first made to empty lines by rule one (if the comment is not aligned with the beginning of line than rule 2 still reduces it to empty) and further removed by rule 4, so that they are effectively removed.

Note further, that comment signs will be removed regardless of them being in quotes, being escaped, etc., or not. If you need this functionality you cannot use regexps but have to use a parser.

You can extend this by further adding rules to filter out lines you are not interested in:

sed 's/#.*$//;s/^[[:blank:]]*//;s/[[:blank:]]*$//;/^$/d;/^devpts/d;/swap/d;/^sysfs/d;/^proc/d' /input/file

You should be able to extend this to fit your own needs.

I hope this helps.

bakunin