cut -f1 -d"," to a array and loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cut -f1 -d"," to a array and loop
# 1  
Old 11-09-2010
cut -f1 -d"," to a array and loop

############## SOLVE #############


Hi

I would like to do something quite simple but i could not figure it out

Example file:
Code:
BAS,Fast ZeroSPEC,Fast ZeroSPEC,Fast Zero,CAD MASTER SWAP
BAS,Fast ZeroSPEC,Fast ZeroSPEC,Fast Zero,CHF SWAP GOTTEX
BAS,Fast ZeroSPEC,Fast ZeroSPEC,Fast Zero,CLP MONEY
BAS,Fast ZeroSPEC,Fast ZeroSPEC,Fast Zero,CNY MONEY
BAS,Fast ZeroSPEC,Fast ZeroSPEC,Fast Zero,COF DUMMY

Notice the last field containt space

I want to create array of this, then print it line by line

####################
Code:
array=($(cut -f5 -d"," $FILE))
 
        for array in "${array[@]}";
        do
            echo $array
            echo yes
        done

###################

wanted output:

Code:
CAD MASTER SWAP
yes
CHF SWAP GOTTEX
yes
....



but somehow my variable array containt only one argument with all my line on it like this, so my code return that

Code:
CAD MASTER SWAP
CHF SWAP GOTTEX
CLP MONEY
CNY MONEY
COF DUMMY
yes

Moderator's Comments:
Mod Comment Please use code tags

Last edited by kykyboss023; 11-10-2010 at 05:56 AM.. Reason: solve
# 2  
Old 11-09-2010
Code:
sed 's/.*,//;G;s/\n/&yes/' $FILE

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 11-09-2010
Code:
awk -F"," '{print$5"\nyes"}' $FILE

This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 11-09-2010
If you really want to populate a shell array this should do it: I used awk to fetch field 5 and put quotes around it and then eval to assign array.

Code:
eval array=( $(awk -F, '{print "\""$5"\"";}' $file) )
 
        for array in "${array[@]}";
        do
            echo $array
            echo yes
        done

This equates to:
Code:
eval array=("CAD MASTER SWAP" "CHF SWAP GOTTEX" ... )


Last edited by Chubler_XL; 11-09-2010 at 11:11 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 11-09-2010
Code:
cut -f5 -d"," $FILE |while read line
        do
            echo $line
            echo yes
        done

This User Gave Thanks to rdcwayx For This Post:
# 6  
Old 11-10-2010
Hi

Thanks everyone, i try all the solution propose and the one that work for me was the following , so probleme solve

Code:
 
cut -f5 -d"," $FILE |while read line
        do
            echo $line
            echo yes
        done

there is still sommething i don't understand as somehow as soon as i store the result in a array or variable it does work, the array containt just one argument
but nervermind

Code:
 
bash-2.05b$ array=$(awk -F, '{print "\""$5"\"";}' ETL/Bas_uniq/uniq.csv)
bash-2.05b$ echo ${array[@]}
"CAD MASTER SWAP" "CHF SWAP GOTTEX" "CLP MONEY" "CNY MONEY" "COF DUMMY" "COP MONEY" "CSD MONEY" "CYP DUMMY" "CZK SWAP" "HRK MONEY" "HUF MONEY MASTER CURVE"
 
bash-2.05b$ echo ${array[1]}
 
bash-2.05b$


Last edited by kykyboss023; 11-10-2010 at 05:58 AM..
# 7  
Old 11-10-2010
Code:
while IFS=, read a b c d e
do
   echo $e
   echo yes
done < $FILE

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

3. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Programming

C: Initialize "const" array from the "heap"

Hello, I am working on solving an NP-Complete problem, so it is very important that operations and data with limited integer-argument ranges be computed using immutable look-up-tables contained entirely in CPU cache. Retrieval of the look-up-table data must never leave the CPU once initially... (6 Replies)
Discussion started by: HeavyJ
6 Replies

6. Shell Programming and Scripting

help for saving vertical datas to horizontal with "awk" or "cut"

hi, i have a file having datas like that ./a.txt 12344 12345 12346 12347 ..... ..... ... i want to save this datas to another file like that ./b.txt 12344 12345 12346 12347 ... ... ... i think awk can make this but how? :) waiting for ur help. (3 Replies)
Discussion started by: mercury
3 Replies

7. Shell Programming and Scripting

How to cut a file using " ", but fields can be separated with more than one " "

Hello, let's say I have a text file: word11 word12 word13 word21 word22 word23 word31 word32 word33 and I want to put the second field of each line into a list: set list = `cut -d" " -f2 ${1}` and I use space (" ") as a delimiter, only that there's a catch: there can be more than... (12 Replies)
Discussion started by: shira
12 Replies

8. Windows & DOS: Issues & Discussions

Unix "cut' and "awk" in Windows XP?

Hi, How can I execute Unix's ksh equivalent of "cut' and "awk" in Windows XP? For example, I want to execute ksh commands from Windows command prompt. Is there a place I can download "cut.exe" and "awk.exe" ? Thanks in advance (4 Replies)
Discussion started by: ihot
4 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question