I have a file with the header (first line) with a variable length; and it comes as:One|Two|Threeor One|Two|Three|...|One Hundredand want to get do a loop that transforms it into a new file:Hello One!Hello Two!Hello Three! ...Hello One Hundred!What should I do? Use awk? sed?I couldn't find a solution without fixing the size.My OS is Linux-RedHat-RHEL-6.
Reading a variable-sized line and splitting its content into separate lines with text
linux;shell script;sed;awk
bash: read the pipe-separated words into an arrayIFS='|' read -ra words < <(head -n 1 file)printf Hello %s!\n ${words[@]}awk: iterate over the words in the line.awk -F'|' 'NR == 1 { for (i=1; i<=NF; i++) print Hello  $i !; exit}' fileand a sed:sed -e 2Q -e 's/\([^|]\+\)/Hello \1!/g' -e 's/[|]/\n/g' file