The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
existing file to a fixed length file cmanand Shell Programming and Scripting 3 01-25-2008 01:50 PM
Extracting data from text file based on configuration set in config file suparnbector Shell Programming and Scripting 3 08-09-2007 11:25 PM
search file, change existing value based on input (awk help) nortonloaf Shell Programming and Scripting 3 12-05-2006 09:35 PM
i want to delete a file based on existing file in a directory srivsn Shell Programming and Scripting 3 04-11-2006 01:38 AM
Creating a Mirror RAID With Existing Disk sysera Filesystems, Disks and Memory 2 02-08-2004 09:40 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-02-2007
Registered User
 

Join Date: Dec 2007
Posts: 2
Creating a csv file based on Existing file

Hi I am Newbie to Unix.Appreciate Help from forum

user would loada b.Csv File(Below example) in /data/m/ directory.Program need to read the b.csc to extract certain column and create a new file /data/d/ directory as csv file with new name.

User File Format

1232,samshouston,12345 houston,56666,20030811,78576,5,EA,789,SHELL
1456,dellshouston,2222 houston, 5644666,20030811,78576,5,EA,789,dell

need to extract column1,column3,column4,column5,column7
and one more condition if the column4 length is lessthan 6 then add 000 before

output wil look like in /data/o/tran.csv

1232,12345 houston,00056666,20030811,78576
1456,2222 houston,5644666,20030811,78576



Appreciate your help

thanks

Skyway
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-02-2007
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,035
Thats quite straightforward. Find a script skeleton below:

Code:
#! /bin/ksh
typeset    fInput="/data/m/b.csc"      # input filename
typeset    fOutput="/data/o/tran.csv"  # output filename

if [ -r "$fInput" ] ; then     # perhaps there should be more input validation
     print -u2 "unable to read input file"
     exit 1
fi

sed 's/^\([^,]*,[^,]*,[^,]*,\)\([0-9]\{1,5\}\),/\1000\2/
     s/^\([^,]*,\)[^,]*,\([^,]*,[^,]*,[^,]*,\)[^,]*,\([^,]*\).*$/\1\2\3/' $fInput > $fOutput

exit $?
Hope this helps.

bakunin
Reply With Quote
  #3 (permalink)  
Old 12-02-2007
Moderator
 

Join Date: Feb 2007
Posts: 1,665
Or with awk:

Code:
awk -F, '{printf("%s,%s,%s,%0.8d,%s,%s\n", $1,$3,$4,$5,$6,$7)}' infile > outfile
Regards
Reply With Quote
  #4 (permalink)  
Old 12-02-2007
Registered User
 

Join Date: Dec 2007
Posts: 2
Thumbs up

I really Appreciate immediate Replyt.I am Going to Try both methods Today

thanks

Skyway
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0