Please help to create script using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please help to create script using awk
# 1  
Old 08-13-2010
Question Please help to create script using awk

I have one csv file and it contains values for 10 columns. Need to create one script using awk which will take the values from csv and write in a flat file.While writing the values from csv to flat file,one condition has to satisfy ie:-if suppose csv contains values as
Code:
abc,cd,e,f,gh,ij,kl,mn.op,qr

these values has to write in say examople:--in the form
Code:
abc   cd e   f     gh  ij    kl   mn             op  qr

space between abc and cd may 3 characters,space b/w cd and e may 5 characters..this way i ahve to write the values in flat file
please help to solve....

thanks in advance

Last edited by Franklin52; 08-13-2010 at 06:21 AM.. Reason: Please use code tags, thank you!
# 2  
Old 08-13-2010
Your question does not make a lot of sense....

try something like:

Code:
#  echo "abc,cd,e,f,gh,ij,kl,mn.op,qr" | tr ",." "  "
abc cd e f gh ij kl mn op qr

or

#  echo "abc,cd,e,f,gh,ij,kl,mn.op,qr" | nawk -F"[,.]" '{print $1"   "$2" "$3"   "$4"     "$5"  "$6"    "$7"   "$8"             "$9"  "$10}'
abc   cd e   f     gh  ij    kl   mn             op  qr

Have a play with these, and see if you can get what you want. Otherwise, if you define your problem a little more clearly it would help.

HTH
# 3  
Old 08-13-2010
thanks for the reply..i will make it more clear......

my csv file has 10 columns
eg:-
A,1,2,3,4,5,6,7,8,9
B,1,2,3,4,5,6,7,8,9
c,1,2,3,4,5,6,7,8,9
-----------------

I have to create a flat file from this.Conditions are:if the first column is "A",then take 1st 3rd,5th and 9th column from csv file and write to flat file.IF the first record is C then take 2nd.4th and 8th column and write to same flat file..like that..
one more condition is :-
at the time of writing values from csv to flat file,if suppose for record A,it will take 1st 3rd,5th and 9th column from csv file and while writing,after 1st column,give some spaces say 4 characters ,then write 3rd column,after that give some space say 5 characters and write 5th column......
# 4  
Old 08-13-2010
ok..

something along lines of:

Code:
#  awk -F, '$1=="A"{print $1,$3,$5,$9}$1=="c"{print $2,$4,$8}' infile
A 2 4 8
1 3 7

or
#  awk -F, '$1=="A"{print $1"    "$3"        "$5,$9}$1=="c"{print $2,$4,$8}' infile
A    2        4 8
1 3 7

Closer to what you're after ?
# 5  
Old 08-13-2010
thanks tyta..i will check this logic.....

another one is:---
from this csv file
suppose 10 columns:-
A,1,2,3,4,5,6,7,8,9
B,1,2,3,6,7,8,9
c,1,2,3,4,5,9
-----------------
just need to create one flat file.only condition is each columns has fixed length size..say

length of column 1 =1,length of column 2=3,length of column 3=5,length of column4=15....so at the time of writing values from csv to flat file,this format has to follow.
if supppose here 3 rd column is "2" and while writing to flat file length of column 3=5 is declared,so that after 2 four space then only another ....
# 6  
Old 08-13-2010
you can prob deal with that using printf e.g.

Code:
#  awk -F, '{printf("%5s%3s%7s\n",$1,$2,$3)}' infile
    A  1      2
    B  1      2
    c  1      2

HTH
# 7  
Old 08-16-2010
thanks....

Last edited by Sanal; 08-17-2010 at 09:26 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk and sed script to create one output CSV file

Hi All , I would require your help to generate one output file after post processing of one CSV file as stated below This file is just a small cut from a big file . Big file is having 20000 lines PATTERN,pat0,pat1,pat2,pat3,pat4,pat5,pat6,pat7,pat8,pat9... (2 Replies)
Discussion started by: kshitij
2 Replies

2. Shell Programming and Scripting

awk to create variables to pass into a bash loop to create a download link

I have created one file that contains all the necessary info in it to create a download link. In each of the lines /results/analysis/output/Home/Auto_user_S5-00580-6-Medexome_67_032/plugin_out/FileExporter_out.67... (8 Replies)
Discussion started by: cmccabe
8 Replies

3. Shell Programming and Scripting

Using awk and sed to modify a create sql script

Hi, I have a file which contains the following data claim_src|clm_id,typ_id pat_src|pat_id prov_src|prov_id,clm_id,prov_name The first field is table name and second field is primary keys of the table Now I have three files which contain ddl of each table. clam_src.sql... (4 Replies)
Discussion started by: wahi80
4 Replies

4. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

5. Shell Programming and Scripting

Wrap lines with awk to create SQL script

Greetings! Some of my files list hardware errors (we test electronic components), some have none. If the file name has no errors, I still want to display a message like "No error", else I display the error from the file itself. I came up with this (with help) for myfile in `find . -name... (2 Replies)
Discussion started by: alan
2 Replies

6. Homework & Coursework Questions

Create script to add user and create directory

first off let me introduce myself. My name is Eric and I am new to linux, I am taking an advanced linux administration class and we are tasked with creating a script to add new users that anyone can run, has to check for the existence of a directory. if the directory does not exist then it has... (12 Replies)
Discussion started by: pbhound
12 Replies

7. Shell Programming and Scripting

Awk script to create new file based on previous line

Need help creating a script that does the following: Sort a file Compare the previous line "last field" with current line "last field" If they are the same, print output to a file If they are different print output to a new file The script should keep creating new files if the previous... (5 Replies)
Discussion started by: Muga801
5 Replies

8. Shell Programming and Scripting

create a shell script that calls another script and and an awk script

Hi guys I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file. #!/bin/sh echo " $2 $3 $4 $5 $6 $7 isql -w400 -U$2 -S$5 -P$3 << xxx use $4 go print"**Changes to the table... (0 Replies)
Discussion started by: magikminox
0 Replies

9. Shell Programming and Scripting

create function with awk

hi all, i print four variable delimited by comma with awk : awk -F "," '{print $1;$2;$3;$4}' ' if $3="" code ... } i want to extract this information from another file using another awk using the $1 printed awk -v { code } but the problem i can't use two awk in the same code... (2 Replies)
Discussion started by: kamel.seg
2 Replies

10. Shell Programming and Scripting

help with awk to create report

Hi, I am trying to create a report using the following syntax: #!/bin/awk -f #script name: users_report BEGIN { FS=":" ; OFS="\t" ; print "User\tGID\tUser Name\tHome Dir\t" { print $1 , $3 , $5 , $6 } END { print "\n End of Report \n" } $> user_report /etc/passwd the output of... (5 Replies)
Discussion started by: ghazi
5 Replies
Login or Register to Ask a Question