How to compare two lines in a csv file using shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare two lines in a csv file using shell script?
# 1  
Old 07-23-2013
How to compare two lines in a csv file using shell script?

I have a file lets say input.csv having two columns like-
Code:
Name,Mobile No
A,111
B,222
A,333
A,123
B,213

I would like to find result in a new file lets say output.csv as-
Code:
Name,Mobile No
A,111
B,222

means short the file on the basis of first column and first value corresponding to the unique one of first column from second one.

Any help should be highly appreciated..... Plz help me I m a new in shell

Last edited by Franklin52; 07-23-2013 at 10:33 AM.. Reason: Please use code tags
# 2  
Old 07-23-2013
Code:
 
 sort -t, -k1,1 -u input.csv

This User Gave Thanks to vidyadhar85 For This Post:
# 3  
Old 07-23-2013
Code:
cat input.csv | awk -F"|" '{ if (NR == 1 || NR == 2) print $0' >> output_file_name


Assuming you want to capture first two lines

Last edited by Franklin52; 07-23-2013 at 10:34 AM.. Reason: Please use code tags
This User Gave Thanks to Himanshu Sharma For This Post:
# 4  
Old 07-23-2013
Thanks for quick response but i don't want only first two lines. I like to find unique values from column one and their corresponding values from column 2 from a input csv file.
# 5  
Old 07-23-2013
Quote:
Originally Posted by Ashish Singhal
Thanks for quick response but i don't want only first two lines. I like to find unique values from column one and their corresponding values from column 2 from a input csv file.
Have you tried the sort command which I have provided above?
This User Gave Thanks to vidyadhar85 For This Post:
# 6  
Old 07-23-2013
Quote:
Originally Posted by Himanshu Sharma
cat input.csv | awk -F"|" '{ if (NR == 1 || NR == 2) print $0' >> output_file_name

Assuming you want to capture first two lines
Smilie

Useless use of cat and awk instead of head.
This User Gave Thanks to ripat For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 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

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

3. Shell Programming and Scripting

Help with Shell Scrip in Masking particular columns in .csv file or .txt file using shell script

Hello Unix Shell Script Experts, I have a script that would mask the columns in .csv file or .txt file. First the script will untar the .zip files from Archive folder and processes into work folder and finally pushes the masked .csv files into Feed folder. Two parameters are passed ... (5 Replies)
Discussion started by: Mahesh G
5 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

Script to compare lines in a file

Need help to create the script that does the following : - 1. Compare the current line "column B and C" with next line "column B and C" 2. If they are the same, print output to a file Input file 2014-08-25 04:45:56.673|T1|JO|Begin|10 2014-08-25 04:55:56.673|T1|JO|Begin|11 2014-08-25... (8 Replies)
Discussion started by: chailee
8 Replies

6. Shell Programming and Scripting

Compare 2 csv files in ksh and o/p the difference in a new csv file

(say) I have 2 csv files - file1.csv & file2.csv as mentioned below: file1.csv ID,version,cost 1000,1,30 2000,2,40 3000,3,50 4000,4,60 file2.csv ID,version,cost 1000,1,30 2000,2,45 3000,4,55 6000,5,70 The... (7 Replies)
Discussion started by: Naresh101
7 Replies

7. Shell Programming and Scripting

Need perl or shell script to sort vertical lines to horizontal line in csv format

Need perl or shell script to sort vertical lines to horizontal line in csv format My file like below ------------------------- ================================================================================ PATH PINKY1000#I1-1-ZENTA1000-2#I7-1-ASON-SBR-UP-943113845 ... (4 Replies)
Discussion started by: sreedhargouda.h
4 Replies

8. Shell Programming and Scripting

Read Two lines in a CSV File and Compare

Hi , I have a CSV file ( file.csv) with some data as below: A,1,abc,x,y,z,,xyz,20100101,99991231 A,1,abc,x,y,z,234,xyz,20100101,99991231 I have to delete the duplicate line based on unique identifiers which are values in the fields- 2,3,4,8.These coulmns in both the rows have same... (6 Replies)
Discussion started by: Sheel
6 Replies

9. Shell Programming and Scripting

Need to compare two csv files values and write into another csv file

Hi all, Am new to scripting. So i just need your ideas to help me out. Here goes my requirement. I have two csv files 1.csv 2.csv abc,1.24 abc,1 def,2.13 def,1 I need to compare the first column of 1.csv with 2.csv and if matches then need to compare... (2 Replies)
Discussion started by: chinnahyd
2 Replies

10. Shell Programming and Scripting

how to compare two lines using shell script?

how to compare two lines using shell script? (1 Reply)
Discussion started by: suman_dba1
1 Replies
Login or Register to Ask a Question