문제

i have a file created by a spool. This file has columns separated by a separator : #@; Is there a way to find the data between the separator? I mean for example :

#@;Hello #@;World #@;!!!

i have to find hello then world then !!!. I tried this way but doesn't work:

tp=${ENDFL##*@#}
          HELLOSTRING=`printf '"%s"\n' "${tp%%#@;*}"`

Any ideas? thanks

도움이 되었습니까?

해결책

Use translate command:

  echo "#@;Hello #@;World #@;!!!"  | tr '#@;' '^' | tr -s '^' 

Output:

 Kaizen ~
 $ echo "#@;Hello #@;World #@;!!!"  | tr '#@;' '^' | tr -s '^'
   ^Hello ^World ^!!!

Explanation:

The first tr replaces the '#@;' delimiter with ^ , but it does that three times.

"# , @ , ;" are three separate literals so it subs ^ for all three separately

The second tr suppresses the occurrence of multiple ^ to one.

Hence you get a ^ delimited output as "^Hello ^World ^!!!"

For your file just cat filename then pipe it to the translate command after which you can use AWK, cut or whatever to format or extract as per you need.

다른 팁

If you have to deal with a bunch of lines

sed -e 's/#@;//g' input.dat

To find the data between the separator (assuming you have the value in a variable as suggested by tp=)

echo "${tp/#@;/}"

If you want to work with the parts

origIFS=$IFS        # always kep the original $IFS save
IFS='§'            # define a InputFieldSeparator which will not be used in your input
set ${tp//#@;/$IFS} # reduce separator to one char and set $1 ... based on that separator
IFS=$origIFS        # and restore $IFS when you are done
echo "'$1' '$2' '$3' '$4'"  # note that $1 is empty, it is before the first separator)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top