First 4 Characters of a column in CSV file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting First 4 Characters of a column in CSV file
# 1  
Old 08-04-2014
First 4 Characters of a column in CSV file

Hi All,

I have a CSV file in the below format:

Code:
Id,Text,File
"123","This is unix file","Unix"
"121","This is
C file","C"

Want to have output in below format:

Code:
Id,Text,File
"123","This","Unix"
"121","This","C"

File is , separator with each fields in quotes. Some fields are having new line in middle. Any inputs please.

Last edited by Franklin52; 08-04-2014 at 04:25 AM.. Reason: Please use code tags
# 2  
Old 08-04-2014
You could use the csv_parse functions (I created these a while ago based on Lorance Stinson's CSV Parser)
you would call them from awk like this:

Code:
{
  num_fields = csv_parse($0, csv)
  if (NR>1 && num_fields > 1) csv[2]= substr(csv[2],1,4)

  # param 3 (level) of 3 will force quotes around all fields
  # default is to only quote if needed (e.g. when field contains comma or newline)
  print csv_create(csv, num_fields, NR>1?3:0)
}

The full code is attached, including these CSV parsing functions; call it like this from the command line:

Code:
$ awk -f trim2.awk.txt infile
Id,Text,File
"123","This","Unix"
"121","This","C"


Last edited by Chubler_XL; 08-06-2014 at 08:24 PM..
# 3  
Old 08-04-2014
Chubler_XL ,

Thanks for response.

I am trying this:
Code:
#!/usr/bin/ksh
awk -v lengths=10,4,10 '
BEGIN { FS = ","  ; OFS = ","
        split(lengths,len,FS)
      }
      { n = 0
        while ( ++n <= NF ) $n = substr($n,1,len[n])
        print
      }' File

It works good but when I have input as:
Code:
Id,Text,File
"123","Th
is is unix file","Unix"

Its Giving:
Code:
Id,Text,File
"123","Th
is is unix

Its not accepting the new line when it comes in middle of the field.

Last edited by Franklin52; 08-04-2014 at 04:26 AM.. Reason: Please use code tags
# 4  
Old 08-04-2014
Yes, your program won't support CSV files with imbedded CR characters, as there is no code in it to do this.
# 5  
Old 08-04-2014
Input File...

Code:
cat file3.txt
"123","This is unix file","Unix"
"121","This is
 C file","C"

Script..
Code:
awk -F"," 'BEGIN{print "Id"",""Text"",""File";OFS=",";}
NF==3{split($2,a," ");print $1,a[1]"\"",$3}
NF!=3{getline x;t=sprintf("%s%s", $0,x);split(t,a,",");split(a[2],s," ");
print a[1],s[1]"\"",a[3]}' file3.txt

Output..
Code:
Id,Text,File
"123","This","Unix"
"121","This","C"

---------- Post updated at 10:05 AM ---------- Previous update was at 09:44 AM ----------

Sufer,
The above script can also take care of second scenario too..
Code:
Id,Text,File "123","Th is is unix file","Unix"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare every column from one csv file to another csv file

1.csv contains following column- Empid code loc port 101 A xy 01 102 B zx 78 103 A cg 12 104 G xy 78 2.csv contains follwing data- Empid code loc port 101 A gf 01 102 B zx 78 103 C cg 32 104 ... (1 Reply)
Discussion started by: rishabh
1 Replies

2. Shell Programming and Scripting

Get maximum per column from CSV file, based on date column

Hello everyone, I am using ksh on Solaris 10 and I'm gathering data in a CSV file that looks like this: 20170628-23:25:01,1,0,0,1,1,1,1,55,55,1 20170628-23:30:01,1,0,0,1,1,1,1,56,56,1 20170628-23:35:00,1,0,0,1,1,2,1,57,57,2 20170628-23:40:00,1,0,0,1,1,1,1,58,58,2... (6 Replies)
Discussion started by: ejianu
6 Replies

3. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

4. Emergency UNIX and Linux Support

How to convert the following characters in some fields in a csv file?

I have a csv file which is produced out of a SED command sed 's/|/","/g; s/^/"/; s/$/"/' A4.txt > A5.csv and I need either an addition to the SED command or a separate command to convert the following characters which occur within the fields in multiple lines 1) "=" to =" and 2)""~ to " (4 Replies)
Discussion started by: etldev
4 Replies

5. Shell Programming and Scripting

Remove the values from a certain column without deleting the Column name in a .CSV file

(14 Replies)
Discussion started by: dhruuv369
14 Replies

6. Shell Programming and Scripting

Pick the column value based on another column from .csv file

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated. Main imp point, in my .csv file, third column is having price value with comma (1,20,300), it has... (2 Replies)
Discussion started by: Ganesh L
2 Replies

7. Shell Programming and Scripting

Square like special characters in .csv file

Hi All - I am new to unix and scripting. I was asked to export a sql output into .csv format and email the attachment. I could do this however i see some square like boxes or special char at the end of the headings and data values in .csv file. I could not fix this and will need your help. ... (5 Replies)
Discussion started by: Sanchalla
5 Replies

8. Linux

Removing non printing characters from a csv file

Hi, I have an csv file and there are some non printable characters(extended ascii) so I am trying to create a clean copy of the csv file . I am using this command: tr -cd "" < /opt/informatica/PowerCenter8.6.0/server/infa_shared/SrcFiles/ThirdParty/locations.csv > ... (4 Replies)
Discussion started by: gerkus
4 Replies

9. UNIX for Dummies Questions & Answers

Replacing characters in csv file

Hello all, This is my first post here, so please excuse me if this question is too obvious or has been asked before. I am new to Unix and although I tried to search your forum for the answer to my question, I could not find an answer that would help me. I have a 500MB csv file with numeric values... (1 Reply)
Discussion started by: finwhiz
1 Replies
Login or Register to Ask a Question