Grab line regardless of if it ends with tabs or spaces

so i have a data file that has various lines which may or may not end with spaces or tabs.

data.file:

[hello], \t \t
{sample}  <spaces> <spaaces> several more spaces....
{"resemble"}, <nospaces>

Command i'm using:

sed -n 8p data.file | egrep "\],$|\],\ $"

or

egrep "\],$|\],\ $" data.file

basically, i just want to make sure there are no additional characters after the string that i specify to egrep. so in the above code, i'm telling egrep to only grab lines that end in any one of the following:

], <nospaces>
], <spaces>

my problem is, there is no way for me to know what comes after the string i'm searching for. there maybe multiple tabs, there may be no tabs, there may be one tab, there may be no spaces, there may be many spaces, or one. i have no clue.

what i need is to be able to modify the egrep command so that i get an output as long as no other character (except tabs or spaces) comes after the specific characters i provided to egrep.

I have no idea what you were doing with the sed in your pipeline for what you said you were trying to do. Try:

grep '[]],[[:blank:]]*$' file

to print all lines in file that end with a closing square bracket followed by a comma followed by any combination of zero or more space and tab characters. This RE will work with both grep and egrep (AKA grep -E ).

1 Like