I have a file that contains lines with various alphanumeric strings that are separated with space-dash-space. However, there are lines where only one alphanumeric value and a separator are present but the other string value is missing. Example:

MN_L_DAX-NORDNET_D36 - DK0060975886
MN_L_DAX-NORDNET_D35 - DK0060975613
DK0060056323-DKK -
DK0060186294-DKK -

The last two lines in the above sample lack alphanumeric string on the right-hand side of the separator and I want to remove them. I tried to use the following awk expression:

awk '!/* - /' sourcefile.txt > temp.txt && mv temp.txt sourcefile.txt

By !/* - / I mean "look for pattern where after any character (*) there is a space-dash-space ( - ) and nothing afterwards, and if no such pattern is found (!)" then move the rest of the lines to temp.txt file and and replace the sourcefile.txt contents with temp.txt contents. However, when I run the above awk script, nothing happens with neither sourcefile.txt contents - everything remains the same. I get no error either. What could be wrong here? Given the above sample lines the desired output after running awk script should look like this:

MN_L_DAX-NORDNET_D36 - DK0060975886
MN_L_DAX-NORDNET_D35 - DK0060975613
  • 1
    Try this awk '!/^.* - /' if I understand you correctly, or awk '!/^.* -$/' if I don't. Mar 30, 2020 at 17:47

3 Answers 3


To print lines that end in an alphanumeric character is simply:

$ awk '/[[:alnum:]]$/' file
MN_L_DAX-NORDNET_D36 - DK0060975886
MN_L_DAX-NORDNET_D35 - DK0060975613

Even simpler:

awk '$3' inputfile

This is shorthand for awk '$3!=""', which is shorthand for awk '$3!=""{print}', which is short for awk '$3!=""{print $0}'

For the record:

sed /-$/d is probably faster, and grep -ve '-$' even faster still.

  • Using $3 as the condition will also evaluate to false if $3 is numerically equivalent to zero. e.g. this won't print anything: echo 1 2 000.00000 | awk '$3'
    – guest
    Mar 31, 2020 at 8:20
  • @guest The sample input data, and the accepted answer did seem to hint, that '00.000' or any similar zero-ish value would not be expected valid content for $3. I had debated with myself, whether I should write '$3' checks, if $3 looks "truthy-ish", but then did not want to explain that .. ;) Mar 31, 2020 at 11:03

Thanks to @guillermo chamorro suggestion I was able to achieve the desired output by modifying my script in the following way:

awk '!/^.* -$/' sourcefile.txt > temp.txt && mv temp.txt sourcefile.txt

Thank you Guillermo.

  • 3
    The ^.* part of your regexp is doing nothing useful.
    – Ed Morton
    Mar 30, 2020 at 18:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .