Save output of updated csv file as csv file itself


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Save output of updated csv file as csv file itself
# 8  
Old 03-31-2015
That's because characters sort below digits. And it would surprise me if the outputs of the two commands were different.
To obtain the desired result with the header on top of the data, try
Code:
head -1 test.csv; tail -n+2 test.csv | sort -t, -k2

This User Gave Thanks to RudiC For This Post:
# 9  
Old 04-09-2015
Quote:
Originally Posted by RudiC
That's because characters sort below digits. And it would surprise me if the outputs of the two commands were different.
To obtain the desired result with the header on top of the data, try
Code:
head -1 test.csv; tail -n+2 test.csv | sort -t, -k2

Hi, RudiC

Thanks for your help. Would you mind to explain those script to me? I don't understand yet about it. Thanks in advance.

Regards,
Intan

---------- Post updated at 12:36 AM ---------- Previous update was at 12:25 AM ----------

Quote:
Originally Posted by Don Cragun
Code:
sort -k2.1,2.4n -k2.6,2 -o test.csv -t, test.csv

Hi, Don Cragun

Thanks for your help. Would you mind to explain those script to me? I am getting confused about this part:
Code:
-k2.1,2.4n -k2.6,2

I think k2 means the second row of the file. Is that correct? But i don't understand the other yet. Thanks in advance.

Regards,
Intan
# 10  
Old 04-09-2015
Have you tried looking at the man page for the sort, head, and tail utilities?

The command:
Code:
sort -k2.1,2.4n -k2.6,2 -o test.csv -t, test.csv

sorts using a primary sort key that starts with the 1st character in the 2nd field and ends with the 4th character in the second field treating that text as a numeric value (-k2.1,2.4n), using a secondary sort key that starts with the 6th character in the second field and ends with the last character of the second field as an alphanumeric value ( -k2.6,2 ), putting the sorted output into the file test.csv (-o test.csv), using comma as the field delimiter (-t,), and reading input to be sorted from the file test.csv (test.csv).

Using a numeric sort on data that starts with a non-numeric character (in this case, the 1st 4 characters of DateTimeOrigina) is treated as zero (which sorts before the year in the other lines in your file). This makes the heading sort to the beginning of your file instead of the end of your file without having to split out the 1st line of your file (as RudiC did with head -1 test.csv) and sorting the remainder of your input file (head -n+2 test.cv | sort...).
# 11  
Old 04-09-2015
head -1 prints test's first line - the header.
tail -n+2 prints all lines starting with test's second line, then sort -t, -k2 does the rest. You may want to specify the sort keys in more detail, though.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

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

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

4. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

5. Shell Programming and Scripting

FILE_ID extraction from file name and save it in CSV file after looping through each folders

FILE_ID extraction from file name and save it in CSV file after looping through each folders My files are located in UNIX Server, i want to extract file_id and file_name from each file .and save it in a CSV file. How do I do that? I have folders in unix environment, directory structure is... (15 Replies)
Discussion started by: princetd001
15 Replies

6. Shell Programming and Scripting

need to save the space when converting to CSV file

Hi, I have a text file with the following format. Some of the fields are blank. 1234 3456 23 45464 327837283232 343434 5654353 34 34343 3434345 434242 .... .... .... I need to convert this file to a CSV file, like 1234, ,23, ... (3 Replies)
Discussion started by: wintersnow2011
3 Replies

7. UNIX for Dummies Questions & Answers

CSV file:Find duplicates, save original and duplicate records in a new file

Hi Unix gurus, Maybe it is too much to ask for but please take a moment and help me out. A very humble request to you gurus. I'm new to Unix and I have started learning Unix. I have this project which is way to advanced for me. File format: CSV file File has four columns with no header... (8 Replies)
Discussion started by: arvindosu
8 Replies

8. Shell Programming and Scripting

select data from oracle table and save the output as csv file

Hi I need to execute a select statement in a solaris environment with oracle database. The select statement returns number of rows of data. I need the data to be inserted into a CSV file with proper format. For that we normally use "You have to select all your columns as one big string,... (2 Replies)
Discussion started by: rdhanek
2 Replies

9. Shell Programming and Scripting

Data fetched from text file and save in a csv file

Hi i have wriiten a script which fetches the data from text file, and saves in the output in a text file itself, but i want that the output should save in different columns. I have the output like: For Channel:response_time__24.txt 1547 data points 0.339 0.299 0.448 0.581 7.380 ... (1 Reply)
Discussion started by: rohitkalia
1 Replies
Login or Register to Ask a Question