New answers tagged

0

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)


0

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.


1

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.


2

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 ...


2

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)) ...


0

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 ...


3

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 ...


6

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


10

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



Top 50 recent answers are included