LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-21-2016, 06:22 AM   #1
nexuslinux
LQ Newbie
 
Registered: Apr 2016
Posts: 3

Rep: Reputation: Disabled
How to print lines in csv file if 1 csv column field = "text". There are 10 column (;) in csv file


How to print lines in csv file if 1 csv column field = "xxxxx".

xxxxx = alphabet
xxxxx = numeric

There are 10 column in csv file:
For instant : Test result;unix timestamp;1234567890;2233;111;A;B;C;D;E

If csv column 5= 111, then print the line in source file
 
Old 04-21-2016, 06:58 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
And have you made any effort to solve this yourself ?.
 
Old 04-21-2016, 06:59 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Welcome to LQ!
How about some actual data and not some arbitrary 'examples"?
 
1 members found this post helpful.
Old 04-21-2016, 07:20 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
i would look into the awk command.
 
1 members found this post helpful.
Old 04-21-2016, 07:26 AM   #5
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by Habitual View Post
Welcome to LQ!
How about some actual data and not some arbitrary 'examples"?
^Ditto. But here are a couple of hints:
• awk
• field separator

Example:
Code:
echo "Test result;unix timestamp;1234567890;2233;111;A;B;C;D;E
> Test result;unix timestamp;1234567890;2233;112;A;B;C;D;E
> Test result;unix timestamp;1234567890;2233;113;A;B;C;D;E
> Test result;unix timestamp;1234567890;2233;114;A;B;C;D;E" | awk <hidden>
Test result;unix timestamp;1234567890;2233;111;A;B;C;D;E
Best regards,
HMW

Last edited by HMW; 04-21-2016 at 07:30 AM. Reason: spelling
 
Old 04-21-2016, 07:50 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Another vote for awk. If you are thinking in columns, you should often be thinking awk
 
Old 04-22-2016, 08:04 PM   #7
nexuslinux
LQ Newbie
 
Registered: Apr 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
This posting has been cancelled due to click post button unintentionally

Last edited by nexuslinux; 04-22-2016 at 08:16 PM. Reason: click post button unintention
 
Old 04-22-2016, 08:08 PM   #8
nexuslinux
LQ Newbie
 
Registered: Apr 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Another vote for awk. If you are thinking in columns, you should often be thinking awk


Anyway, I just applied the grep CLI and manage to print the lines with specific column [2233].
Example source file:
Test result;unix timestamp;1234567890;2233;111;A;B;C;D;E
Test result;unix timestamp;1234567890;2235;111;A;B;C;D;E

grep 2233 [source filename] > output file
>Test result;unix timestamp;1234567890;2233;111;A;B;C;D;E
 
Old 04-22-2016, 09:01 PM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
No, you printed lines that had that number anywhere in the entire record. You could do it with grep, but you'll need some regex to eliminate the unwanted columns.
 
Old 04-22-2016, 11:35 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Using a mod of your example shows what syg00 is saying:
Code:
$ cat source.file
Test result;unix timestamp;1234567890;2233;111;A;B;C;D;E
Test result;unix timestamp;1223367890;2235;111;A;B;C;D;E
$ grep 2233 source.file
Test result;unix timestamp;1234567890;2233;111;A;B;C;D;E
Test result;unix timestamp;1223367890;2235;111;A;B;C;D;E
As you can see, the highlighted portions match but are not in the same columns.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] A challenging script - Replace field of CSV file based on another CSV file arbex5 Programming 11 06-12-2013 06:56 AM
need shell command to wrap text &adjust column width (csv file ) Sankaran29 Linux - Newbie 6 12-17-2012 09:33 AM
[SOLVED] How to script csv editing? Remove rows from csv file that do not contain certain text ingram87 Linux - Software 9 08-03-2012 12:45 PM
How to redirect the output to a different column in .csv file Sayan Acharjee Linux - General 2 04-25-2011 11:43 AM
Rearrange the column in CSV file.. govi1234 Linux - Newbie 9 08-27-2010 07:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration