Home > Blockchain >  Parse each line with multiple string values in a file using shell script
Parse each line with multiple string values in a file using shell script

Time:01-15

    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

CodePudding user response:

try this one:

cat 1.txt|awk '{print "str="$1,"country="$2,"name="$3}'

CodePudding user response:

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.

CodePudding user response:

Depending on your exact input format, Miller (that provides binaries for most OSs) can help you:

#!/bin/bash

while IFS=$'\t' read -r sr country name
do
    # now you can process those variables
    # ...
done < <(mlr --icsv --ifs ' ' --otsvlite cat -- file.txt)

It has its shortcomings though - it doen't escape the TSV values, but if you don't have literal tabs or newlines as part of the CSV data then it's all good.

  •  Tags:  
  • Related