Help parse comma separated list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help parse comma separated list
# 1  
Old 10-26-2010
Help parse comma separated list

I have a list of files with the same name, but they have a different date stamp in the name. I can find the first file, but I need to find the second file. I am using this information to create a variable I use later. Here is a example of how I find the first file.
"ls -mr /HOME/Daily_????????.txt | cut -d "," -f1 | head -1"
files are Daily_20100101.txt, Daily_20100102.txt, Daily_20100103.txt, etc.
this list get converted to Daily_20100103.txt,Daily_20100102.txt,Daily_2010101.txt
The variable1 gets set to "/HOME/Daily_20100103.txt"

I can grab the most current day, but I also need the information for the prior day and I can not seem to grab the second file in the list.
I would like variable2 to be "/HOME/Daily_20100102.txt"
Thanks in advance for your assistance.
# 2  
Old 10-26-2010
Code:
i=0
ls -t /HOME/Daily_*.txt | while read a
do
var[$i]=$a                                      or ${a##*/} if you want to get rid off the PATH
echo "var[$i] = ${var[$i]}"
let i++
done


Last edited by ctsgnb; 10-26-2010 at 11:42 AM..
# 3  
Old 10-26-2010
You don't need the "-m" to ls. Use "ls -1" instead.

Code:
# Current
ls -1 /HOME/Daily_????????.txt | tail -1
# Previous
ls -1 /HOME/Daily_????????.txt | tail -2 | head -1

This User Gave Thanks to methyl For This Post:
# 4  
Old 10-26-2010
BTW, if these are true MS Comma Separated Value (CSV) files, they can have double quoted fields with comma, doubled double quotes for any double quote in a field and a carriage return as well as a line feed. (Excel always honors this, but not some old Access versions.)
Code:
haha,hoho,"Baked, AK","He said ""Get out of my head!"" and shook his head violently.",975^M

# 5  
Old 10-26-2010
"ls -1 /HOME/Daily_????????.txt | tail -2 | head -1" seems to work like a charm..

Thanks
NMB
# 6  
Old 10-26-2010
@DGPickett
This post has nothing to do with CSV files. The O/P had generated a comma-separated list of filenames using "ls -m" but this made processing of the list fiddly.
Btw. CSV pre-dates Microsoft.
# 7  
Old 10-26-2010
It seems to me that you want the two most recent files. Have you considered:-
Code:
ls -1rt|tail -2|while read var2 # -1rt is number one (for one file name per line) rt (for reverse time order)
do
   read var1
done

Of course, this relies on the files update timestamp being in the correct sequence. if you would prefer to work on the file name being the determining factor, then:-
Code:
ls -1 Daily_*|tail -2|while read var2
do
   read var1
done

....or if there are a large number of files, you might be better with a find, such as:-
Code:
find . -name "Daily_*.txt"|sort|while read var2
do
   read var1
done

The sort is required because the order of find will probably read in i-node sequence, which is not necessarilly the order you want.

Last edited by vgersh99; 10-26-2010 at 12:33 PM.. Reason: code tags, please!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove duplicates from comma separated list

Hi, I have following input file: niki niki niki1 niki niki2 niki,niki2 niki3 niki,niki3,niki niki4 niki4,blabla niki5 jkjkl niki6 niki60,niki6 I would like to delete lines with identical matches completely and remove the selfmatches in the other lines. ... (2 Replies)
Discussion started by: niki0211
2 Replies

2. UNIX for Beginners Questions & Answers

How to extract fields from a CSV i.e comma separated where some of the fields having comma as value?

can anyone help me!!!! How to I parse the CSV file file name : abc.csv (csv file) The above file containing data like abv,sfs,,hju,',',jkk wff,fst,,rgr,',',rgr ere,edf,erg,',',rgr,rgr I have a requirement like i have to extract different field and assign them into different... (4 Replies)
Discussion started by: J.Jena
4 Replies

3. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

4. UNIX for Dummies Questions & Answers

[solved] Comma separated values to space separated

Hi, I have a large number of files which are written as csv (comma-separated values). Does anyone know of simple sed/awk command do achieve this? Thanks! ---------- Post updated at 10:59 AM ---------- Previous update was at 10:54 AM ---------- Guess I asked this too soon. Found the... (0 Replies)
Discussion started by: lost.identity
0 Replies

5. Shell Programming and Scripting

Need Help - comma inside double quote in comma separated csv,

Hello there, I have a comma separated csv , and all the text field is wrapped by double quote. Issue is some text field contain comma as well inside double quote. so it is difficult to process. Input in the csv file is , 1,234,"abc,12,gh","GH234TY",34 I need output like below,... (8 Replies)
Discussion started by: Uttam Maji
8 Replies

6. Shell Programming and Scripting

Output of command in comma separated list

Hi; I have an output of a particular command say $command fstl:r-x ajay:r-x how can i get this in comma separated list, eg: fstl:r-x,ajay:r-x Thnks; (4 Replies)
Discussion started by: ajaypadvi
4 Replies

7. Shell Programming and Scripting

Perl script to parse output and print it comma separated

I need to arrange output of SQL query into a comma separated format and I'm struggling with processing the output... The output is something like this: <Attribute1 name><x amount of white spaces><Atribute value> <Attribute2 name><x amount of white spaces><Atribute value> <Attribute3... (2 Replies)
Discussion started by: Juha
2 Replies

8. Shell Programming and Scripting

Parse apart strings of comma separated data with varying number of fields

I have a situation where I am reading a text file line-by-line. Those lines of data contain comma separated fields of data. However, each line can vary in the number of fields it can contain. What I need to do is parse apart each line and write each field of data found (left to right) into a file.... (7 Replies)
Discussion started by: 2reperry
7 Replies

9. Shell Programming and Scripting

Unix shell script to parse the contents of comma-separated file

Dear All, I have a comma-separated file. 1. The first line of the file(header) should have 4 commas(5 fields). 2. The last line of the file should have 1 comma(2 fields). Pls help me in checking this condition in a shell script. And the number of lines between the first line and last... (11 Replies)
Discussion started by: KrishnaSaran
11 Replies
Login or Register to Ask a Question