Shell: Check line format file and loop over file lines
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I need to check 2 assetions:
- The file content format
- Iterate over that content:
So, the content file must be:
key1=value
key2=value2
...
So, each line must have the format key=value
. Spaces are not allowed before or after the =
. This file content has to be an env format file.
By other hand, I need to split out each line into a key
and a value
inside a loop.
Any ideas?
shell-script text-processing
add a comment |Â
up vote
1
down vote
favorite
I need to check 2 assetions:
- The file content format
- Iterate over that content:
So, the content file must be:
key1=value
key2=value2
...
So, each line must have the format key=value
. Spaces are not allowed before or after the =
. This file content has to be an env format file.
By other hand, I need to split out each line into a key
and a value
inside a loop.
Any ideas?
shell-script text-processing
1
What are the restrictions on the key and value? Single words? Are quoted strings ok? Are multi-line value ok? Are values containing=
ok?
â Kusalananda
Jun 26 at 14:26
Only has to match<word>=<word>
â Jordi
Jun 26 at 14:42
Right, but can a "word" bepreference=apple-juice
for a valid line readingdrink=preference=apple-juice
? Are spaces allowed before and/or after the=
?
â DopeGhoti
Jun 26 at 15:26
No. This file content has to be an env format file.
â Jordi
Jun 26 at 15:27
Do you mean thatkey1=value with spaces
should be rejected, or that it should be parsed as key=key1
and value=value with spaces
?
â Gilles
Jun 27 at 15:28
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I need to check 2 assetions:
- The file content format
- Iterate over that content:
So, the content file must be:
key1=value
key2=value2
...
So, each line must have the format key=value
. Spaces are not allowed before or after the =
. This file content has to be an env format file.
By other hand, I need to split out each line into a key
and a value
inside a loop.
Any ideas?
shell-script text-processing
I need to check 2 assetions:
- The file content format
- Iterate over that content:
So, the content file must be:
key1=value
key2=value2
...
So, each line must have the format key=value
. Spaces are not allowed before or after the =
. This file content has to be an env format file.
By other hand, I need to split out each line into a key
and a value
inside a loop.
Any ideas?
shell-script text-processing
edited Jun 27 at 15:28
Gilles
502k1179901515
502k1179901515
asked Jun 26 at 14:15
Jordi
1424
1424
1
What are the restrictions on the key and value? Single words? Are quoted strings ok? Are multi-line value ok? Are values containing=
ok?
â Kusalananda
Jun 26 at 14:26
Only has to match<word>=<word>
â Jordi
Jun 26 at 14:42
Right, but can a "word" bepreference=apple-juice
for a valid line readingdrink=preference=apple-juice
? Are spaces allowed before and/or after the=
?
â DopeGhoti
Jun 26 at 15:26
No. This file content has to be an env format file.
â Jordi
Jun 26 at 15:27
Do you mean thatkey1=value with spaces
should be rejected, or that it should be parsed as key=key1
and value=value with spaces
?
â Gilles
Jun 27 at 15:28
add a comment |Â
1
What are the restrictions on the key and value? Single words? Are quoted strings ok? Are multi-line value ok? Are values containing=
ok?
â Kusalananda
Jun 26 at 14:26
Only has to match<word>=<word>
â Jordi
Jun 26 at 14:42
Right, but can a "word" bepreference=apple-juice
for a valid line readingdrink=preference=apple-juice
? Are spaces allowed before and/or after the=
?
â DopeGhoti
Jun 26 at 15:26
No. This file content has to be an env format file.
â Jordi
Jun 26 at 15:27
Do you mean thatkey1=value with spaces
should be rejected, or that it should be parsed as key=key1
and value=value with spaces
?
â Gilles
Jun 27 at 15:28
1
1
What are the restrictions on the key and value? Single words? Are quoted strings ok? Are multi-line value ok? Are values containing
=
ok?â Kusalananda
Jun 26 at 14:26
What are the restrictions on the key and value? Single words? Are quoted strings ok? Are multi-line value ok? Are values containing
=
ok?â Kusalananda
Jun 26 at 14:26
Only has to match
<word>=<word>
â Jordi
Jun 26 at 14:42
Only has to match
<word>=<word>
â Jordi
Jun 26 at 14:42
Right, but can a "word" be
preference=apple-juice
for a valid line reading drink=preference=apple-juice
? Are spaces allowed before and/or after the =
?â DopeGhoti
Jun 26 at 15:26
Right, but can a "word" be
preference=apple-juice
for a valid line reading drink=preference=apple-juice
? Are spaces allowed before and/or after the =
?â DopeGhoti
Jun 26 at 15:26
No. This file content has to be an env format file.
â Jordi
Jun 26 at 15:27
No. This file content has to be an env format file.
â Jordi
Jun 26 at 15:27
Do you mean that
key1=value with spaces
should be rejected, or that it should be parsed as key=key1
and value=value with spaces
?â Gilles
Jun 27 at 15:28
Do you mean that
key1=value with spaces
should be rejected, or that it should be parsed as key=key1
and value=value with spaces
?â Gilles
Jun 27 at 15:28
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
With bash:
n=0
while IFS="=" read -r key value; do
((n++))
if [[ -z $key ]]; then echo "missing key on line $n"; continue; fi
if [[ -z $value ]]; then echo "empty value on line $n"; continue; fi
echo "key:>$key<; value:>$value<"
done <<END
key1=value1
key2=value2
key3=
=value3
foo
key4=value4=value5=value6
END
outputs
key:>key1<; value:>value1<
key:>key2<; value:>value2<
empty value on line 3
missing key on line 4
empty value on line 5
missing key on line 6
key:>key4<; value:>value4=value5=value6<
add a comment |Â
up vote
2
down vote
Given this file as input
:
key1=value1
key2=value2
key3=
=value3
key4=value4
We can use =
as a field separator for awk
:
$ awk -F= '!($1 && $2 && NF==2) print "File failed validation on line " NR; exit 1 print $1, $2 ' input
key1 value1
key2 value2
File failed validation on line 3
To throw validation error messages to standard error, the print
statement can be modified, and to continue processing after an error is seen, change the exit
statement to a next
statement:
$ awk -F= '!($1 && $2 && NF==2) "cat 1>&2"; next print $1, $2 ' input
[stdout] key1 value1
[stdout] key2 value2
[stdout] key4 value4
[stderr] File failed validation on line 3
[stderr] File failed validation on line 4
Validation is done in three steps; if any of these criteria are not met the error is thrown and, in the first example, execution is aborted with an error-state exit code:
$1
- Ensure that there is (in the parlance of the input specification) a "key"$2
- Ensure that there is (in the parlance of the input specification) a "value"NF==2
- Ensure that there are only two fields; the aforementioned "key" and "value"
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
With bash:
n=0
while IFS="=" read -r key value; do
((n++))
if [[ -z $key ]]; then echo "missing key on line $n"; continue; fi
if [[ -z $value ]]; then echo "empty value on line $n"; continue; fi
echo "key:>$key<; value:>$value<"
done <<END
key1=value1
key2=value2
key3=
=value3
foo
key4=value4=value5=value6
END
outputs
key:>key1<; value:>value1<
key:>key2<; value:>value2<
empty value on line 3
missing key on line 4
empty value on line 5
missing key on line 6
key:>key4<; value:>value4=value5=value6<
add a comment |Â
up vote
3
down vote
accepted
With bash:
n=0
while IFS="=" read -r key value; do
((n++))
if [[ -z $key ]]; then echo "missing key on line $n"; continue; fi
if [[ -z $value ]]; then echo "empty value on line $n"; continue; fi
echo "key:>$key<; value:>$value<"
done <<END
key1=value1
key2=value2
key3=
=value3
foo
key4=value4=value5=value6
END
outputs
key:>key1<; value:>value1<
key:>key2<; value:>value2<
empty value on line 3
missing key on line 4
empty value on line 5
missing key on line 6
key:>key4<; value:>value4=value5=value6<
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
With bash:
n=0
while IFS="=" read -r key value; do
((n++))
if [[ -z $key ]]; then echo "missing key on line $n"; continue; fi
if [[ -z $value ]]; then echo "empty value on line $n"; continue; fi
echo "key:>$key<; value:>$value<"
done <<END
key1=value1
key2=value2
key3=
=value3
foo
key4=value4=value5=value6
END
outputs
key:>key1<; value:>value1<
key:>key2<; value:>value2<
empty value on line 3
missing key on line 4
empty value on line 5
missing key on line 6
key:>key4<; value:>value4=value5=value6<
With bash:
n=0
while IFS="=" read -r key value; do
((n++))
if [[ -z $key ]]; then echo "missing key on line $n"; continue; fi
if [[ -z $value ]]; then echo "empty value on line $n"; continue; fi
echo "key:>$key<; value:>$value<"
done <<END
key1=value1
key2=value2
key3=
=value3
foo
key4=value4=value5=value6
END
outputs
key:>key1<; value:>value1<
key:>key2<; value:>value2<
empty value on line 3
missing key on line 4
empty value on line 5
missing key on line 6
key:>key4<; value:>value4=value5=value6<
answered Jun 26 at 16:55
glenn jackman
45.6k264100
45.6k264100
add a comment |Â
add a comment |Â
up vote
2
down vote
Given this file as input
:
key1=value1
key2=value2
key3=
=value3
key4=value4
We can use =
as a field separator for awk
:
$ awk -F= '!($1 && $2 && NF==2) print "File failed validation on line " NR; exit 1 print $1, $2 ' input
key1 value1
key2 value2
File failed validation on line 3
To throw validation error messages to standard error, the print
statement can be modified, and to continue processing after an error is seen, change the exit
statement to a next
statement:
$ awk -F= '!($1 && $2 && NF==2) "cat 1>&2"; next print $1, $2 ' input
[stdout] key1 value1
[stdout] key2 value2
[stdout] key4 value4
[stderr] File failed validation on line 3
[stderr] File failed validation on line 4
Validation is done in three steps; if any of these criteria are not met the error is thrown and, in the first example, execution is aborted with an error-state exit code:
$1
- Ensure that there is (in the parlance of the input specification) a "key"$2
- Ensure that there is (in the parlance of the input specification) a "value"NF==2
- Ensure that there are only two fields; the aforementioned "key" and "value"
add a comment |Â
up vote
2
down vote
Given this file as input
:
key1=value1
key2=value2
key3=
=value3
key4=value4
We can use =
as a field separator for awk
:
$ awk -F= '!($1 && $2 && NF==2) print "File failed validation on line " NR; exit 1 print $1, $2 ' input
key1 value1
key2 value2
File failed validation on line 3
To throw validation error messages to standard error, the print
statement can be modified, and to continue processing after an error is seen, change the exit
statement to a next
statement:
$ awk -F= '!($1 && $2 && NF==2) "cat 1>&2"; next print $1, $2 ' input
[stdout] key1 value1
[stdout] key2 value2
[stdout] key4 value4
[stderr] File failed validation on line 3
[stderr] File failed validation on line 4
Validation is done in three steps; if any of these criteria are not met the error is thrown and, in the first example, execution is aborted with an error-state exit code:
$1
- Ensure that there is (in the parlance of the input specification) a "key"$2
- Ensure that there is (in the parlance of the input specification) a "value"NF==2
- Ensure that there are only two fields; the aforementioned "key" and "value"
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Given this file as input
:
key1=value1
key2=value2
key3=
=value3
key4=value4
We can use =
as a field separator for awk
:
$ awk -F= '!($1 && $2 && NF==2) print "File failed validation on line " NR; exit 1 print $1, $2 ' input
key1 value1
key2 value2
File failed validation on line 3
To throw validation error messages to standard error, the print
statement can be modified, and to continue processing after an error is seen, change the exit
statement to a next
statement:
$ awk -F= '!($1 && $2 && NF==2) "cat 1>&2"; next print $1, $2 ' input
[stdout] key1 value1
[stdout] key2 value2
[stdout] key4 value4
[stderr] File failed validation on line 3
[stderr] File failed validation on line 4
Validation is done in three steps; if any of these criteria are not met the error is thrown and, in the first example, execution is aborted with an error-state exit code:
$1
- Ensure that there is (in the parlance of the input specification) a "key"$2
- Ensure that there is (in the parlance of the input specification) a "value"NF==2
- Ensure that there are only two fields; the aforementioned "key" and "value"
Given this file as input
:
key1=value1
key2=value2
key3=
=value3
key4=value4
We can use =
as a field separator for awk
:
$ awk -F= '!($1 && $2 && NF==2) print "File failed validation on line " NR; exit 1 print $1, $2 ' input
key1 value1
key2 value2
File failed validation on line 3
To throw validation error messages to standard error, the print
statement can be modified, and to continue processing after an error is seen, change the exit
statement to a next
statement:
$ awk -F= '!($1 && $2 && NF==2) "cat 1>&2"; next print $1, $2 ' input
[stdout] key1 value1
[stdout] key2 value2
[stdout] key4 value4
[stderr] File failed validation on line 3
[stderr] File failed validation on line 4
Validation is done in three steps; if any of these criteria are not met the error is thrown and, in the first example, execution is aborted with an error-state exit code:
$1
- Ensure that there is (in the parlance of the input specification) a "key"$2
- Ensure that there is (in the parlance of the input specification) a "value"NF==2
- Ensure that there are only two fields; the aforementioned "key" and "value"
edited Jun 26 at 15:43
answered Jun 26 at 15:37
DopeGhoti
39.7k54779
39.7k54779
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452028%2fshell-check-line-format-file-and-loop-over-file-lines%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
What are the restrictions on the key and value? Single words? Are quoted strings ok? Are multi-line value ok? Are values containing
=
ok?â Kusalananda
Jun 26 at 14:26
Only has to match
<word>=<word>
â Jordi
Jun 26 at 14:42
Right, but can a "word" be
preference=apple-juice
for a valid line readingdrink=preference=apple-juice
? Are spaces allowed before and/or after the=
?â DopeGhoti
Jun 26 at 15:26
No. This file content has to be an env format file.
â Jordi
Jun 26 at 15:27
Do you mean that
key1=value with spaces
should be rejected, or that it should be parsed as key=key1
and value=value with spaces
?â Gilles
Jun 27 at 15:28