Cut text and assign them into array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut text and assign them into array
# 1  
Old 01-11-2011
Cut text and assign them into array

file.txt : is delimiter:
Code:
abc:def:ghi
jkl:mno: pqr
123:456:789

if I do the cut command, and cut the first column, and echo it out
I will get the output:
Code:
abc
jkl
123

How can I assign the column of text that I've cut into Array?
e.g If I were to echo array[0] array[2] it will output as:
abc 123

Thanks in advance!

Last edited by fpmurphy; 01-11-2011 at 11:18 AM.. Reason: code tags please!!
# 2  
Old 01-11-2011
Code:
$ cat infile
abc:def:ghi
jkl:mno: pqr
123:456:789

Code:
$ set -- $(cut -d: -f1 infile)

Code:
$ echo $1
abc

Code:
$ echo $2
jkl

Code:
$ echo $3
123

# 3  
Old 01-11-2011
Code:
i=0
while read line
do
  array[$i]=${line%%:*}
  echo ${array[$i]}
  i=$((i+1))
done < file.txt

# 4  
Old 01-11-2011
actually I'm trying to make a search function,
lets say infile.txt
Code:
Chocolate:Supermarket A:5
Ice Cream:SuperMarket B:7
Banana:Supermarket B:3

I try to search products under Supermarket B, and output it like this:
Code:
Ice Cream, Supermarket B, $7
Banana, Supermarket B, $3

(note that the : delimiter is replaced by ", and a space" and the price now has a $)

I tried coding it like this:
Code:
productcount=`grep -c ":Supermarket B:." infile.txt`
echo $productcount

output will be:
Code:
2

Code:
price=`grep ".:Supermarket B:." infile.txt | cut -f3 -d ":"`
echo $price

output will be:
Code:
7
3

Code:
product=`grep ".:Supermarket B:." infile.txt | cut -f1 -d ":"`
echo $product

output will be:
Code:
Ice Cream
Banana

Code:
for (( i=0; i<$productcount; i++ ))
do
echo " ${product[$i]} Supermarket B \$${price[$i]}"
done

Am I totally doing it wrong? I just pick up BASH programming, so I can't code very well.
Is there a better way to do this?
Please advise, thanks alot!

Last edited by fpmurphy; 01-11-2011 at 11:17 AM.. Reason: code tags please!!
# 5  
Old 01-11-2011
Code:
awk -F: '/Supermarket B/{$3="$"$3;print}' OFS=\, file | sed 's/,/, /g'

This User Gave Thanks to Franklin52 For This Post:
# 6  
Old 01-11-2011
Code:
grep -i 'Supermarket B' infile.txt | sed 's/:/, /;s/:/, \$/'

Code:
# cat infile2
Chocolate:Supermarket A:5
Ice Cream:SuperMarket B:7
Banana:Supermarket B:3
# grep -i 'Supermarket B' infile2 | sed 's/:/, /;s/:/, \$/'
Ice Cream, SuperMarket B, $7
Banana, Supermarket B, $3


Last edited by ctsgnb; 01-11-2011 at 01:34 PM..
This User Gave Thanks to ctsgnb For This Post:
# 7  
Old 01-11-2011
Code:
awk -F: '$2==v && $3="$"$3' v="Supermarket B" OFS=', ' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to assign value to variable using cut?

Hey guys. Below is the command I am using to assign "yellow" to the variable yellow. I can seem to echo it out using xargs but when I assign it to yellow and then try to echo it out it prints a blank line. Below is the command. Your help is highly appreciated! cut -c 2- color.txt |... (11 Replies)
Discussion started by: eldan88
11 Replies

2. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

3. Shell Programming and Scripting

Assign value to array separated by #

Hi all , I have a string like para1#para2#para3 i want to assign para1 as first element para2 as second and so on i tried IFS=# set -A array para1#para2#para3 echo ${array} para1 para2 para3 i want echo ${array} para1 (2 Replies)
Discussion started by: max_hammer
2 Replies

4. Programming

assign array with a local var???

Hi all, I am just wondering is this (below code) valid in programming int xx = 10110; int i; long yy = {1,2,3,4,5,6,7,8,xx}; I compiled this using gcc will all working option successfully without getting any warning or error. I didn't see this kind of code in my past... (7 Replies)
Discussion started by: zing_foru
7 Replies

5. UNIX for Advanced & Expert Users

assign array with a local var???

topic moved to programming (0 Replies)
Discussion started by: zing_foru
0 Replies

6. Shell Programming and Scripting

assign value to array variable

Hi, I have a piece of code as follows: i=0 while read LINE do var = "$LINE" i=$((i+1)) echo "${var}" done < file I want to assign value to the array var. However, when i execute the script i get a error. Please can you help me know what i am missing. I ultimately want to... (2 Replies)
Discussion started by: sunrexstar
2 Replies

7. Shell Programming and Scripting

How to assign a variable to an array

I want to ask the user to enter an X amount of file names. I want to put those names into an array and then loop back through them to verify they are in the directory. 1st- How would I assign the value to an array and what is the correct syntax. 2nd- how would i reference that array after I... (3 Replies)
Discussion started by: tvb2727
3 Replies

8. UNIX for Dummies Questions & Answers

Cut Command value assign to variable

Hi, I am new to UNIX Scripting. I have been trying to use the CUT command to retrieve part of the header from a file and assign it to a variable. I have tried searching a lot, but I am still unsuccessful. Sample Header: HJAN BALANCE 20090616 I need to retrieve the date here, which always... (10 Replies)
Discussion started by: ragz_82
10 Replies

9. UNIX for Dummies Questions & Answers

to assign cut values to an array

i need to seperate values seperated by delimiters and assign it to an array.. can u plz help me on that. Variables = "asd,rgbh,(,rty,got,),sroe,9034," i need to assign the variables into arrays.. like.. var=asd var=rgbh.. and so on how do i do this. i need to reuse the values stored in... (6 Replies)
Discussion started by: Syms
6 Replies

10. Shell Programming and Scripting

Assign variables with cut

I need to read a file (a list) and assign the value to a variable (for each line), I'm looping until the end of the file. My problem is, I want to assign 2 separate variables from the list. The process I'm using is: awk '{print $3}' file1 > file2 awk '{print $4}' file1 > file3 cat file2... (2 Replies)
Discussion started by: douknownam
2 Replies
Login or Register to Ask a Question