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
# 8  
Old 11-10-2010
The reason it did not work with the array was the value of IFS. This should work
Code:
oldIFS=$IFS; IFS="
"
array=($(cut -f5 -d, "$FILE"))
IFS=$oldIFS

Code:
$ echo "${array[1]}"
CHF SWAP GOTTEX
$ echo "${array[0]}"
CAD MASTER SWAP

(BTW, It is good practice to give the variable in the loop a different name than the array itself)

Last edited by Scrutinizer; 11-10-2010 at 08:10 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 9  
Old 11-10-2010
MySQL

Code:
# sed 's/.*,\([^,]*\)/\1\nyes/' infile
CAD MASTER SWAP
yes
CHF SWAP GOTTEX
yes
CLP MONEY
yes
CNY MONEY
yes
COF DUMMY
yes

# 10  
Old 11-10-2010
Some more golf Smilie
Code:
sed 's/.*,//;s/$/\nyes/' file

Code:
awk -F, '$0=$5RS"yes"' file


Last edited by Scrutinizer; 11-10-2010 at 09:23 AM..
# 11  
Old 11-10-2010
MySQL

Quote:
Originally Posted by kykyboss023
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$



Because your array value is not a real array..

try this Smilie
Code:
array=( $(awk -F, '{print "\""$5"\"";}' file1) )

Code:
echo ${array[1]}
"CHF SWAP GOTTEX"

regards
@ygemici
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