Displaying specific lines from a CSV file

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Displaying specific lines from a CSV file
# 8  
Old 02-03-2010
Lightbulb

If your system supports the -A option for grep, you could try something like this (I'm assuming that the records are stored in a file called file.txt, which does not contain the actual header "name,telno..", only the records):

Code:
#!/bin/bash

#copy the content of file.txt to a temporary file
cat file.txt > tmp.txt

#get today's month and day
today=`date +%m'/'%d`

#insert a dummy row, with todays date and time in the temporary file
dummy="test 0,123456789,1800/$today"

echo "$dummy" >> tmp.txt

#sort the file by month and day and output to another temp file
sort -t'/' -k2,3 tmp.txt > tmp2.txt

#no of birthdays today, apart from the dummy row
matches_today=`grep "$today" tmp2.txt | grep -v "$dummy" | wc -l`


#if there are no matches for today, check to see if the dummy is the last record; 
#if it is the next birthday is top of the list, otherwise it's the next row; otherwise, 
#if there are matches for today echo them

if test $matches_today -eq 0
        then
                if test "`tail -1 tmp2.txt`" == "$dummy"
                        then
                                echo "`head -1 tmp2.txt`"
                        else
                                echo "`grep -A1 "$dummy" tmp2.txt | grep -v "$dummy"`"
                fi
        else
                echo "`grep "$today" tmp2.txt | grep -v "$dummy"`"

fi

This is not optimally organized obviously, but I'm writting in a hurry Smilie
# 9  
Old 02-04-2010
Your a star!

Seriously, I was trying to make it too complicated. All it took was 20-odd lines. I modified it to suit my situation and it works an absolute treat.

Cheers,
Adzi
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert a horizontal lines to vertical lines in a csv file

Hi.. I need some help in converting the below horizontal lines to vertical lines format. can anyone help me on this. input file Hour,1,2,3,4,5 90RT,106,111,111,112,111 output file Hour,90RT 1,106 2,111 3,111 4,112 5,111 (3 Replies)
Discussion started by: Raghuram717
3 Replies

2. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

3. Shell Programming and Scripting

insert data into specific lines of a CSV

So I work in a 1 to 1 laptop deployment and sometimes we need to mass order parts. The vendor will send us a text file and we have to manually input serial numbers. Well I have a full blown web based inventory system which I can pull serial number reports from. I then have to input the part... (4 Replies)
Discussion started by: tlarkin
4 Replies

4. Shell Programming and Scripting

Displaying lines of a file which have the highest number?

Hello Wondering if anybody may be able to advise on how I can filter the contents of the following file: <object_name>-<version> <Instance> GM_GUI_code.fmb-4 1 GM_GUI_code.fmb-5 1 GM_GUI_code.fmx-4 ... (7 Replies)
Discussion started by: Glyn_Mo
7 Replies

5. Shell Programming and Scripting

Displaying lines of a file where the second field matches a pattern

Howdy. I know this is most likely possible using sed or awk or grep, most likely a combination of them together, but how would one go about running a grep like command on a file where you only try to match your pattern to the second field in a line, space delimited? Example: You are... (3 Replies)
Discussion started by: LordJezoX
3 Replies

6. UNIX for Dummies Questions & Answers

Displaying specific columns in a file

Hi, I'm just wondering how you display a specific set of columns of a specified file in Unix. For example, if you had an AddressBook file that stores the Names, Phone numbers, and Addresses of people the user entered in the following format (the numbers are just to give an idea of what column... (1 Reply)
Discussion started by: logorob
1 Replies

7. Shell Programming and Scripting

Displaying number of lines from file

Hi, I am using below command to display the number of line, but its returning no of lines along with file name. But i want only no of line in the variable p. Please help me on this? p=`wc -l "text file"` echo "$p" (6 Replies)
Discussion started by: shivanete
6 Replies

8. UNIX for Dummies Questions & Answers

how to display specific lines of a specific file

are there any basic commands that can display lines 99 - 101 of the /etc/passwd file? I'm thinking use of head and tail, but I forget what numbers to use and where to put /etc/passwd in the command. (2 Replies)
Discussion started by: raidkridley
2 Replies

9. Shell Programming and Scripting

Displaying the Last Modification Time of a specific file

How can I get and display the last modification time of a file? in scripting or specifically using Batch file I want this info for me to determine whether an image has been edited or not by using the last modification time and compare it to our stored date of modification. can somebody help... (5 Replies)
Discussion started by: jaque18
5 Replies

10. UNIX for Dummies Questions & Answers

Displaying specific lines in a file.

I'm trying to figure out how to display a certain line in a text file. I keep getting references to Tail and Head, and I know how these work, but i'm lost on how to find say the third out of the five lines and display only that. I thought maybe grep could help, but that doesn't seem likely. ... (3 Replies)
Discussion started by: MaestroRage
3 Replies
Login or Register to Ask a Question