Visit Our UNIX and Linux User Community

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
# 1  
Old 06-18-2008
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 line of the file, should match with the last field of the first and last line.

That is, the last field of first and last line wil have a number, that should match with {number of line in the file} -2.

Pls help me out with this.

Example file: QDB_2008.txt


# 2  
Old 06-18-2008
awk -F, '
NR==1 { expect=$NF; fields=NF; last=0 }
NF != fields { last=NR;
  if (NR != 2) print NR ": wrong number of fields: " $0;
  if ($NF != expect) print NR ": last field value not the same as on first line"
  if ($NF != NR-2) print NR ": last field not equal to line count minus two"
  if ($NF != expect) print NR ": line count from first line not identical"
last > 0 && NR > last { print NR ": wrong number of fields: " $0 }' QDB_2008.txt

Slightly unwieldy, but should hopefully at least get you started.
# 3  
Old 06-18-2008
Hammer & Screwdriver Not as elegant, but a different way of approaching the matter

> cat chk_valid 
#! /bin/bash
# script to check on file conditions


line_1=$(head -1 $ifile)
line_lst=$(tail -1 $ifile)
line_cnt=$(cat $ifile | wc -l)

line_1_val=$(echo $line_1 | cut -d"," -f5)
line_lst_val=$(echo $line_lst | cut -d"," -f2 | cut -d"#" -f1)

if [ "$line_1_val" -ne "$line_lst_val" ]
   echo "Error - header & footer line counts differ"

if [ "$detl_cnt" -ne "$line_1_val" ]
   echo "Error - # detail lines does not match expected counts"

# 4  
Old 06-19-2008
Hi ,

The above code works fine.
I want to run the script after adding newline character at the end of file.

Pls let me know how to append a newline character at the end of the file, if it does not exists.

# 5  
Old 06-19-2008
awk 1 file

# 6  
Old 06-19-2008
$ echo "" >> file

# 7  
Old 06-19-2008
Originally Posted by ripat
$ echo "" >> file

This always append a newline at the end of a file while the OP wants to append a newline character at the end of the file, only if it does not exists.


Previous Thread | Next Thread
Test Your Knowledge in Computers #40
Difficulty: Easy
HTML stands for 'Hyper Technology Transfer Protocol'.
True or False?

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

Featured Tech Videos