Grep after - til the end of the line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep after - til the end of the line
# 1  
Old 07-03-2017
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

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags (or ICODE-tags) for code and data. Thank you.

Last edited by bakunin; 07-03-2017 at 08:02 AM..
# 2  
Old 07-03-2017
Quote:
Originally Posted by batchenr
usually ill do cat /etc/somthing | grep -A999999 #
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:

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

Your example above would be:

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

I hope this helps.

bakunin
# 3  
Old 07-03-2017
Quote:
Originally Posted by bakunin
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:

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

Your example above would be:

Code:
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
# 4  
Old 07-03-2017
Quote:
Originally Posted by batchenr
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.
Code:
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.
Code:
awk '/^#/{sub(/^#*/,"");print}'   Input_file

Output will be as follows.
Code:
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
# 5  
Old 07-04-2017
Hey thanks
my output file looks like this :

it is fstab
Code:
#
# /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 :
Code:
cat /etc/fstab  | awk '{print$1,$2}' | sed '/^#.*/d'| grep -v "devpts\|sysfs\|proc" | awk '{print$2}' | grep ^/

for seeing this :
Code:
/
/boot
/dev/shm
/var/spool/asterisk/monitor


thanks anyways!
# 6  
Old 07-04-2017
Hello batchenr,

You could use following command to achieve the same in a single awk, let meknow if this helps you.
Code:
awk '!/^#/ && !/^devpts/ && !/^sysfs/ && !/^proc/ && !/^$/ && !/^\//{print $2}'   /etc/fstab

Thanks,
R. Singh
# 7  
Old 07-04-2017
almost Smilie
yours give extra file that i dont need - swap

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

my output :
Code:
/
/boot
/dev/shm
/var/spool/asterisk/monitor

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search for word in huge logfile and need to continue to print few lines from that line til find date

Guys i need an idea for one logic..in shell scripting am struggling with a logic...So the thing is... i need to search for a word in a huge log file and i need to continue to print few more lines from that line and the consecutive line has to end when it finds the line with date..because i know... (1 Reply)
Discussion started by: Prathi
1 Replies

2. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

3. SCO

Grep to ignore suffix & find end of line

In COBOL, a hyphen can be used in a field name and in a specific program some field names would be identical to others except a suffix was added--sometimes a suffix to a suffix was used. For example, assume I am looking for AAA, AAA-BBB, and AAA-BBB-CCC and don't want to look at AAA-BBB-CCC... (7 Replies)
Discussion started by: wbport
7 Replies

4. Shell Programming and Scripting

Grep start and end line of each segments in a file

Cat file1 -------- ---------- SCHEMA.TABLE1 insert------- update----- ------------- ---------- SCHEMA.TABLE2 insert------- update----- ----------- ------------ SCHEMA.TABLE3 insert------- update----- ------------ grep -n SCHEMA > header_file2.txt (2 Replies)
Discussion started by: Veera_V
2 Replies

5. UNIX for Dummies Questions & Answers

Grep lines with numbers greater than 2 digits at the end of the line

I'm trying to grep lines where the digits at the end of each line are greater than digits. Tried this but it will only allow me to specify 2 digits. Any ideas would greatly be appreciated. grep -i '\<\{3,4,5\}\>' file ---------- Post updated at 05:58 PM ---------- Previous update was at 05:41... (1 Reply)
Discussion started by: jimmyf
1 Replies

6. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

7. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

8. Shell Programming and Scripting

AWK-grep from line number to the end of file

Does anyone know how to use awk to act like grep from a particular line number to the end of file? I am using Solaris 10 and I don't have any GNU products installed. Say I want to print all occurrences of red starting at line 3 to the end of file. EXAMPLE FILE: red green red red... (1 Reply)
Discussion started by: thibodc
1 Replies

9. Shell Programming and Scripting

Grep from a starting line till the end of the file

Hi Folks, I got to know from this forums on how to grep from a particular line say line 6 awk 'NR==6 {print;exit}' But how do i grep from line 6 till the end of the file or command output. Thanks, (3 Replies)
Discussion started by: Mr. Zer0
3 Replies

10. Shell Programming and Scripting

a better way to grep until end of error message, although most seem to be 1 or 2 line

My file creates an output log after which includes a few sql queries. I segregate them into warnings and errors and then get a total count. The errors' and warnings' lines always start with SQL{4} followed by the details of the error. This is what im doing as o now... errors=`grep -A 1 -E... (11 Replies)
Discussion started by: VGR
11 Replies
Login or Register to Ask a Question