LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2013, 03:59 AM   #1
konkura
LQ Newbie
 
Registered: Apr 2011
Posts: 22

Rep: Reputation: 1
help with script that search through csv file


Hi,

I have to create script which will search through log file in csv format
which have lines like this:

"","100","0992342235676","office","100","main location","23"
"","101","666666666","office","101","main location","24"

so I need to search third column which have numbers that begin with 56.
So far I have this script:

#!/bin/sh

INPUT=file.csv
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read col3
do
echo $col3
done < $INPUT
IFS=$OLDIFS

but I dont know how to include search for lines that have third
columns that begin with '56'

thanx!
 
Old 12-10-2013, 04:26 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Have a look at this:
Code:
#!/bin/bash

INPUT=file.csv

OLDIFS=$IFS
IFS=,

[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }

while read col1 col2 col3 col4 col5 col6 col7
do
  [[ "$col3" =~ ^.56.*$ ]] && echo "$col1 $col2 $col3 $col4 $col5 $col6 $col7"
done < $INPUT

# another way of doing this using bash:
while read LINE
do
  csvArray=( ${LINE} )
  [[ "${csvArray[2]}" =~ ^.56.*$ ]] && echo "$LINE"
done < $INPUT

IFS=$OLDIFS

# and another using awk:
awk -F, '$3 ~ /"56/' $INPUT
 
1 members found this post helpful.
Old 12-10-2013, 09:31 AM   #3
konkura
LQ Newbie
 
Registered: Apr 2011
Posts: 22

Original Poster
Rep: Reputation: 1
Druuna- what to say thx!!! I learn somethin new :-)
I still got one more question- is it possible, if I
have log file with time stamps like this one here,
that script start to search (it will be run periodically through
cron job) from time stamp when last cron job finished.
Let say script is started, and start search from place in
log file that have 16:00 onward? Than again script is started at
18:00 and start search in log file from place that have time stamp
17:00 onward?
thx again!
 
Old 12-10-2013, 10:21 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by konkura View Post
Druuna- what to say thx!!! I learn somethin new :-)
You're welcome.

Quote:
I still got one more question- is it possible, if I
have log file with time stamps like this one here,
that script start to search (it will be run periodically through
cron job) from time stamp when last cron job finished.
Let say script is started, and start search from place in
log file that have 16:00 onward? Than again script is started at
18:00 and start search in log file from place that have time stamp
17:00 onward?
First of all: The file shown in post #1 isn't a log file, but a csv file.

Yes, you can check (log) files based on a time period. Here's a simple example using awk:
Code:
$ awk '$0 >= "Dec 10 08:00:00" && $0 <= "Dec 10 09:00:00" { print $0 }' logfile
The above will show lines between Dec 10 08:00:00 and Dec 10 09:00:00 (included).

Your date string must have the same layout as the one used in the log file. The date command can generate just about any form you like/want.
 
Old 12-10-2013, 01:54 PM   #5
konkura
LQ Newbie
 
Registered: Apr 2011
Posts: 22

Original Poster
Rep: Reputation: 1
Again, respect! Thx! :-)
 
  


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 script help with a CSV file. lothario Linux - Software 2 08-31-2012 11:40 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
Shell Script for CSV file comparision aravind_balan Programming 1 02-17-2009 03:33 AM
Shell script to read from csv file hendemeg Programming 1 05-11-2004 08:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:37 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
Open Source Consulting | Domain Registration