Looping using cut statement

 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support Looping using cut statement
# 1  
Old 01-31-2011
Looping using cut statement

i want to create loop for below mentioned
Code:
A1=`echo $obj1 | cut -d "," -f3`
A2=`echo $obj1 | cut -d "," -f4`
A3=`echo $obj1 | cut -d "," -f5`
A4=`echo $obj1 | cut -d "," -f6`
A5=`echo $obj1 | cut -d "," -f7`
A6=`echo $obj1 | cut -d "," -f8`
A7=`echo $obj1 | cut -d "," -f9`
A8=`echo $obj1 | cut -d "," -f10`
A9=`echo $obj1 | cut -d "," -f11`
A10=`echo $obj1 | cut -d "," -f12`
A11=`echo $obj1 | cut -d "," -f13`
A12=`echo $obj1 | cut -d "," -f14`
A13=`echo $obj1 | cut -d "," -f15`
A14=`echo $obj1 | cut -d "," -f16`
A15=`echo $obj1 | cut -d "," -f17`
A16=`echo $obj1 | cut -d "," -f18`
A17=`echo $obj1 | cut -d "," -f19`
A18=`echo $obj1 | cut -d "," -f20`
A19=`echo $obj1 | cut -d "," -f21`
A20=`echo $obj1 | cut -d "," -f22`
A21=`echo $obj1 | cut -d "," -f23`
A22=`echo $obj1 | cut -d "," -f24`

pls help

Last edited by Scott; 01-31-2011 at 07:46 AM.. Reason: Code tags
# 2  
Old 01-31-2011
Try this:
Code:
IFS=, read x x A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16 A17 A18 A19 A20 A21 A22 x <<EOV
$obj1
EOV

The values are contained in A1,A2, etcetera


or if your shell knows arrays:
Code:
A=( $(IFS=,; echo ${obj1#*,*,}) )

The values are contained in A[0],A[1], etcetera
# 3  
Old 01-31-2011
That's fine, but it kinda begs the question of what loop this lives in. Maybe while read . . . do . . . done <file.csv

Also, true csv has double quoting, a little hard to deal with outside a real language.
# 4  
Old 01-31-2011
Are you trying to get fields 3-24 from each row and assign each field to a variable? Or put them vertically? What is the greater problem?
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Programming

Missing Logic Looping Through Switch Statement

Having trouble with the logic when looping over this switch case again: for (j = 0; data != 0; j++){ switch(data){ case 'c': output = ranit(r_brace_array); break; case 'h': output = ranit(pipe_array); break; ... (6 Replies)
Discussion started by: Azrael
6 Replies

2. Shell Programming and Scripting

Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code. Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines. The "warning " is at last line of script. done < results 169 echo "END read all positioning parameters" 170... (8 Replies)
Discussion started by: annacreek
8 Replies

3. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

4. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

5. UNIX for Dummies Questions & Answers

Help with cut statement

Hi i have the below content in a file and i am trying to cut 5.4 , but when i use the below cut command nothing comes up . I am not sure what i am doing wrong. I am new to unix. Please help me, thanks for the help in advance. $ cat pid.txt 5.4 21399 ./PreRating $ cut -d ' ' -f1 pid.txt ... (8 Replies)
Discussion started by: nick1982
8 Replies

6. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies

7. Shell Programming and Scripting

Iterative statement to cut values from a line

Hi I am new to shell scripting and trying to get values from a text file, I have a text file with values seperated with "|". like aga|120220090525|120220090525|120220090525|120220090530 bab|120220090530|120220090530|120220090535|120220090535|120220090535... (4 Replies)
Discussion started by: mannepalli
4 Replies
Login or Register to Ask a Question