티스토리 뷰

공부

[sh] csv string count

승가비 2023. 3. 16. 00:45
728x90
#!/bin/bash

input=$1
output=$2

rm $output

n=0
while read line; do
  comma="${line//[^,]}"
  cnt="${#comma}"

  echo "Line No. ${n} : ${cnt}" >> $output

  n=$((n+1))
done < ${input}
var="text,text,text,text"
res="${var//[^,]}"
echo "$res"
echo "${#res}"
,,,
3

https://stackoverflow.com/questions/16679369/count-occurrences-of-a-char-in-a-string-using-bash

 

Count occurrences of a char in a string using Bash

I need to count the number of occurrences of a char in a string using Bash. In the following example, when the char is (for example) t, it echos the correct number of occurrences of t in var, but ...

stackoverflow.com

https://linuxhint.com/read_file_line_by_line_bash/

 

How to read file line by line in Bash script

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u H

linuxhint.com

 

728x90

'공부' 카테고리의 다른 글

[hive] partition reload  (0) 2023.03.16
[windows] COPY /b headers.txt+data.txt result.txt  (0) 2023.03.16
[sh] sudo systemctl list-units  (0) 2023.03.14
[jinja2] {{ asdf|join(", ") }}  (0) 2023.03.14
[airflow] variables `{{ var.value.aa }}`  (0) 2023.03.14
댓글