Modify CSV file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modify CSV file
# 1  
Old 11-19-2016
Modify CSV file

Hi, I would like to change my CSV file by adding " and : and moving some of the information around. the CSV file looks as follows:
Code:
501254424;500440257;PE PACKS;300467279;PREP;;276476070;655031001867176;Two Block;Olga;25/12/2015 00:00:00;Olga

I would like to move the field 7 to the front " and followed by the : and putting the rest of the CSV information in "". How can I do this with a shell script?

The desired output should look like this:
Code:
"276476070": "501254424;500440257;PE PACKS;300467279;PREP;;276476070;655031001867176;Two Block;Olga;25/12/2015 00:00:00;Olga"

# 2  
Old 11-19-2016
Any attempts/ideas/thoughts from your side?
# 3  
Old 11-19-2016
I tried the following:
Code:
awk 'BEGIN { FS=";"; OFS=";"; } {print $7,$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13}' file

however this does not work and how to add the " and : I have not found a solution.
# 4  
Old 11-19-2016
How about
Code:
awk -F";" -vDQ='"' '{print DQ $7 DQ ": " DQ $0 DQ}' file4
"276476070": "501254424;500440257;PE PACKS;300467279;PREP;;276476070;655031001867176;Two Block;Olga;25/12/2015 00:00:00;Olga"

# 5  
Old 11-19-2016
Thank you very much that is amazing, it works.

When I echo the line and run the filter all is working as expected, however if I run the statement against a file, the last Double Quote is not present, any idea why?

Here is what I use:
Code:
 awk -F";" -vDQ='"' '{print DQ "264" $7 DQ ": " DQ $0 DQ}' /archive/ftp/SUBSDATA_001_20161118235001.txt

---------- Post updated at 09:53 PM ---------- Previous update was at 09:35 PM ----------

I have it now like this in the script:
Code:
#!/bin/bash
file="/archive/ftp/TNM_SUBSDATA_001_20161118235001.txt"
while IFS= read -r line
do
 awk -F";" -vDQ='"' '{print DQ "264" $7 DQ ": " DQ $0 DQ }' >> /tmp/output.txt
done <"$file"

However the last " behind the text is not present in the output file?
# 6  
Old 11-19-2016
Hi,

When i try the command RudiC ( including your addition of 264 in latest post ) it works as expected.

Code:
cat file
501254424;500440257;PE PACKS;300467279;PREP;;276476070;655031001867176;Two Block;Olga;25/12/2015 00:00:00;Olga
501254424;500440257;PE PACKS;300467279;PREP;;27647xxxx;655031001867176;Two Block;Olga;25/12/2015 00:00:00;awk

Code:
awk -F";" -vDQ='"' '{print DQ "264" $7 DQ ": " DQ $0 DQ}' file > output

Gives output as follows:
Quote:
"264276476070": "501254424;500440257;PE PACKS;300467279;PREP;;276476070;655031001867176;Two Block;Olga;25/12/2015 00:00:00;Olga"
"26427647xxxx": "501254424;500440257;PE PACKS;300467279;PREP;;27647xxxx;655031001867176;Two Block;Olga;25/12/2015 00:00:00;awk"

Last edited by greet_sed; 11-19-2016 at 04:20 PM.. Reason: Add quotes
# 7  
Old 11-19-2016
Quote:
Originally Posted by omuhans123
...

[COLOR="#738fbf"][SIZE=1]...
I have it now like this in the script:
Code:
#!/bin/bash
file="/archive/ftp/TNM_SUBSDATA_001_20161118235001.txt"
...
...

However the last " behind the text is not present in the output file?
I notice that you have "ftp" in your path and my wild guess is that you are doing ftp from one OS to another and that is messing up your end-of-line characters, due to which you don't see the last character.

Do you observe the missing character after an ftp of "output.txt" from Unix/Linux to Windows?

First thing to check is that the "output.txt" is formed correctly in your Unix/Linux box. Run your script on a small data file (maybe 3 or 4 lines) and once "output.txt" is created, run the following command:

Code:
od -bc output.txt

You should see "\n" as the end-of-line character in Unix/Linux.
If you see "\r\n" as the end-of-line character, then that file is in DOS (or Windows) format.
If you are doing an ftp of the file, ensure that you do a binary ftp and not ascii. The ftp program usually takes care of end-of-line character unless you override it.

If the assumptions above are not true, then your problem might lie somewhere else.
You could start with showing us the output of "od -bc output.txt".
Also, what exactly do you do after the "output.txt" is created to find that the last character is missing?
Do you open it in vim/emacs/nano etc? Do you transfer it somewhere and then open it in some editor?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modify csv-files with awk

Hello everyone! I have thousands of csv files I have to import into a Database table. As usually the files aren't perfect. For example they have a different number of columns and some weird columns. The second problem is, that I have to add 3 parts of the filename into 3 rows in the... (6 Replies)
Discussion started by: elRonaldo
6 Replies

2. Shell Programming and Scripting

Save output of updated csv file as csv file itself, part 2

Hi, I have another problem. I want to sort another csv file by the first field. result.csv SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw /home/intannf/foto5/2015_0313_090651_219.JPG,0.,-7.77223,110.37310,30.75,996.46,148.75,180.94,182.00,63.92 ... (2 Replies)
Discussion started by: refrain
2 Replies

3. Shell Programming and Scripting

Save output of updated csv file as csv file itself

Hi, all I want to sort a csv file based on timestamp from oldest to newest and save the output as csv file itself. Here is an example of my csv file. test.csv SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0739.JPG,2015:02:17 11:32:21 /home/intannf/foto/IMG_0749.JPG,2015:02:17 11:37:28... (10 Replies)
Discussion started by: refrain
10 Replies

4. 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

5. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

6. Shell Programming and Scripting

Perl script to modify csv file

Hi Friends, I want to convert a csv file into a ordinary .txt file. I am able to convert but I want the output to look as shown below in the .txt file table findhost= { {"xyz","abc"}, {"rxz","mmz"}, {"vrr","nnz"}, } default={"NONE"} My current perl script #!/usr/bin/env perl... (12 Replies)
Discussion started by: dbashyam
12 Replies

7. Shell Programming and Scripting

Calling Pl/sql function in shell script to modify csv

I need to 1.Open a csv 2.Process the csv i.e. Modify 2 column in the csv. To modify the column the value needs to be passed to a pl/sql function and the return value should be updated For eg: If column 2 E,then E will be passed in database function which will return Employee. 3. Write a... (5 Replies)
Discussion started by: Chinky23
5 Replies

8. Shell Programming and Scripting

Read data from .csv file through shell script & modify

I need to read data from a file called "test.csv" through shell script where the file contains values like name,price,descriptor etc. There are rows where descriptor (& in some rows name) are written as string & other characters like "car_+" OR "bike*" etc where it should contains strings like... (3 Replies)
Discussion started by: raj100
3 Replies

9. Shell Programming and Scripting

Need to modify csv-file with bash script

Hi Guys, I need to write a script, that exports the "moz_places" table of the "places.sqlite"-file (firefox browser history) into a csv-file. That part works. After the export, my csv looks like this: ... 4429;http://www.sqlite.org/sqlite.html;"Command Line Shell For... (11 Replies)
Discussion started by: Sebi0815
11 Replies
Login or Register to Ask a Question