Sponsored Content
Top Forums UNIX for Dummies Questions & Answers unix command to insert double quotes in a delimited file Post 302430302 by ygemici on Thursday 17th of June 2010 05:48:00 AM
Old 06-17-2010
Quote:
Originally Posted by Scrutinizer
Hi radoulov, this would be equivalent to this no?
Code:
sed 's/[^,]*/"&"/g' infile

The first will also delete spaces in fields
The second is only for 4 fields
You would have to do something like this to get rid of spurious whitespace:
Code:
sed 's/^[ \t]*/"/; s/[ \t]*,[ \t]*/","/g; s/[ \t]*$/"/' infile

Thanks for your information Smilie

Code:
# cat infile
153665031,abc,abc ,abc, defdasdasd,dasdasdas,dasdasdpjqwopw,dasdasdqwdqw
131278839,def,def ,dec,sdasdadasd,dasdasdas,ferfefer,fergtrghtynu,nynjyunsdasasasd,dasdadas
179821481,efg,efg ,lmn,ijjjjopkpkpd   ,  asjpdjasda         , dsddjqwqwddw
fergtrgdasdasdas,adadaa,dasdasdasdasdd          ,dasdasdasddas          das,,
fergtrghtynu,sdasda  , dddwddasd , 12121231223r4 ,e2e2e ,12121

Code:
# sed -e 's/ *//g' -e 's/\t//g' -e 's:\([[:alnum:]]*[[:digit:]]*\),:"\1",:g' \
> 's:",\([[:alnum:]][[:alnum:]]*\):","\1":' -e 's:",\([[:digit:]][[:digit:]]*\):","\1":' infile
"153665031","abc","abc","abc","defdasdasd","dasdasdas","dasdasdpjqwopw","dasdasdqwdqw"
"131278839","def","def","dec","sdasdadasd","dasdasdas","ferfefer","fergtrghtynu","nynjyunsdasasasd","dasdadas"
"179821481","efg","efg","lmn","ijjjjopkpkpd","asjpdjasda","dsddjqwqwddw"
"fergtrgdasdasdas","adadaa","dasdasdasdasdd","dasdasdasddasdas","",
"fergtrghtynu","sdasda","dddwddasd","12121231223r4","e2e2e","12121"

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do I insert double quotes

Hi all, I have a spreadsheet with 20 fields(columns) in which the fifth column is a numerical column with records such as 00024,00455, 00234,556670,465700,00456 etc. I want to consider the leading zeros for this column so that I can use some logic. Now the problem is that I am unable to add... (3 Replies)
Discussion started by: dsean
3 Replies

2. Shell Programming and Scripting

unix command to insert double quotes

Hi, I am looking for a unix command which inserts double quotes around all values in a text file. For example, Input: 1234 200 4686 3506056 9646 457 5647 5066762 5656 366 5869 5978459 Output: "1234" "200" "4686" "3506056" "9646" "457" "5647" "5066762" "5656" "366" "5869"... (2 Replies)
Discussion started by: berlin_germany
2 Replies

3. Shell Programming and Scripting

ksh execute command containing double quotes

How do I execute a command containing a double quote ? I pass a variable to grep that contains spaces, so I need to quote it, but it does not work. #!/usr/bin/ksh set -x txt='"next to"' cmd="grep $txt ~dpearso5/file2" echo $cmd $cmd This is the error I get: + grep "next to"... (1 Reply)
Discussion started by: pearson05
1 Replies

4. Shell Programming and Scripting

Parse comma delimited and optionally quotes dimilited file

Hi, Can you please help me? How to Parse a comma delimited and optionally quotes dimilited file? sample.dat ---------- "I",+2007,"SANDA, 20, MARTIN PLACE","SANDA 20MARTIN" "D",+2008,"RANDA, 22, MARTIN PLACE","RANDA 22MARTIN" Thank you. Ram (8 Replies)
Discussion started by: vramana2003
8 Replies

5. Shell Programming and Scripting

insert a field into a tab delimited file

Hello, Can someone help me to do this with awk or sed? I have a file with multiple lines, each line has many fields separated with a tab. I would like to add one more field holding 'na' in between the first and second fields. old file looks like, 1, field1 field2 field3 ... 2, field1... (7 Replies)
Discussion started by: ssshen
7 Replies

6. Shell Programming and Scripting

How to pass a parameter with double quotes around it to a command

In a bash script, I need to pass a parameter that has space in-between using double quotes as follows: CMD="SomeExecutable" # Parameter that has a space in-between PARAM1="TIMO 2" CMD_IN="--name=\"$PARAM1\"" CMD_OUT=`$CMD $CMD_IN` expected/required command execution:... (4 Replies)
Discussion started by: Timo
4 Replies

7. Shell Programming and Scripting

How to remove extra double quotes from string in a delimited file??

Hi Unix Gurus.. I am new to Unix. Please help me. The file I am getting is as follows: Input File "2011-07-06 03:53:23","0","I","NOT SET ",,,,"123985","SAW CUT CONCRETE SLAB 20"THICK",,"98.57","","EACH","N" "2011-07-06 03:53:23","0","I","NOT SET ",,,,"204312","ARMAFLEX-1 3/8 X... (2 Replies)
Discussion started by: BICC
2 Replies

8. Shell Programming and Scripting

Convert csv to pipe delimited except the ones in double quotes

I have a csv data file : A,B,C,D,"A,B",E,"GG,H" E,F,G,H,I,J,"S,P" I need to replace all "," with "|" except the ones between double quotes i.e A|B|C|D|"A,B"|E|"GG,H" E|F|G|H|I|J|"S,P" CAn someone assist? (8 Replies)
Discussion started by: Shivdatta
8 Replies

9. Shell Programming and Scripting

Replace Double quotes within double quotes in a column with space while loading a CSV file

Hi All, I'm unable to load the data using sql loader where there are double quotes within the double quotes As these are optionally enclosed by double quotes. Sample Data : "221100",138.00,"D","0019/1477","44012075","49938","49938/15043000","Television - 22" Refurbished - Airwave","Supply... (6 Replies)
Discussion started by: mlavanya
6 Replies

10. Shell Programming and Scripting

Printing double quotes in echo command

Please help me to use echo or printf type of command to print some value from variable within double quotes - I want to print the double quote ( " ") also. I tried #!/bin/bash VALUE=some_value echo '{"value" : "$VALUE"}' I was expecting the above script would produce .. {"value" :... (3 Replies)
Discussion started by: atanubanerji
3 Replies
Char(3pm)						User Contributed Perl Documentation						 Char(3pm)

NAME
PDL::Char -- PDL subclass which allows reading and writing of fixed-length character strings as byte PDLs SYNOPSIS
use PDL; use PDL::Char; my $pchar = PDL::Char->new( [['abc', 'def', 'ghi'],['jkl', 'mno', 'pqr']] ); $pchar->setstr(1,0,'foo'); print $pchar; # 'string' bound to "", perl stringify function # Prints: # [ # ['abc' 'foo' 'ghi'] # ['jkl' 'mno' 'pqr'] # ] print $pchar->atstr(2,0); # Prints: # ghi DESCRIPTION
This subclass of PDL allows one to manipulate PDLs of 'byte' type as if they were made of fixed length strings, not just numbers. This type of behavior is useful when you want to work with charactar grids. The indexing is done on a string level and not a character level for the 'setstr' and 'atstr' commands. This module is in particular useful for writing NetCDF files that include character data using the PDL::NetCDF module. FUNCTIONS
new Function to create a byte PDL from a string, list of strings, list of list of strings, etc. # create a new PDL::Char from a perl array of strings $strpdl = PDL::Char->new( ['abc', 'def', 'ghij'] ); # Convert a PDL of type 'byte' to a PDL::Char $strpdl1 = PDL::Char->new (sequence (byte, 4, 5)+99); $pdlchar3d = PDL::Char->new([['abc','def','ghi'],['jkl', 'mno', 'pqr']]); string Function to print a character PDL (created by 'char') in a pretty format. $char = PDL::Char->new( [['abc', 'def', 'ghi'], ['jkl', 'mno', 'pqr']] ); print $char; # 'string' bound to "", perl stringify function # Prints: # [ # ['abc' 'def' 'ghi'] # ['jkl' 'mno' 'pqr'] # ] # 'string' is overloaded to the "" operator, so: # print $char; # should have the same effect. setstr Function to set one string value in a character PDL. The input position is the position of the string, not a character in the string. The first dimension is assumed to be the length of the string. The input string will be null-padded if the string is shorter than the first dimension of the PDL. It will be truncated if it is longer. $char = PDL::Char->new( [['abc', 'def', 'ghi'], ['jkl', 'mno', 'pqr']] ); $char->setstr(0,1, 'foobar'); print $char; # 'string' bound to "", perl stringify function # Prints: # [ # ['abc' 'def' 'ghi'] # ['foo' 'mno' 'pqr'] # ] $char->setstr(2,1, 'f'); print $char; # 'string' bound to "", perl stringify function # Prints: # [ # ['abc' 'def' 'ghi'] # ['foo' 'mno' 'f'] -> note that this 'f' is stored "f" # ] atstr Function to fetch one string value from a PDL::Char type PDL, given a position within the PDL. The input position of the string, not a character in the string. The length of the input string is the implied first dimension. $char = PDL::Char->new( [['abc', 'def', 'ghi'], ['jkl', 'mno', 'pqr']] ); print $char->atstr(0,1); # Prints: # jkl perl v5.14.2 2011-03-30 Char(3pm)
All times are GMT -4. The time now is 07:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy