Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux, Print all lines in a file, NOT starting with

I would like to print the contents of a file, but all lines starting with # I want to ignore. I was trying some stuff with grep and awk, but it kept printing the whole file, or just printed the lines starting with #. I you could give me a push in the right way, or a grep/awk command that would print anyline in the file that does not start with #.

like image 713
Dasoren Avatar asked Mar 09 '13 22:03

Dasoren


People also ask

How do I print the first 5 lines of a file in Linux?

The head command is used to display the first lines of a file. By default, the head command will print only the first 10 lines. The head command ships with the coreutils package, which might be already installed on our machine.

How do I see all lines in a file in Linux?

The simplest way to view text files in Linux is the cat command. It displays the complete contents in the command line without using inputs to scroll through it. Here is an example of using the cat command to view the Linux version by displaying the contents of the /proc/version file.

How do I show the first 4 lines of a file in Linux?

To look at the first few lines of a file, type head filename, where filename is the name of the file you want to look at, and then press <Enter>. By default, head shows you the first 10 lines of a file. You can change this by typing head -number filename, where number is the number of lines you want to see.


2 Answers

Use the -v option of grep to negate the condition:

grep -v '^#' file
like image 198
choroba Avatar answered Nov 03 '22 01:11

choroba


You can use the ! operator:

awk '!/^ *#/ { print; }'

This negates the result of the match. I also included lines that start with spaces and then #, but you can tailor the regex how you like.

like image 28
FatalError Avatar answered Nov 03 '22 00:11

FatalError