csv.file 에는 아래와 같이 콤마(,)로 구분된 문자 집합이 들어 있다.

zeus@zeus-pc ~/temp $ cat csv.file
a,b,c,d,e,f,g,h,i,j,k
l,m,n,o,p,q,r,s,t,u,v
w,x,y,z,@,#,$,%,^,&,*


이를 아래와 같이 awk 명령을 사용하여 콤마(,) 단위로 split 한 후,
특정 위치의 문자만 화면에 출력할 수 있다.

zeus@zeus-pc ~/temp $ cat csv.file | awk '{ split($0,arr,","); printf("%s,%s\n",arr[1],arr[5]); }'
a,e
l,p
w,@


출처 - http://home.zany.kr:9003/board/bView.asp?bCode=11&aCode=3165






split string using separetor

i have one string , I want to split that string.
exmp:


string="abc@hotmail.com;xyz@gmail.com;uvw@yahoo.com"

I want to split it and save it in three variable
str1=abc@hotmail.com 
str2=xyz@gmail.com
str3=uvw@yahoo.com

I want to split using ';'.

please help.


Code: 1

echo $string |cut -d';' -f1 | read str1
echo $string |cut -d';' -f2 | read str2
echo $string |cut -d';' -f3 | read str3


Code: 2

$str = "abc\@hotmail.com;xyz\@gmail.com;uvw\@yahoo.com";

@arr = split(/;/, $str);

print "first: $arr[0]\n";
print "second: $arr[1]\n";
print "third: $arr[2]\n";


Code: 3

string="abc@hotmail.com;xyz@gmail.com;uvw@yahoo.com"
str1=${string%%;*}
str3=${string##*;}
temp=${string#$str1;}
str2=${temp#;$str3}



matrixmadhan's solution is for perl script.
You can do the same thing with ksh :

Code: 4
#!/usr/bin/ksh

string="abc@hotmail.com;xyz@gmail.com;uvw@yahoo.com"

oIFS="$IFS"; IFS=';' 
set -A str $string
IFS="$oIFS"

echo "strings count = ${#str[@]}"
echo "first : ${str[0]}";
echo "second: ${str[1]}";
echo "third : ${str[2]}";

Output:

Code:
strings count = 3
first : abc@hotmail.com
second: xyz@gmail.com
third : uvw@yahoo.com



Code: 4
string="abc@hotmail.com;xyz@gmail.com;uvw@yahoo.com"
var=$(echo $string | awk -F";" '{print $1,$2,$3}')   
set -- $var
echo $1
echo $2
echo $3

output:

Code:
# ./test.sh
abc@hotmail.com
xyz@gmail.com
uvw@yahoo.com



출처 - http://www.unix.com/shell-programming-scripting/38450-split-string-using-separetor.html




Posted by linuxism
,