# split string

**URL:** <https://community.unix.com/t/split-string/293559>\
**Category:** Shell Programming and Scripting\
**Created:** [August 16, 2011, 12:55am UTC](https://community.unix.com/t/split-string/293559 "2011-08-16T00:55:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jionnet](https://community.unix.com/letter_avatar/jionnet/32/6_449c533f586939b6e5f246632a46421f.png) [@jionnet](https://community.unix.com/u/jionnet)\
**Post date:** [August 16, 2011, 12:55am UTC](https://community.unix.com/t/split-string/293559/1 "2011-08-16T00:55:03Z")

</div>

Hi  
I am facing a problem in spitting a string.

Here is the string  
--------------------  
subject1=10;subject2=30;subject3=40;subjectcode=10001;...

Now, I want only marks not the subject code. (there can be 'n' subjects)  
ie.  
10  
30  
40

My doubt  
----------  
How do I look for subject'n'= (where 'n' is number). I want to ignore if there is any characters between subject and =.

---

<div class="post-metadata">

**Author:** ![yazu](https://community.unix.com/user_avatar/community.unix.com/yazu/32/7048_2.png) [@yazu](https://community.unix.com/u/yazu)\
**Post date:** [August 16, 2011, 1:01am UTC](https://community.unix.com/t/split-string/293559/2 "2011-08-16T01:01:50Z")

</div>

Try:

```nohighlight
echo 'subject1=10;subject2=30;subject3=40;subjectcode=10001;../..' |
tr ';' '\n' | sed -n 's/subject[0-9]\+=\([0-9]\+\)/\1/p'
10
30
40

```

---

<div class="post-metadata">

**Author:** ![itkamaraj](https://community.unix.com/user_avatar/community.unix.com/itkamaraj/32/1880_2.png) [@itkamaraj](https://community.unix.com/u/itkamaraj)\
**Post date:** [August 16, 2011, 1:31am UTC](https://community.unix.com/t/split-string/293559/3 "2011-08-16T01:31:46Z")

</div>

```nohighlight
 
$ nawk -F"[=;]" '{for(i=2;i<=NF;i=i+2)print $i}' test                                                                                              
10
30
40
10001

 
$ cat test 
subject1=10;subject2=30;subject3=40;subjectcode=10001;

```

---------- Post updated at 11:01 AM ---------- Previous update was at 10:55 AM ----------

i didnt read your question properly... try the below ( test is the input file name )

```nohighlight
 
$ nawk -F"[=;]" '{for(i=1;i<=NF;i++){ if($i~/subject[0-9]/) {print $(i+1)}}}' test                                                                 
10
30
40

```

---

<div class="post-metadata">

**Author:** ![cfajohnson](https://community.unix.com/user_avatar/community.unix.com/cfajohnson/32/16669_2.png) [@cfajohnson](https://community.unix.com/u/cfajohnson)\
**Post date:** [August 16, 2011, 1:36am UTC](https://community.unix.com/t/split-string/293559/4 "2011-08-16T01:36:57Z")

</div>

```nohighlight
IFS=';'
set -f
set -- $string
set +f

for n
do
  case $n in
    subject*[!0-9]*=*) continue ;;
    subject*) printf "%s\n" "${n#*=}" ;;
  esac
done

```

---

<div class="post-metadata">

**Author:** ![ahamed101](https://community.unix.com/user_avatar/community.unix.com/ahamed101/32/1118_2.png) [@ahamed101](https://community.unix.com/u/ahamed101)\
**Post date:** [August 16, 2011, 2:25am UTC](https://community.unix.com/t/split-string/293559/5 "2011-08-16T02:25:03Z")

</div>

Another option

```nohighlight
awk -F"=|;" '{ for(i=2;i<=NF;i=i+2){print $i} }' infile

```

regards,  
Ahamed

---------- Post updated at 10:25 PM ---------- Previous update was at 10:23 PM ----------

Ooops... please ignore the above code... I missed out the actual requirement...  
My code also will boil down to itkamaraj's code... 🙂

regards,  
Ahamed

---

<div class="post-metadata">

**Author:** ![jionnet](https://community.unix.com/letter_avatar/jionnet/32/6_449c533f586939b6e5f246632a46421f.png) [@jionnet](https://community.unix.com/u/jionnet)\
**Post date:** [August 16, 2011, 3:12am UTC](https://community.unix.com/t/split-string/293559/6 "2011-08-16T03:12:14Z")

</div>

Thanks a lot...  
It Worked 🙂 🙂 🙂
