简体   繁体   中英

Bash Conditional Quote in String

I have a CSV file that I'm parsing in bash that, for one column, has more than one value for some rows. For example, a line with multiple values may look like

name,12,120,east,"sw1,sw2,sw3"

But not all rows have it. Some may look like

name,10,141,west,sw5534a

What I'm trying to do is if that column has quotes in it to remove them and set the variable to just sw1,sw2,sw3

Relevant parts of the script:

#!/bin/bash
INPUT=file.csv
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read name building id region parents
do
echo "
....snip....
parents $parents"

The output I want for $parents should be sw1,sw2,sw3 but right now it spits out as "sw1,sw2,sw3" I've tried messing around with regex matching in a conditional if the column has a comma to remove the first and last two characters, but I couldn't get it to work. Either it would remove the first s and the last 3 or it would just error out.

Any suggestions appreciated!

You can remove both instances of the " character in the $parents variable with substring replacement:

echo ${parents//\"/}

This replaces all " characters with the empty string.

parents="${parents#\"}"
parents="${parents%\"}"

This will remove the first character if a quote, and the last character if a quote. If they are not a quote, they will be left untouched.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM