Parse file from 2nd line in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse file from 2nd line in shell script
# 8  
Old 10-06-2010
Thanks Ahmad, but always I am getting below message,
Code:
"Incorrect number of fields"

Let me know, what can i do?
# 9  
Old 10-06-2010
Strange, and why?

my output was as below:-


Code:
I/P:-

ABC123;20100913115432;2000000;NO;
04;AAA;09;DDD;601020304;AAAA;1;OPTA1;OPTA2;;;
04;BBB;09;BBB;601020304;BBBB;0;OPTB1;OPTB2;OPTB3;OPTB4;OPTB5;
04;CCC;09;DDD;601020304;CCCC;1;;;;;

Code:
O/P:-

Incorrect number of fields
AAA DDD
Incorrect number of fields
CCC DDD

# 10  
Old 10-06-2010
Quote:
Originally Posted by ahmad.diab

my output was as below:-


Code:
I/P:-

ABC123;20100913115432;2000000;NO;
04;AAA;09;DDD;601020304;AAAA;1;OPTA1;OPTA2;;;
04;BBB;09;BBB;601020304;BBBB;0;OPTB1;OPTB2;OPTB3;OPTB4;OPTB5;
04;CCC;09;DDD;601020304;CCCC;1;;;;;

My output base on this data sample is
Code:
Incorrect number of fields
Status is Null
Incorrect number of fields

If I take the following data sample
Code:
ABC123;20100913115432;2000000;NO;
04;AAA;09;DDD;601020304;AAAA;1;OPTA1;OPTA2;;;
04;BBB;09;BBB;601020304;BBBB;0;OPTB1;OPTB2;OPTB3;OPTB4;OPTB5;
04;BCB;09;BCB;601020304;BBBB;1;OPTB1;OPTB2;OPTB3;OPTB4;OPTB5;
04;CCC;09;DDD;601020304;CCCC;1;;;;;

the output is
Code:
Incorrect number of fields
Status is Null
Old is BCB and New is BCB
Incorrect number of fields

# 11  
Old 10-06-2010
No , the line you refer in below is 13 field long not 12, you can test it using:

Code:
nawk -F";" '{print NF}' infile.txt

there is a null filed in the end of the line after the last ";" .

BR
# 12  
Old 10-07-2010
My code is,
Code:
while read line
	do
	fields=$(IFS=\;; set -- $line ; echo $#)
	echo "Fields are $fields"
	a=$(IFS=\; ; set -- $line ; echo $@)
	Status=${a[6]}
	echo "Status is $Status"
	if [ $(($fields+1)) -ne 12 ]
	then
	echo "Incorrect number of fields"
	elif [ "$Status" -eq 1 ]
	then
	echo -n "${a[1]} "
	echo "${a[3]}"
	fi
	done < $file

my output is,
Code:
Fields are 4
Status is
Incorrect number of fields
Fields are 11
Status is
Fields are 12
Status is
Incorrect number of fields
Fields are 11
Status is

I am not able to print the values of array and status variable?
Could you please help me out. Thanks in advance.
# 13  
Old 10-07-2010
here is your problem :-

Code:
a=(  $(IFS=\; set -- $line ; echo $@)  )

in bash to initialize an array you need () :-

example:-

a=( 1 2 3 4 5 )

BR


SmilieSmilieSmilie
# 14  
Old 10-07-2010
tail +2 $yourfile .... before parsing : so this will skip the first line

i you want to handle it as a stream, go for sed or awk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SH script to parse string and return multi-line file

Hello all, I have been asked to exercise my shell scripting and it has been 10 plus years since I used to do it so I can not remember hardly anything and ask for your help. What I need to do is copy a line out of a file that can be 10 to 100 characters long, I then need to parse this line into... (3 Replies)
Discussion started by: Alivadoro
3 Replies

2. UNIX for Dummies Questions & Answers

Help to parse csv file with shell script

Hello ! I am very aware that this is not the first time this question is asked here, because I have already read a lot of previous answers, but none of them worked, so... As said in the title, I want to read a csv file with a bash script. Here is a sample of the file: ... (4 Replies)
Discussion started by: Grhyll
4 Replies

3. UNIX for Advanced & Expert Users

Shell script to parse apache httpd line

Hello, I have serveral SUN Web servers that need to write a KSH to figure out where Apache is installed and, tar up the directory (for backup purposes) once a month. The trick is that Apache is not installed in same location. So when I do "ps -ef| grep httpd" it shows on some boxes from... (2 Replies)
Discussion started by: afadaghi
2 Replies

4. Shell Programming and Scripting

Bash Shell Script to parse file

Raw Results: results|192.168.2|192.168.2.1|general/udp|10287|Security Note|For your information, here is the traceroute from 192.168.2.24 to 192.168.2.1 : \n192.168.2.24\n192.168.2.1\n\n results|192.168.2|192.168.2.1|ssh (22/tcp)|22964|Security Note|An SSH server is running on this port.\n... (2 Replies)
Discussion started by: jroberson
2 Replies

5. Shell Programming and Scripting

Parse XML file in shell script

Hi Everybody, I have an XML file containing some data and i want to extract it, but the specific issue in my file is that the data is repeated some times like the following example : <section1> <subsection1> X=... Y=... Z=... <\subsection1> <subsection2> X=... Y=... Z=...... (2 Replies)
Discussion started by: yassine
2 Replies

6. UNIX for Advanced & Expert Users

shell script to parse html file

hi all, i have a html file something similar to this. <tr class="evenrow"> <td class="data">added</td><td class="data">xyz@abc.com</td> <td class="data">filename.sql</td><td class="modifications-data">08/25/2009 07:58:40</td><td class="data">Added TK prof script</td> </tr> <tr... (1 Reply)
Discussion started by: sais
1 Replies

7. Shell Programming and Scripting

Shell script to parse a line and insert a word

Hi All, I have a file like this, data1,data2,,,data5,data6. i want to write a shell script to replace data3 with "/example/string". which means my data file should look like this . data1,data2,example/string],,data5,data6. Could you guys help me to get a sed command or any other command... (8 Replies)
Discussion started by: girish.raos
8 Replies

8. Shell Programming and Scripting

Help!!! Shell script to parse data file.

I am faced with a :confused: tricky problem to parse a data file ( May not be a tricky problem to the scripting guru's ). Here is what I am faced with. I have a file with multiple rows of data and the rows are not of fixed length. "|" is used as a delimiters for individual columns and each row... (3 Replies)
Discussion started by: yajaykumar
3 Replies

9. Shell Programming and Scripting

Parse a string in XML file using shell script

Hi! I'm just new here and don't know much about shell scripting. I just want to ask for help in creating a shell script that will parse a string or value of the status in the xml file. Please sample xml file below. Can you please help me create a simple script to get the value of status? Also it... (46 Replies)
Discussion started by: ayhanne
46 Replies

10. Shell Programming and Scripting

How to parse config variables from external file to shell script

How do i use a config.txt to recursively pass a set of variables to a shell script eg my config.txt looks like this : path=c://dataset/set1 v1= a.bin v2= b.bin path=c://dataset/set2 v1= xy.bin v2= abc.bin .................. and so on . and my testscript : (2 Replies)
Discussion started by: pradsh
2 Replies
Login or Register to Ask a Question