Parse each line with multiple string values in a file using shell script
cat file.txt
"1" "USA" "abc"
"2" "Canada" "pqr"
....
I'm trying to assign the above string values to a variable iterating through each line at a time. For eg. ->
sr="1" country="USA" name="abc"
sr="2" country="Canada" name="pqr"
Any advise on how I can achieve this? Thanks
try this one:
cat 1.txt|awk '{print "str="$1,"country="$2,"name="$3}'
If the quoting of the data is compatible to the Bash rules for Double Quotes, you can just use set
:
#! /bin/bash
exec <<EOF
"1" "USA" "abc"
"2" "Canada" "pqr"
EOF
while read -r line; do
source <(printf 'set %s\n' "$line")
printf 'sr="%s" country="%s" name="%s"\n' "$@"
done
If the quoting of the data is compatible to the JSON quoting rules, you can use jq
to parse the data:
#! /bin/bash
exec <<EOF
"1" "USA" "abc"
"2" "Canada" "pqr"
EOF
jq -sr '.[]' | while true; do
read -r sr || break
read -r country
read -r name
printf 'sr="%s" country="%s" name="%s"\n' \
"$sr" "$country" "$name"
done
This will be less prone to security exploits.