1

I have a file in the below format:

Name=Dan
Age=45
Name=Alex
Age=35
Cellphone=12345678901
Name=John
Age=
Cellphone=12345678901

This file has got 1000's of data items in above format, Name=Value.

If values are missing, and I want to find these in below output as missing value for Age.

I tried by using the cut command, cut -d “=“ -f2 > some file, then grep for an empty line, grep -n ‘[[:blank:]]’ file, then display the line number in the original file.

But since I have multiple files, this method looks complicated.

Is there any simpler method for this?

  • I did not understand what's the output you want for a single file. Could insert an example? – aborruso Jan 5 '19 at 18:00
0

You could try using this:

cat <filename> | sed "s/ /\n/g" | grep "=$"

This will replace your spaces to new lines (which will not work if you have values contaning spaces) and then look for entries with = followed by line end.

  • 1
    Since there are multiple files and the field name is always the same, it might be useful to use cat -n, which will display the line number. – ChatterOne Jan 2 '19 at 9:32
  • This will also report values that end in =, e.g. key=value=. – Kusalananda Jan 2 '19 at 11:44
0

If you treat your file as delimited, then you can match and output any lines whose second field consists of at most whitespace:

awk -F= '$2 ~ /^[ \t]*$/' file
-1

Using grep with extended regexp:

grep -Eno "[A-Za-z]+=( |$)" /path/to/file

Should print empty keys and corresponding line numbers.

  • Thanks , this is also helpful , above solution worked , thanks – SuS Jan 2 '19 at 8:00
  • This too would disallow values to end in =. – Kusalananda Jan 2 '19 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.