LinuxQuestions.org
Review your favorite Linux distribution.
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, 07: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, 07:58 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
And have you made any effort to solve this yourself ?.
 
Old 04-21-2016, 07:59 AM   #3
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,158
Blog Entries: 10

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
Welcome to LQ!
How about some actual data and not some arbitrary 'examples"?
 
1 members found this post helpful.
Old 04-21-2016, 08:20 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,026

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
i would look into the awk command.
 
1 members found this post helpful.
Old 04-21-2016, 08:26 AM   #5
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Xubuntu, Manjaro
Posts: 745
Blog Entries: 3

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347
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 08:30 AM. Reason: spelling
 
Old 04-21-2016, 08:50 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Another vote for awk. If you are thinking in columns, you should often be thinking awk
 
Old 04-22-2016, 09: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 09:16 PM. Reason: click post button unintention
 
Old 04-22-2016, 09: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, 10:01 PM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
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-23-2016, 12:35 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 07:56 AM
need shell command to wrap text &adjust column width (csv file ) Sankaran29 Linux - Newbie 6 12-17-2012 10: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 01:45 PM
How to redirect the output to a different column in .csv file Sayan Acharjee Linux - General 2 04-25-2011 12:43 PM
Rearrange the column in CSV file.. govi1234 Linux - Newbie 9 08-27-2010 08:15 PM


All times are GMT -5. The time now is 10:40 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration