New answers tagged


I'm assuming that in your expected result, the first instance of $myarray[2] should have been $myarray[1]. You can achieve the desired result by stripping the embedded newlines before assinging the file's content to the shell variable: filecontent=$(tr -d \\n <myfile)


Did you try newline on IFS like IFS=$'\n' read -a myarray <<< "$filecontent" The reason I am suggesting $'\n' is because bash doesn't interpret escape sequences in string literals.


Sticking with your awk ... just make sure you understand the difference between a field and a record separator :} echo "a,b,c,d,e,f" | awk 'BEGIN{RS=","}{$1=$1}1' But the tr solution in the comments is preferable.


Here's a sed script that will split your example: #!/bin/sed -Ef # replace all commas with newlines s/,/\ /g # Do we need to re-join any lines? :loop # Unmatched brace containing possibly another (matched) level of # braces: s/(\{([^{}]|\{[^{}]*\})*)\ /\1,/ tloop # remove any leading space s/\n */\ /g # At first line, print result, then exit. 1q ...


Try a pure bash #!/bin/bash TEST_STRING="{0,1}, alpha, {(x,y,z)}, {{1,2,3}, {a,b,c}}" TEST_STRING="$TEST_STRING""," count=0 newword='' while [ "${TEST_STRING::1}" ] ; do l="${TEST_STRING::1}" TEST_STRING=${TEST_STRING:1} [ "$l" = '{' ] && ((count++)) [ "$l" = '}' ] && ((count--)) if [ "$l" = ',' ] && ! ((count)) ...


Basically, I'm looking for split that will output to stdout, not files. If you have access to gnu split, the --filter option does exactly that: ‘--filter=command’ With this option, rather than simply writing to each output file, write through a pipe to the specified shell command for each output file. So, in your case, you could either ...


Just add x="F0" to the beginning so the target file is always defined, even if the first line doesn't contain the pattern: awk 'BEGIN { x="F0" ; } /START/{x="F"++i;}{print > x}' The above breaks down to this pseudo code: ### -> BEGIN { x="F0" ; } i=0 # implicit x="F0" # explicit loop through file ### -> /START/{x="F"++i;} if ( line contains ...


With any POSIX shell: $ str=/home/user/a/directory/ $ path=${str%/*} $ app=${str##*/} $ printf 'path is: %s\n' "$path" path is: /home/user/a/directory $ printf 'app is: %s\n' "$app" app is: save you for two processes forking. In case of /, and /path/to/, basename/dirname are more graceful. See also this ...


The commands basename and dirname can be used for that, for example: $ basename /home/user/a/directory/ $ dirname /home/user/a/directory/ /home/user/a/directory For more information, do not hesitate to do man basename and man dirname.

Top 50 recent answers are included