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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix shell script to parse the contents of comma-separated file
# 8  
Old 06-19-2008
Hi,

The above command does not seek to work.

I tried echo >> file
It is appending the newline at the end of file.

But can you help me in putting with a condition to check whether
if {newline does not exists at end of file}
then
echo >> file

Pls help me out with this.

Regards,
Krishna
# 9  
Old 06-19-2008
Have you tried the awk solution?
# 10  
Old 06-20-2008
Hi,

I got it working like this :
if [ -n "`tail -1c $file`" ]
then
echo >> $file
fi

This wil append new-line at the EOF if it does not exists.

I have some more file-level validation to take care. Need your valuable inputs on this.

My (comma-separated) file is something like below:

1.0,20080317081500,00001,00006,6
00001,20080317,00001,60213000071,2105,I
00002,20080317,00002,60213000071,0,D
00003,20080317,00003,60213000072,2104,I
00004,20080317,00004,60213000073,2103,I
00005,20080317,00005,60213000074,2102,I
00006,20080317,00006,60213000074,0,D
10.1.254.21

Ignoring first line and last line, I need to retrieve the last 3 fields of the other lines, for further validation. Copying these fields, to a separate file also is ok.

The new file can be:

60213000071,2105,I
60213000071,0,D
60213000072,2104,I
60213000073,2103,I
60213000074,2102,I
60213000074,0,D

Here i need to validate 2 points:
1. In each line, first field should have length equal to 11 and start with 60
2. In each line, last field should have either "D", "I" or "U" in it.


Pls let me know about this.

With Regards,
Krishna
# 11  
Old 06-20-2008
Hi All,

Need someone's help urgently on this.

Pls let me know.

With Regards
# 12  
Old 06-20-2008
It's not allowed to bump up questions, please read the rules.

The moderator team.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing Comma Separated values to UNIX variable from PLSQL

Hi All, I'm trying to pass the comma separated values (string) returned from Plsql Procedure to UNIX variable. getting the below log message cat: -: Bad file descriptor awk: cmd. line:1: fatal: error reading input file `-': Bad file descriptor The output coming from plsql procedure is... (3 Replies)
Discussion started by: Mahesh3089
3 Replies

2. 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

3. 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

4. Shell Programming and Scripting

Extract comma separated value in unix

Hello All Can anyone please guide me how to solve the issue In the below code I am getting concat of two value in result variable with comma separated result=`sqlplus -s / <<EOF set pages 0 feed off; set feedback off; spool abc.txt select... (4 Replies)
Discussion started by: Pratik4891
4 Replies

5. 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

6. Shell Programming and Scripting

Reading comma separated variable into other variables in shell script

Hi, In shell script, I have a variable var = xyz, inn, day, night, calif ....n and I would like to read them in to var1 = xzy, var2 = inn, var3= day, var4 = night....var. probably in a loop. I would like to read the variables until end of the line. Comma is the delimiter and there's no comma at... (3 Replies)
Discussion started by: suryaemlinux
3 Replies

7. Shell Programming and Scripting

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... (11 Replies)
Discussion started by: NoMadBanker
11 Replies

8. Shell Programming and Scripting

Removing the entire file contents using unix shell script.

I need to remove the entire file contents in file using the shell script. Actually the grap -v command will create one more file and it occupy the space also. I need to remove the entire file contents without creating new file using the shell scripting. Please help me. (5 Replies)
Discussion started by: praka
5 Replies

9. 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

10. UNIX Desktop Questions & Answers

Unix Comma Separated to Excel Column

I would like to copy 2 parts of a csv file from Unix to an XL sheet. However to save time I do not want to format the column ever time I cut and paste into XL(Text2Column). I've used awk -F, '{Print $1, $2....}'. Is there a script or code that can automatically format the csv for XL columns? ... (3 Replies)
Discussion started by: ravzter
3 Replies
Login or Register to Ask a Question