returning split fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting returning split fields
# 1  
Old 05-11-2007
returning split fields

I have a variable with data in this format


field1;field2;field3

I wanted to split the variable like this

field1
field2
field3

this statement was working fine echo $key_val | awk '{gsub(";" , "\n"))'


but sometimes we get the data in the variable in this format key_val="field1"

if this value is sent through the above statement it does'nt return any value

I want to write a short shell script which will return the data like


field1
field2
field3

when the input was like field1;field2;field3

and just field1 when the input was just like that

I think this way will work

count=0
count=`echo $key_val | sed -cd ";" |wc -c`

if [[ $ count -eq 0 ]]
then
echo $key_val
else
echo $key_val | awk '{gsub(";" , "\n"))'
fi

However it seems too long and cumbersome way of implementing it,do you have any suggestions how i can make the code more compact or more efficient

I am working in the korn shell

Thanks

Mervin
# 2  
Old 05-11-2007
Code:
tr ';' '\n' < file

or
Code:
sed 's/;/\n/g' file

or
Code:
awk 'BEGIN{FS=";"} {for(i=1;i<=NF;i++)print $i}' file

# 3  
Old 05-11-2007
Quote:
Originally Posted by ghostdog74
Code:
tr ';' '\n' < file

or
Code:
sed 's/;/\n/g' file

or
Code:
awk 'BEGIN{FS=";"} {for(i=1;i<=NF;i++)print $i}' file


Thanks but dont i need to have the flow to come in from a file for the above solutions ?or can i can have it like this ?

output_val=`echo $key_val | sed 's/;/\n/g'`

or any of the solutions you had suggested
Thanks for the help

Mervin
# 4  
Old 05-11-2007
just a clarification,what if there is no ; at the end of the field value say something like this "AAAA" ,will it still return the value

I could test and try it but i dont have access to an unix box right now

thanks

Mervin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to add plus or minus to fields and split another field

In the tab-delimited input below I am trying to use awk to -10 from $2 and +10 to $3. Something like awk -F'\t' -v OFS='\t' -v s=10 '{split($4,a,":"); print $1,$2-s,$3+s,a,$5,$6} | awk {split(a,b,"-"); print $1,$2-s,$3+s,b-s,b+s,$5,$6}' input should do that. I also need to -10 from $4... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Split fields from a file

Hi All, I have a file where a list of email id's are stored as shown below: emailid1@blh.com emaild2@blh.com asdf@blah.com emailid3@blh.com In my shell script, i am sending emails to above id's My requirement is to seperate the email id's into 2 groups.. emailid1@blh.com... (10 Replies)
Discussion started by: galaxy_rocky
10 Replies

3. Shell Programming and Scripting

Split a free form text delimited by space to words with other fields

Hi, I need your help for below with shell scripting or perl I/P key, Sentence customer1, I am David customer2, I am Taylor O/P Key, Words Customer1,I Customer1,am Customer1,David Customer2,I Customer2,am Customer2,Taylor (4 Replies)
Discussion started by: monishathampi
4 Replies

4. Shell Programming and Scripting

awk split lines without knowing the number of fields a-priori

I want to use awk to split fields and put them into a file but I don't know the number of fields for example, in the following line Ports: 22/filtered/tcp//ssh///, 53/open/tcp//tcpwrapped///, 111/filtered/tcp//rpcbind///, 543/filtered/tcp//klogin///, 544/filtered/tcp//kshell///,... (3 Replies)
Discussion started by: esolvepolito
3 Replies

5. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

6. Shell Programming and Scripting

AWK:Split fields separated by semicolon

Hi all, I have a .vcf file which contains 8 coulmns and the data under each column as shown below, CHROM POS ID REF ALT QUAL FILTER INFO 1 3000012 . A G 126 ... (6 Replies)
Discussion started by: mehar
6 Replies

7. Shell Programming and Scripting

how to split the row(array) in to fields and store in to oracle database in unix

Hi, the csv file with the delimeter #. A#B#C#D#E#F#G#H 1#2#3#4#5#6#7#8 Z#x#c#V 7#2#8#9 N. I want to read the file line by line and store in rowarray. then the rowarray content should be spilt or made to fields using the delimeter #. i am not sure can we store the fields in to... (3 Replies)
Discussion started by: barani75
3 Replies

8. Web Development

split the fields in a column into 3 columns

Have a column "address" which is combination of city, region and postal code like. Format is : city<comma><space>region<space>postal code abc, xyz 123456 All these three city, region and postal code are not mandatory. There can be any one of the above. In that case a nell... (2 Replies)
Discussion started by: rakshit
2 Replies

9. Shell Programming and Scripting

How to split a field into two fields?

Hi, I have a comma delimited text file where character fields (as opposed to numeric and date fields) are always enclosed with double quotes. Records are separated by the newline character. In a shell script I would like to split a particular field into two separate fields (enclosed with double... (4 Replies)
Discussion started by: vbrown
4 Replies

10. Shell Programming and Scripting

split varibles and store fields into shell varible array

I need to split a long varible which is a whole line read from a file into fields and store them in an array, the fields are delimited by pipe and a field may contain white spaces. I tried the following concept test and it has problem with field 5 which contain a space, appearently so because... (3 Replies)
Discussion started by: gratus
3 Replies
Login or Register to Ask a Question