Home > OS >  Parse multiple string as single variable in Shell
Parse multiple string as single variable in Shell

Time:03-29

Suppose I have files containing below string.

Paris, France baguette
Madrid, Spain callos
Sheffield, England steak

I want to loop through the files, printing City and culinary.

City: Paris, France
Food: baguette

The tricky part is there is a space between Country and its City, obviously simple while read -r doesn't work because of the whitespace.

CodePudding user response:

you need an other delimiter between country and food for example ","

find . -type f -name "*.txt"|xargs -I {} sh -c "awk -F, '{print \"City: \" \$1 \",\" \$2; nexline; print \"Food: \"\$3}' {}"

City: London, Great Britain
Food: fish & chips
City: Paris, France
Food: baguette
City: Madrid, Spain
Food: callos
City: Sheffield, England
Food: steak
  • Related