Shell script: split line

Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I need to split a line with two string separated by an space like: key value.
I've tried:
key=$(awk -FS=" " print $1 line)
value=$(awk -FS=" " print $2 line)
But I'm getting:
awk: line 2: missing } near end of file
Any ideas?
shell-script
add a comment |Â
up vote
3
down vote
favorite
I need to split a line with two string separated by an space like: key value.
I've tried:
key=$(awk -FS=" " print $1 line)
value=$(awk -FS=" " print $2 line)
But I'm getting:
awk: line 2: missing } near end of file
Any ideas?
shell-script
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I need to split a line with two string separated by an space like: key value.
I've tried:
key=$(awk -FS=" " print $1 line)
value=$(awk -FS=" " print $2 line)
But I'm getting:
awk: line 2: missing } near end of file
Any ideas?
shell-script
I need to split a line with two string separated by an space like: key value.
I've tried:
key=$(awk -FS=" " print $1 line)
value=$(awk -FS=" " print $2 line)
But I'm getting:
awk: line 2: missing } near end of file
Any ideas?
shell-script
asked Jun 28 at 7:09
Jordi
1424
1424
add a comment |Â
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
6
down vote
An awk script on the command line should be single quoted:
awk -F ' ' ' print $1 ' filename
Notice the single quotes around ... , and also that the correct way to set FS is through -F, or through -v FS=... or in a BEGIN block, but not with -FS=....
Your command, the way you have written it, also assumes that line is a file name.
If $line is a string with a single space in it, you can separate it into two strings with
first_part=$line% * # removes the space and everything after it
second_part=$line#* # removes the space and everything before it
Likewise, if $line is a string with a = in it:
first_part=$line%=* # removes the = and everything after it
second_part=$line#*= # removes the = and everything before it
add a comment |Â
up vote
3
down vote
You can also use an array for splitting a line on spaces:
if line is a string
arr=($line)
key="$arr[0]"
value="$arr[1]"
Note:- If the first word of $line is * then the arr array will contain
all the filenames in the current directory. So to be on the safe side and avoid such situations , use
set -f; arr=($line); set +f
key="$arr[0]"
value="$arr[1]"
If line is file
while read -r words
do
set -- $words
key=$1
value=$2
done < line
linein the question is a filename.
â Kusalananda
Jun 28 at 7:24
@Kusalananda i thought it was a string, edited my answer for both the situations
â Arushix
Jun 28 at 7:30
In the first snippet, you are leaving the variable unquoted to employ word splitting. However you are also subject to filename expansion. If the first word of $line is*then the arr array will contain all the filenames in the current directory. You will have toset -f; arr=($line); set +f
â glenn jackman
Jun 28 at 11:30
@Kusalananda, I wouldn't assume that it's a file, given the apparent experience level of the question. I would put more trust in the words: "I need to split a line..."
â glenn jackman
Jun 28 at 11:37
@glennjackman thanks for the suggestion, i did not considered that situation, added in my answer too :)
â Arushix
Jun 28 at 12:13
add a comment |Â
up vote
1
down vote
You can easily achieve this, without using awk, that is inteded for more complex data manipulation.cut bash command is all you need.
key="$(echo "$line" | cut -d ' ' -f 1)"
value="$(echo "$line" | cut -d ' ' -f 2)"
Hope it helped.
1
Quote your variables
â glenn jackman
Jun 28 at 11:32
add a comment |Â
up vote
0
down vote
You can use the shell's parameter expansion facilities instead of calling out to an external program:
key=$line%% * # from the end remove the longest string matching
# a space followed by any characters
value=$line#* # from the start remove the shortest string matching
# any characters followed by a space
add a comment |Â
up vote
0
down vote
read
Just use read:
read key value
Everything before the first space on the line goes into key and everything after it (including any additional spaces) goes into value.
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
An awk script on the command line should be single quoted:
awk -F ' ' ' print $1 ' filename
Notice the single quotes around ... , and also that the correct way to set FS is through -F, or through -v FS=... or in a BEGIN block, but not with -FS=....
Your command, the way you have written it, also assumes that line is a file name.
If $line is a string with a single space in it, you can separate it into two strings with
first_part=$line% * # removes the space and everything after it
second_part=$line#* # removes the space and everything before it
Likewise, if $line is a string with a = in it:
first_part=$line%=* # removes the = and everything after it
second_part=$line#*= # removes the = and everything before it
add a comment |Â
up vote
6
down vote
An awk script on the command line should be single quoted:
awk -F ' ' ' print $1 ' filename
Notice the single quotes around ... , and also that the correct way to set FS is through -F, or through -v FS=... or in a BEGIN block, but not with -FS=....
Your command, the way you have written it, also assumes that line is a file name.
If $line is a string with a single space in it, you can separate it into two strings with
first_part=$line% * # removes the space and everything after it
second_part=$line#* # removes the space and everything before it
Likewise, if $line is a string with a = in it:
first_part=$line%=* # removes the = and everything after it
second_part=$line#*= # removes the = and everything before it
add a comment |Â
up vote
6
down vote
up vote
6
down vote
An awk script on the command line should be single quoted:
awk -F ' ' ' print $1 ' filename
Notice the single quotes around ... , and also that the correct way to set FS is through -F, or through -v FS=... or in a BEGIN block, but not with -FS=....
Your command, the way you have written it, also assumes that line is a file name.
If $line is a string with a single space in it, you can separate it into two strings with
first_part=$line% * # removes the space and everything after it
second_part=$line#* # removes the space and everything before it
Likewise, if $line is a string with a = in it:
first_part=$line%=* # removes the = and everything after it
second_part=$line#*= # removes the = and everything before it
An awk script on the command line should be single quoted:
awk -F ' ' ' print $1 ' filename
Notice the single quotes around ... , and also that the correct way to set FS is through -F, or through -v FS=... or in a BEGIN block, but not with -FS=....
Your command, the way you have written it, also assumes that line is a file name.
If $line is a string with a single space in it, you can separate it into two strings with
first_part=$line% * # removes the space and everything after it
second_part=$line#* # removes the space and everything before it
Likewise, if $line is a string with a = in it:
first_part=$line%=* # removes the = and everything after it
second_part=$line#*= # removes the = and everything before it
edited Jun 28 at 13:34
answered Jun 28 at 7:12
Kusalananda
101k13199312
101k13199312
add a comment |Â
add a comment |Â
up vote
3
down vote
You can also use an array for splitting a line on spaces:
if line is a string
arr=($line)
key="$arr[0]"
value="$arr[1]"
Note:- If the first word of $line is * then the arr array will contain
all the filenames in the current directory. So to be on the safe side and avoid such situations , use
set -f; arr=($line); set +f
key="$arr[0]"
value="$arr[1]"
If line is file
while read -r words
do
set -- $words
key=$1
value=$2
done < line
linein the question is a filename.
â Kusalananda
Jun 28 at 7:24
@Kusalananda i thought it was a string, edited my answer for both the situations
â Arushix
Jun 28 at 7:30
In the first snippet, you are leaving the variable unquoted to employ word splitting. However you are also subject to filename expansion. If the first word of $line is*then the arr array will contain all the filenames in the current directory. You will have toset -f; arr=($line); set +f
â glenn jackman
Jun 28 at 11:30
@Kusalananda, I wouldn't assume that it's a file, given the apparent experience level of the question. I would put more trust in the words: "I need to split a line..."
â glenn jackman
Jun 28 at 11:37
@glennjackman thanks for the suggestion, i did not considered that situation, added in my answer too :)
â Arushix
Jun 28 at 12:13
add a comment |Â
up vote
3
down vote
You can also use an array for splitting a line on spaces:
if line is a string
arr=($line)
key="$arr[0]"
value="$arr[1]"
Note:- If the first word of $line is * then the arr array will contain
all the filenames in the current directory. So to be on the safe side and avoid such situations , use
set -f; arr=($line); set +f
key="$arr[0]"
value="$arr[1]"
If line is file
while read -r words
do
set -- $words
key=$1
value=$2
done < line
linein the question is a filename.
â Kusalananda
Jun 28 at 7:24
@Kusalananda i thought it was a string, edited my answer for both the situations
â Arushix
Jun 28 at 7:30
In the first snippet, you are leaving the variable unquoted to employ word splitting. However you are also subject to filename expansion. If the first word of $line is*then the arr array will contain all the filenames in the current directory. You will have toset -f; arr=($line); set +f
â glenn jackman
Jun 28 at 11:30
@Kusalananda, I wouldn't assume that it's a file, given the apparent experience level of the question. I would put more trust in the words: "I need to split a line..."
â glenn jackman
Jun 28 at 11:37
@glennjackman thanks for the suggestion, i did not considered that situation, added in my answer too :)
â Arushix
Jun 28 at 12:13
add a comment |Â
up vote
3
down vote
up vote
3
down vote
You can also use an array for splitting a line on spaces:
if line is a string
arr=($line)
key="$arr[0]"
value="$arr[1]"
Note:- If the first word of $line is * then the arr array will contain
all the filenames in the current directory. So to be on the safe side and avoid such situations , use
set -f; arr=($line); set +f
key="$arr[0]"
value="$arr[1]"
If line is file
while read -r words
do
set -- $words
key=$1
value=$2
done < line
You can also use an array for splitting a line on spaces:
if line is a string
arr=($line)
key="$arr[0]"
value="$arr[1]"
Note:- If the first word of $line is * then the arr array will contain
all the filenames in the current directory. So to be on the safe side and avoid such situations , use
set -f; arr=($line); set +f
key="$arr[0]"
value="$arr[1]"
If line is file
while read -r words
do
set -- $words
key=$1
value=$2
done < line
edited Jun 28 at 12:12
answered Jun 28 at 7:22
Arushix
9968
9968
linein the question is a filename.
â Kusalananda
Jun 28 at 7:24
@Kusalananda i thought it was a string, edited my answer for both the situations
â Arushix
Jun 28 at 7:30
In the first snippet, you are leaving the variable unquoted to employ word splitting. However you are also subject to filename expansion. If the first word of $line is*then the arr array will contain all the filenames in the current directory. You will have toset -f; arr=($line); set +f
â glenn jackman
Jun 28 at 11:30
@Kusalananda, I wouldn't assume that it's a file, given the apparent experience level of the question. I would put more trust in the words: "I need to split a line..."
â glenn jackman
Jun 28 at 11:37
@glennjackman thanks for the suggestion, i did not considered that situation, added in my answer too :)
â Arushix
Jun 28 at 12:13
add a comment |Â
linein the question is a filename.
â Kusalananda
Jun 28 at 7:24
@Kusalananda i thought it was a string, edited my answer for both the situations
â Arushix
Jun 28 at 7:30
In the first snippet, you are leaving the variable unquoted to employ word splitting. However you are also subject to filename expansion. If the first word of $line is*then the arr array will contain all the filenames in the current directory. You will have toset -f; arr=($line); set +f
â glenn jackman
Jun 28 at 11:30
@Kusalananda, I wouldn't assume that it's a file, given the apparent experience level of the question. I would put more trust in the words: "I need to split a line..."
â glenn jackman
Jun 28 at 11:37
@glennjackman thanks for the suggestion, i did not considered that situation, added in my answer too :)
â Arushix
Jun 28 at 12:13
line in the question is a filename.â Kusalananda
Jun 28 at 7:24
line in the question is a filename.â Kusalananda
Jun 28 at 7:24
@Kusalananda i thought it was a string, edited my answer for both the situations
â Arushix
Jun 28 at 7:30
@Kusalananda i thought it was a string, edited my answer for both the situations
â Arushix
Jun 28 at 7:30
In the first snippet, you are leaving the variable unquoted to employ word splitting. However you are also subject to filename expansion. If the first word of $line is
* then the arr array will contain all the filenames in the current directory. You will have to set -f; arr=($line); set +fâ glenn jackman
Jun 28 at 11:30
In the first snippet, you are leaving the variable unquoted to employ word splitting. However you are also subject to filename expansion. If the first word of $line is
* then the arr array will contain all the filenames in the current directory. You will have to set -f; arr=($line); set +fâ glenn jackman
Jun 28 at 11:30
@Kusalananda, I wouldn't assume that it's a file, given the apparent experience level of the question. I would put more trust in the words: "I need to split a line..."
â glenn jackman
Jun 28 at 11:37
@Kusalananda, I wouldn't assume that it's a file, given the apparent experience level of the question. I would put more trust in the words: "I need to split a line..."
â glenn jackman
Jun 28 at 11:37
@glennjackman thanks for the suggestion, i did not considered that situation, added in my answer too :)
â Arushix
Jun 28 at 12:13
@glennjackman thanks for the suggestion, i did not considered that situation, added in my answer too :)
â Arushix
Jun 28 at 12:13
add a comment |Â
up vote
1
down vote
You can easily achieve this, without using awk, that is inteded for more complex data manipulation.cut bash command is all you need.
key="$(echo "$line" | cut -d ' ' -f 1)"
value="$(echo "$line" | cut -d ' ' -f 2)"
Hope it helped.
1
Quote your variables
â glenn jackman
Jun 28 at 11:32
add a comment |Â
up vote
1
down vote
You can easily achieve this, without using awk, that is inteded for more complex data manipulation.cut bash command is all you need.
key="$(echo "$line" | cut -d ' ' -f 1)"
value="$(echo "$line" | cut -d ' ' -f 2)"
Hope it helped.
1
Quote your variables
â glenn jackman
Jun 28 at 11:32
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can easily achieve this, without using awk, that is inteded for more complex data manipulation.cut bash command is all you need.
key="$(echo "$line" | cut -d ' ' -f 1)"
value="$(echo "$line" | cut -d ' ' -f 2)"
Hope it helped.
You can easily achieve this, without using awk, that is inteded for more complex data manipulation.cut bash command is all you need.
key="$(echo "$line" | cut -d ' ' -f 1)"
value="$(echo "$line" | cut -d ' ' -f 2)"
Hope it helped.
edited Jun 28 at 13:42
ilkkachu
47.3k668130
47.3k668130
answered Jun 28 at 10:15
Gio G.
313
313
1
Quote your variables
â glenn jackman
Jun 28 at 11:32
add a comment |Â
1
Quote your variables
â glenn jackman
Jun 28 at 11:32
1
1
Quote your variables
â glenn jackman
Jun 28 at 11:32
Quote your variables
â glenn jackman
Jun 28 at 11:32
add a comment |Â
up vote
0
down vote
You can use the shell's parameter expansion facilities instead of calling out to an external program:
key=$line%% * # from the end remove the longest string matching
# a space followed by any characters
value=$line#* # from the start remove the shortest string matching
# any characters followed by a space
add a comment |Â
up vote
0
down vote
You can use the shell's parameter expansion facilities instead of calling out to an external program:
key=$line%% * # from the end remove the longest string matching
# a space followed by any characters
value=$line#* # from the start remove the shortest string matching
# any characters followed by a space
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can use the shell's parameter expansion facilities instead of calling out to an external program:
key=$line%% * # from the end remove the longest string matching
# a space followed by any characters
value=$line#* # from the start remove the shortest string matching
# any characters followed by a space
You can use the shell's parameter expansion facilities instead of calling out to an external program:
key=$line%% * # from the end remove the longest string matching
# a space followed by any characters
value=$line#* # from the start remove the shortest string matching
# any characters followed by a space
answered Jun 28 at 11:36
glenn jackman
45.6k264100
45.6k264100
add a comment |Â
add a comment |Â
up vote
0
down vote
read
Just use read:
read key value
Everything before the first space on the line goes into key and everything after it (including any additional spaces) goes into value.
add a comment |Â
up vote
0
down vote
read
Just use read:
read key value
Everything before the first space on the line goes into key and everything after it (including any additional spaces) goes into value.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
read
Just use read:
read key value
Everything before the first space on the line goes into key and everything after it (including any additional spaces) goes into value.
read
Just use read:
read key value
Everything before the first space on the line goes into key and everything after it (including any additional spaces) goes into value.
answered Jun 28 at 13:49
Monty Harder
21815
21815
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%2f452363%2fshell-script-split-line%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