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
# 8  
Old 07-04-2017
Hello batchenr,

Could you please try following and let me know if this helps.
Code:
awk '!/^#/ && !/^devpts/ && !/^sysfs/ && !/^proc/ && !/^$/ && !/^\// && !/swap/{print $2}'   Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 9  
Old 07-04-2017
Quote:
Originally Posted by RavinderSingh13
Hello batchenr,

Could you please try following and let me know if this helps.
Code:
awk '!/^#/ && !/^devpts/ && !/^sysfs/ && !/^proc/ && !/^$/ && !/^\// && !/swap/{print $2}'   Input_file

Thanks,
R. Singh
Yes! thanks Smilie
# 10  
Old 07-04-2017
Quote:
Originally Posted by batchenr
it gives me all the # lines and after
i need after # (without showing the #)
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:

Code:
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:

Code:
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

Last edited by bakunin; 07-04-2017 at 11:43 PM.. Reason: corrected typo, thanks to RudiC for spotting
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