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
# 1  
Old 10-06-2010
Question Parse file from 2nd line in shell script

Hi,

I need to parse input file from 2nd line.
Input file contents are,
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;CCC;09;DDD;601020304;CCCC;1;;;;;

For each line,
1] I need to check number of fields should be 12 and check 7th field value.
Code:
fields=$(IFS=\;; set -- $line; echo $#)
 if [ $fields -ne 12 ]; then
    echo "Incorrect number of fields"
else
   Status=`echo $line | cut -d ";" -f 7`

2] If Status -eq 1, I need to get 2nd and 4th field values of line and need to put in variable array.
Code:
 
if [ $Status -eq 1 ]; then
old=`echo $line | cut -d ";" -f 2`
new=`echo $line | cut -d ";" -f 4`
fi

Please tell me how can I do it from 2nd line of file and store values in array variable for a file?
Thanks in advance.
# 2  
Old 10-06-2010
Something like this:

Code:
 
awk -F";" 'NR>1 && $7 == 1 { old[NR]=$2 ; new[NR]=$4 }' input_file

You can print/use the old , using END in the awk if you wish to.
# 3  
Old 10-06-2010
Question

Thanks,
But i need code in shell script (ksh).
also let me know the printing of all old and new values using for loop/ if loop.
Thanks in advance.

---------- Post updated at 05:34 AM ---------- Previous update was at 04:58 AM ----------

How can I write loop from 2nd line of input file till end?
# 4  
Old 10-06-2010
simple way.
Code:
 
 
$ j=0
$ while read line
> do
> if [ $j -eq 0 ]; then
> echo "hi this is the first line"
> j=1
> else
> echo $line
> fi
> done < input_File
hi this is the first line
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;;;;;

# 5  
Old 10-06-2010
I am not satisfied with this solution.
Could you please let me know any other solution?
# 6  
Old 10-06-2010
kindly find below code

Code:
while read line
do
fields=$(IFS=\;; set -- $line ; echo $#)
a=( $(IFS=\; ; set -- $line ; echo $@ ) )
Status=${a[6]}
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 < infile.txt

# 7  
Old 10-06-2010
Try to work from here
bash code:
  1. c=0
  2. while read line
  3. do
  4. if &#91; $c -eq 0 ];then
  5.  let c=1
  6. else
  7.  IFS=\;; set -- $line
  8.  if &#91; $# -ne 12 ]; then
  9.         echo "Incorrect number of fields"
  10.  else
  11.         if   &#91; $7 -eq 1 ];then
  12.                 echo Old is $2 and New is $4 # [COLOR="Red"]do whatever you want here[/COLOR]
  13.         fi
  14.   fi
  15. fi
  16. done < file
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