Break down a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Break down a string
# 1  
Old 06-16-2009
Break down a string

I am wondering how can I take a variable that may have multiple items of data and to use each one indenpendently.

For example lets say that....
Code:
data=/lcl/apps/trm, /lcl/apps/wwe, /prd/sse/qwe, /lcl/ppe/eer

Now I would like to be able to process each item found within the data string. As you can see I separated them by commas only for this example. I am not sure on what the syntax should look like.

I am not sure how to do that.

I guess it would be some sort of "for each" statement or something.
I have never had to do this before so I am not sure what I should even look up.

Thanks in advance for the help.

-----Post Update-----

Figured it out. :-)

Code:
#!/bin/ksh
locations="casd, eif, omnibus_procedure_scripts, lcf, gates, etc"
for i in `echo ${locations} | tr ',' '\n'`
do
echo $i
done

# 2  
Old 06-16-2009
Code:
#!/bin/bash

data=(/lcl/apps/trm /lcl/apps/wwe /prd/sse/qwe /lcl/ppe/eer)

for (( i=0 ; i <= "${#data[@]}" ; i++ ))
do
echo "${data[i]}" 
done

# 3  
Old 06-16-2009
Code:
#!/bin/ksh

data='/lcl/apps/trm,/lcl/apps/wwe,/prd/sse/qwe,/lcl/ppe/eer'

oldIFS=$IFS
IFS=,
set -- ${data}
while [ "${1}" ]
do
  echo "entry->[${1}]"
  shift
done
IFS=$oldIFS

# 4  
Old 06-16-2009
If you have a "," as field seperator and you want to get rid of it use shell script manipulation as in below:-


Code:
#!/bin/bash

data=(/lcl/apps/trm, /lcl/apps/wwe, /prd/sse/qwe, /lcl/ppe/eer)

for (( i=0 ; i <= "${#data[@]}" ; i++ ))
do
echo "${data[i]%,}" 
done

# 5  
Old 06-16-2009
Code:
# string="/lcl/apps/trm,/lcl/apps/wwe,/prd/sse/qwe,/lcl/ppe/eer"
# IFS=","
# set -- $string
# echo $1
/lcl/apps/trm
# echo $2
/lcl/apps/wwe
# echo $3
/prd/sse/qwe

# 6  
Old 06-17-2009
Code:
data="/lcl/apps/trm, /lcl/apps/wwe, /prd/sse/qwe, /lcl/ppe/eer"
IFS=,
set $data
while [ $# -gt 0 ];do
echo $1
shift
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Break a file into smaller pieces with same search string

please help to break a file into smaller pieces with same search string and delete the matched lines. like i have a below file <expression> some text ........ .......... </expression> <expression> some text ........ .......... </expression> <expression> some text ........... (11 Replies)
Discussion started by: nadeemrafikhan
11 Replies

2. Shell Programming and Scripting

Break one long string into multiple fixed length lines

This is actually a KSH under Unix System Services (Z/OS), but hoping I can get a standard AIX/KSH solution to work... I have a very large, single line file in Windows, that we download via FTP, with the "SITE WRAP" option, into a Z/OS file with an LRECL of 200. This essentially breaks the single... (4 Replies)
Discussion started by: bubbawuzhere
4 Replies

3. UNIX for Dummies Questions & Answers

Find a string across line break (because of "segmentation fault core dumped")

Hi, thanks to a precedent post, and thanks to the reply of derekludwig of the forum, I have convert my first awk command as : test.txt is : AAAAAGHIJKLAjKMEFJKLjklABCDJkLEFGHIJKL awk -f findstring.awk test.txt > textreturn.txtfindstring.awk is : BEGIN{ SLENGTH = 3 } { ... (3 Replies)
Discussion started by: thewizarde6
3 Replies

4. UNIX for Dummies Questions & Answers

add a string to a file without line break

I searched and found "echo -n" and "printf" are solution for this, but they are not here: $ echo "hello" >> test $ cat test hello $ echo -n "world" >> test $ cat test hello world $ echo -n " seriously?" >> test $ cat test hello world seriously? This is not successful... (15 Replies)
Discussion started by: stunn3r
15 Replies

5. UNIX for Dummies Questions & Answers

Insert a break page after certain string using SED

Hi: I have 2 files: teststring.txt and a tempfile.txt teststring file contains: s/Primary Ins./\n1/g I'm trying to search for "Primary Ins." string in tempfile. For every "Primary Ins." string that is found, a new line is inserted and put in number 1. Then, write out the newfile... (7 Replies)
Discussion started by: newbeee
7 Replies

6. Shell Programming and Scripting

Trying to take a string and break each letter into a separate variable

I am trying to make a script that takes a word and each letter up and turns it into a separate variable. My code currently does not work but I feel I just need to tweak one thing that I am unsure of. (ex: if forum was typed in letter1=f; letter2=o; letter3=r;...) Thank you count=1; ... (7 Replies)
Discussion started by: crimputt
7 Replies

7. Shell Programming and Scripting

break the string and print it in a new line after a specific word

Hi Gurus I am new to this forum.. I am using HP Unix OS. I have one single string in input file as shown below Abc123 | cde | fgh | ghik| lmno | Abc456 |one |two |three | four | Abc789 | five | Six | seven | eight | Abc098 | ........ I want to achive the result in a output file as shown... (3 Replies)
Discussion started by: kannansr621
3 Replies

8. Shell Programming and Scripting

line break if string exceeds 100chars

I am required to develop a script to look for specific strings in the /var/adm/messages file on our Solaris10 host. When an entry is found in the file then I need that line to be piped to a log file. I have a script with the criteria i just need help with the manipulation of the text string. 1.... (6 Replies)
Discussion started by: s1ckle
6 Replies

9. Shell Programming and Scripting

AWK break string into fields + pattern match

I am trying to break a string into separate fields and print the field that matches a pattern. I am using awk at the moment and have gotten this far: awk '{for(i=1;i<=NF;++i)print "\t" $i}' longstring This breaks the string into fields and prints each field on a separate line. I want to add... (2 Replies)
Discussion started by: Moxy
2 Replies

10. Shell Programming and Scripting

how to insert line break + string in vi (search & replace )

Hello all i have big test file that has allot of structure text something like this : <foo1 *.html> <blah action> somthing 1 somthing 2 </blah> </foo1 > now i will like to insert 2 more lines of text below the <blah action> so it will be like : <foo1... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question