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?

1
  • I did not understand what's the output you want for a single file. Could insert an example? Commented Jan 5, 2019 at 18:00

3 Answers 3

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.

2
  • 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. Commented Jan 2, 2019 at 9:32
  • This will also report values that end in =, e.g. key=value=. Commented Jan 2, 2019 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.

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.