Sponsored Content
Full Discussion: Handling blank spaces
Top Forums Shell Programming and Scripting Handling blank spaces Post 302340840 by techmoris on Tuesday 4th of August 2009 02:13:18 PM
Old 08-04-2009
Handling blank spaces

Hi,

I am trying to replace a specific column values in a csv file with double quotes when I am find embedded spaces with in the fields.

Example:

SNO,NAME,ZIPCODE,RANK,SEX,ADDRESS
1,Robert,74538,12,34, M,Robert Street, NY
2,Sam,07564,13,M,12 Main Ave, CA
3,Kim, Ed,12345,14,M,123D , MN

Desired Output:

SNO,NAME,ZIPCODE,RANK,SEX,ADDRESS
1,Robert Ken,74538,12,"34, Robert Street, NY"
2,Sam Mik,"07564",13,"12 Main Ave, CA"
3,"Kim, Ed",12345,14,"123D , MN"

As per my requirement, I was able to replace the ZIPCODE value with double quotes when I find a leading zeros for the zipcode. Also, I would like to replace the name in double quotes when I find a embedded comma with in the NAME.

Can someone tell me how to handle the embedded spaces(spaces can be one or many) and comma with in a field value as per the above example in the ADDRESS field.

following code was able to handly ZIPCODE and NAME

sed -e 's/,\(0[0-9]*\)/,\"\1\"/g' -e 's/,\([A-Za-z]*, [A-Za-z]*\),/,\"\1\",/g' tempfile.csv > file.csv

Thanks Smilie-

---------- Post updated at 02:13 PM ---------- Previous update was at 12:07 PM ----------

I got it...

sed -e 's/,\(0[0-9]*\)/,\"\1\"/g' -e 's/,\([ 0-9A-Za-z]*, [ 0-9A-Za-z]*\),/,\"\1\",/g' -e 's/,\([ 0-9A-Za-z]*, [ 0-9A-Za-z]*\),/,\"\1\",/g' file.csv
> tempfile.csv

Thanks!!

Last edited by techmoris; 08-04-2009 at 01:13 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

handling spaces in unix

I am testing a ksh script for email. In the script I receive several parameters. One of them is a subject. The subject may contain spaces. Ex. Test this. When I am running the script on telnet to test, how should the syntax at the command line be written. I have this: ksh ResendE.sh '001111'... (2 Replies)
Discussion started by: supercbw
2 Replies

2. Shell Programming and Scripting

blank spaces getting deleted

I have to filter out data from a file based on the value of first three characters of each record I have used the following logic FIN=$LOC/TEST2.TXT FEEDFILE=$LOC/TEST1.TXT while read FDROW do FEEDROW=$FDROW; DTYPE=`echo $FEEDROW |cut -c 1-3` if ; then echo $FEEDROW >> $FIN... (5 Replies)
Discussion started by: gander_ss
5 Replies

3. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

4. UNIX for Dummies Questions & Answers

handling white spaces with getopt

Hi I'm trying to ensure that I have catered for all situations with my getopt cases. One other situation I want to cover is should the user enter the script without any preceding arguments eg: ./script_eg I need the script to the direct the user to the helpfile I have tried... (3 Replies)
Discussion started by: ladyAnne
3 Replies

5. Shell Programming and Scripting

Problems with Blank Spaces

Hi to all. How can I pass to the stat command a file path with blank spaces? And another question, if I use stat command like this: stat / -name "*.sh" -user $user_name -exec stat -c %n%x {} \; How can I get the result with a ":" into the name of the file and the time of the last... (4 Replies)
Discussion started by: daniel.gbaena
4 Replies

6. Shell Programming and Scripting

Handling directory with spaces in for loops

Hi everyone, I have been a big fan here since a couple years (since I started being an admin ...) and finally decided to become a member and help ppl and perhaps being helped Now I have a problem that might interest some of the gurus. I am abig fan of what I call "one liners". I am trying... (2 Replies)
Discussion started by: plmachiavel
2 Replies

7. Shell Programming and Scripting

Remove blank spaces

Gents, Please can you help me.. to remove blank spaces :) Input ABSOLUTE , ,FALSE ,1035 ,28 ,669 ,1817.0 ,CORREL BEFORE ,1 ABSOLUTE , ,FALSE ,1035 ,28 ,686 ,1817.0 ,CORREL BEFORE ,1 ABSOLUTE , ,FALSE ,1035 ,28 ,670 ,1819.0 ,CORREL BEFORE ,1 ABSOLUTE , ,FALSE ... (4 Replies)
Discussion started by: jiam912
4 Replies

8. UNIX for Advanced & Expert Users

File Processing: Handling spaces in a line

Hi All, Iam trying to get a file processed and some lines have spaces...the below is not working Want to remove empty line Want to remove lines that start with # Avoid line with substring WHOA When trying to get the substring from the var also Iam having trouble file is like VAR=VALUE,... (13 Replies)
Discussion started by: baanprog
13 Replies

9. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

10. Shell Programming and Scripting

Handling filenames with spaces

I'm trying to handle some files with spaces in their name using "" or \ . Like "file 1" or file\ 1. My current confusion can be expressed by the following shell script: #!/bin/bash touch "file 1" "file 2" echo -n "ls: " ; ls echo --- for file in "file 1" "file 2" ; do echo $file... (9 Replies)
Discussion started by: Ralph
9 Replies
dgelsx.f(3)							      LAPACK							       dgelsx.f(3)

NAME
dgelsx.f - SYNOPSIS
Functions/Subroutines subroutine dgelsx (M, N, NRHS, A, LDA, B, LDB, JPVT, RCOND, RANK, WORK, INFO) DGELSX solves overdetermined or underdetermined systems for GE matrices Function/Subroutine Documentation subroutine dgelsx (integerM, integerN, integerNRHS, double precision, dimension( lda, * )A, integerLDA, double precision, dimension( ldb, * )B, integerLDB, integer, dimension( * )JPVT, double precisionRCOND, integerRANK, double precision, dimension( * )WORK, integerINFO) DGELSX solves overdetermined or underdetermined systems for GE matrices Purpose: This routine is deprecated and has been replaced by routine DGELSY. DGELSX computes the minimum-norm solution to a real linear least squares problem: minimize || A * X - B || using a complete orthogonal factorization of A. A is an M-by-N matrix which may be rank-deficient. Several right hand side vectors b and solution vectors x can be handled in a single call; they are stored as the columns of the M-by-NRHS right hand side matrix B and the N-by-NRHS solution matrix X. The routine first computes a QR factorization with column pivoting: A * P = Q * [ R11 R12 ] [ 0 R22 ] with R11 defined as the largest leading submatrix whose estimated condition number is less than 1/RCOND. The order of R11, RANK, is the effective rank of A. Then, R22 is considered to be negligible, and R12 is annihilated by orthogonal transformations from the right, arriving at the complete orthogonal factorization: A * P = Q * [ T11 0 ] * Z [ 0 0 ] The minimum-norm solution is then X = P * Z**T [ inv(T11)*Q1**T*B ] [ 0 ] where Q1 consists of the first RANK columns of Q. Parameters: M M is INTEGER The number of rows of the matrix A. M >= 0. N N is INTEGER The number of columns of the matrix A. N >= 0. NRHS NRHS is INTEGER The number of right hand sides, i.e., the number of columns of matrices B and X. NRHS >= 0. A A is DOUBLE PRECISION array, dimension (LDA,N) On entry, the M-by-N matrix A. On exit, A has been overwritten by details of its complete orthogonal factorization. LDA LDA is INTEGER The leading dimension of the array A. LDA >= max(1,M). B B is DOUBLE PRECISION array, dimension (LDB,NRHS) On entry, the M-by-NRHS right hand side matrix B. On exit, the N-by-NRHS solution matrix X. If m >= n and RANK = n, the residual sum-of-squares for the solution in the i-th column is given by the sum of squares of elements N+1:M in that column. LDB LDB is INTEGER The leading dimension of the array B. LDB >= max(1,M,N). JPVT JPVT is INTEGER array, dimension (N) On entry, if JPVT(i) .ne. 0, the i-th column of A is an initial column, otherwise it is a free column. Before the QR factorization of A, all initial columns are permuted to the leading positions; only the remaining free columns are moved as a result of column pivoting during the factorization. On exit, if JPVT(i) = k, then the i-th column of A*P was the k-th column of A. RCOND RCOND is DOUBLE PRECISION RCOND is used to determine the effective rank of A, which is defined as the order of the largest leading triangular submatrix R11 in the QR factorization with pivoting of A, whose estimated condition number < 1/RCOND. RANK RANK is INTEGER The effective rank of A, i.e., the order of the submatrix R11. This is the same as the order of the submatrix T11 in the complete orthogonal factorization of A. WORK WORK is DOUBLE PRECISION array, dimension (max( min(M,N)+3*N, 2*min(M,N)+NRHS )), INFO INFO is INTEGER = 0: successful exit < 0: if INFO = -i, the i-th argument had an illegal value Author: Univ. of Tennessee Univ. of California Berkeley Univ. of Colorado Denver NAG Ltd. Date: November 2011 Definition at line 178 of file dgelsx.f. Author Generated automatically by Doxygen for LAPACK from the source code. Version 3.4.2 Tue Sep 25 2012 dgelsx.f(3)
All times are GMT -4. The time now is 06:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy