The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove whitespaces between comma separated fields from file nitinbjoshi UNIX for Dummies Questions & Answers 2 06-14-2008 08:14 AM
Parse a string in XML file using shell script ayhanne Shell Programming and Scripting 46 01-09-2008 12:33 PM
search for the contents in many file and print that file using shell script cdfd123 Shell Programming and Scripting 3 10-07-2007 10:17 PM
How to parse config variables from external file to shell script pradsh Shell Programming and Scripting 2 07-09-2007 02:21 PM
Splitting comma separated values into an array tmarikle Shell Programming and Scripting 3 06-24-2005 05:50 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-18-2008
KrishnaSaran KrishnaSaran is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 12
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

1.1,20070427151500,99567,99669,0009
00001,20070427,00567,6012345671,2081,I
00002,20070427,00568,6012345672,2054,I
00003,20070427,00569,6012345673,2063,I
00004,20070427,00570,6012345674,2081,D
00005,20070427,00571,6012345675,2054,D
00006,20070427,00572,6012345676,2063,D
00007,20070427,00573,6012345677,2081,U
00008,20070427,00574,6012345678,2054,U
00009,20070427,00575,6012345679,2063,U
101.1.0,0009#

Regards,
Krishna
  #2 (permalink)  
Old 06-18-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Code:
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 (permalink)  
Old 06-18-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Not as elegant, but a different way of approaching the matter

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

ifile="QDB_2008.txt"

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

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" ]
   then
   echo "Error - header & footer line counts differ"
fi

if [ "$detl_cnt" -ne "$line_1_val" ]
   then
   echo "Error - # detail lines does not match expected counts"
fi
  #4 (permalink)  
Old 06-19-2008
KrishnaSaran KrishnaSaran is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 12
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.

Regards,
Krishna
  #5 (permalink)  
Old 06-19-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Code:
awk 1 file
Regards
  #6 (permalink)  
Old 06-19-2008
ripat ripat is online now Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
...or
Code:
$ echo "" >> file
  #7 (permalink)  
Old 06-19-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Quote:
Originally Posted by ripat View Post
...or
Code:
$ 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.

Regards
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 01:39 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0