Monday, July 4, 2022

[FIXED] how to make grep ignore first line and process other line

Issue

I need to remove line beginning with '#' in some txt file. but ignoring the first line as it header. how to make grep ignore first lines and remove any line beginning with # for rest of the lines?

cat sample.txt
#"EVENT",VERSION, NAME
1,2,xyz
1,2,abc
1,2,asd
1,2,ert
#"EVENT",VERSION, NAME
1,2,xyz
1,2,abc
1,2,xyz

cat sample.txt | grep -v "^\s*[#\;]\|^\s*$" > "out.txt"

but this removes the header too!


Solution

With sed:

sed '2,${/^#/d}' sample.txt

From second row (2) to last row ($): search (/.../) for rows beginning (^) with # and delete (d) them. Default action of sed is to print current row.

Output:

#"EVENT",VERSION, NAME
1,2,xyz
1,2,abc
1,2,asd
1,2,ert
1,2,xyz
1,2,abc
1,2,xyz


Answered By - Cyrus
Answer Checked By - Cary Denson (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.