Format Fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format Fields
# 1  
Old 04-29-2011
Format Fields

I have a CSV file like this:
Code:
abc   ,def ghi      ,jkl
mno  ,pqr stu      ,vwx

Desired Output:
Code:
Field 1: 
abc
mno
Field 2: 
def ghi
pqr stu
Field 3: 
jkl
vwx

This does not work:
Code:
for i in `cat file.txt`
do
  Field1=`echo $i | awk -F ',' '{print $1}'| sed 's/^ *// ; s/ *$//'`
  Field2=`echo $i | awk -F ',' '{print $2}'| sed 's/^ *// ; s/ *$//'`
  Field3=`echo $i | awk -F ',' '{print $3}'| sed 's/^ *// ; s/ *$//'`
  echo "$Field1";
  echo "$Field2";
  echo "$Field3";
done


Last edited by Franklin52; 04-29-2011 at 04:29 AM.. Reason: Please use code tags and indentation
# 2  
Old 04-29-2011
If you have very less number of fields the you can use cut command.
Code:
echo "field 1:";cut -d"," -f1 infile;echo "field 2:";cut -d"," -f2 infile;echo "field 3:";cut -d"," -f3 infile

# 3  
Old 04-29-2011
I have 14 fields. I have to insert them in the oracle database. That is why i am going through the loop.
# 4  
Old 04-29-2011
If i go from the pravin27 idea, we can do a loop like that :
Code:
for i in $(seq 1 14); do
    echo "Field ${i}:"
    cut -d',' -f${i} infile
done

If i go from you idea
Code:
for i in $(seq 1 14)
do
    echo "Field ${i}:"
    awk -F',' "{print \$${i}}" file.txt | sed 's/^ *//; s/ *$//'
done

# 5  
Old 04-29-2011
I need to store each field in a variable as i need to insert in the database.

chirel: I do not get correct output:
Code:
for i in $(seq 1 14); do
    echo "Field ${i}:"
    cut -d',' -f${i} infile
done

Output:
Code:
Field 1;
abc def
mno pqr
Field 2;
abc def
mno pqr
Field 3;
abc def
mno pqr

Code:
for i in $(seq 1 14)
do
    echo "Field ${i}:"
    awk -F',' "{print \$${i}}" file.txt | sed 's/^ *//; s/ *$//'
done

Output:
Code:
Field 1;
abc def
mno pqr
Field 2;

Field 3;


Last edited by Scott; 04-30-2011 at 04:09 PM.. Reason: Code tags, please...
# 6  
Old 04-29-2011
I dunno where you did it wrong, because in all the test i can make i have exactly the output you want.

Here is a sample screen log :

Code:
chirel@linux$ cat file.txt
abc   ,def ghi      ,jkl      ,     abc, def, ghi, jkl, mno, pqr, stu, vwx, yz, 012, 345
MNO  ,PQR STU      ,VWX      ,     ABC, DEF, GHI, JKL, MNO, PQR, STU, VWX, YZ, 012, 345
Abc   ,Def Ghi      ,Jkl      ,     Abc, Def, Ghi, Jkl, Mno, Pqr, Stu, Vwx, Yz, 012, 345
chirel@linux$ for i in $(seq 1 14); do echo "Field ${i}:"; awk -F',' "{print \$${i}}" file.txt | sed 's/^ *//; s/ *$//'; done
Field 1:
abc
MNO
Abc
Field 2:
def ghi
PQR STU
Def Ghi
Field 3:
jkl
VWX
Jkl
Field 4:
abc
ABC
Abc
Field 5:
def
DEF
Def
Field 6:
ghi
GHI
Ghi
Field 7:
jkl
JKL
Jkl
Field 8:
mno
MNO
Mno
Field 9:
pqr
PQR
Pqr
Field 10:
stu
STU
Stu
Field 11:
vwx
VWX
Vwx
Field 12:
yz
YZ
Yz
Field 13:
012
012
012
Field 14:
345
345
345
chirel@linux$

# 7  
Old 04-29-2011
Chirel:
It was my mistake. I was reading the wrong text file.
Now, I need to store values in the database. So i need to assign it to variables.
Can i do it this way? My oracle is not installed yet. Not able to test. Will this work?
Code:
#!/usr/bin/ksh
for i in $(seq 1 14)
do 
  $col1=echo "col ${i}:"; awk -F',' "{print \$${i}}" file.txt | sed 's/^ *//; s/ *$//'
  $col2=echo "col ${i}:"; awk -F',' "{print \$${i}}" file.txt | sed 's/^ *//; s/ *$//'
  sqlplus -s  user/password@oracleinstance <<eof
INSERT INTO table (col1, col2, ...)
VALUES
#($col1, $col2,... )
#commit;
#exit;
#eof

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

awk to format file and combine two fields using comma

I am trying to use awk to format the file below, which is tab-delimited. The desired out is space delimited and is in the order of $9 $13 $2 $10-$11.$10 and $11 are often times multiple values separated by a comma, so the value in $10 is combined with the first value from $11 using the comma.... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Print . in blank fields to prevent fields from shifting

The below code works great, kindly provided by @Don Cragun, the lines in bold print the current output. Since some of the fields printed can be blank some of the fields are shifted. I can not seem too add . to the blank fields like in the desired output. Basically, if there is nothing in the field... (10 Replies)
Discussion started by: cmccabe
10 Replies

4. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

5. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

6. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

7. Shell Programming and Scripting

Using sed to format several fields

I have data that is tab delimited and looks like: /dev/dsk/c0t0d0s1 - - swap - no - /dev/dsk/c0t0d0s0 /dev/rdsk/c0t0d0s0 / ufs 1 no - /dev/dsk/c0t0d0s6 /dev/rdsk/c0t0d0s6 /usr ufs 1 no -... (5 Replies)
Discussion started by: bradg
5 Replies

8. Shell Programming and Scripting

awk,cut fields by change field format

Hi Everyone, # cat 1.txt 1321631,77770132976455,19,20091001011859,20091001011907 1321631,77770132976455,19,20091001011859,20091001011907 1321631,77770132976455,19,20091001011859,20091001011907 # cat 1.txt | awk -F, '{OFS=",";print $1,$3,$4,$5}' 1321631,19,20091001011859,20091001011907... (7 Replies)
Discussion started by: jimmy_y
7 Replies

9. UNIX for Advanced & Expert Users

Format problems with fields

The following output has a space as the Field Separator. I need: $1 Set the field width to 15 then zero-fill to the right. $6 Set the field width to 15 then zero-fill to the left. 01-10016 1000 MV010 20090708 12003 $NK0015101 01 01-100161 12000 MV070 20090708 12003 $NK0015201 01... (6 Replies)
Discussion started by: talk2pawee
6 Replies

10. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies
Login or Register to Ask a Question