Column wise file parsing.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Column wise file parsing.
# 1  
Old 01-30-2009
Column wise file parsing.

Shell script for the below operation :

File "A" contains :

SEQ++1'
MOA+9:000,00:ABC'
RFF+AIK:000000007'
FII+PH+0170++AA'
NAD+PL+++XXXXXXXXXXX XXXXXXX XX++XXX XXXX XXXX X.X. XXXXXXXXX+++NL'
SEQ++2'
MOA+9:389,47:ABC'
RFF+AIK:02110300000008'
FII+PH+0PSTBNL2A:25:5+BB'
NAD+PL+++************************++********************+++NL'
SEQ++3'
MOA+9:3086,55SmilieDD'
RFF+AIK:00009'
FII+PH+0348932928++GG'
NAD+PL+++XXX++XXX XXXX XXXX X.X. XXXXXXXXX+++NL'

Need to fetch the details from the above file "A" and put them in file "B" in the below format :

payment file | Amount | Ref | Account No

1 9:000,00:ABC' 000000007' 0170++AA
2 9:389,47:ABC' 02110300000008' 0PSTBNL2A:25:5+BB'

The columns can be tab delimited.
I was able to do it in step wise using sed...have anyone done this in this way?

Thanks....
# 2  
Old 01-30-2009
There is probably a much much easier way to do this, but here's how I would do it given my limited knowledge.
Code:
perl -pe 's/SEQ/SEQSEQ/' < $yourfile |\
 perl -pe 's/\w\w\w//' | perl -pe 's/\n/::::/' |\
 perl -pe 's/SEQ/\n/g' | perl -pe 's/^\+\+//' |\
 perl -pe's/::::/\t/g' | perl -pe 's/\t\+/\t/' |\
 perl -pe 's/\+AIK\://' | perl -pe 's/\+PH\+//' |\
 awk '{print $1,$2,$3,$4}'

# 3  
Old 02-01-2009
Hi Digby,
Thanks very much for the reply but I was watching out for some unix shell script stuff for the solution.

Can anyone help in this....using shell script.

Thanks...
# 4  
Old 02-02-2009
Code:
printf "%-5s %-15s %-20s %-20s \n\n" "File" "Amount" "Ref" "Account No" > fileB
while read line
do
   if [ `echo $line | grep SEQ` ]
   then
      f1=`echo $line | cut -d"+" -f3- | cut -d"'" -f1`
   fi
   if [ `echo $line | grep MOA` ]
   then
      f2=`echo $line | cut -d"+" -f2-`
   fi
   if [ `echo $line | grep RFF` ]
   then
      f3=`echo $line | cut -d":" -f2-`
   fi
   if [ `echo $line | grep FII` ]
   then
      f4=`echo $line | cut -d"+" -f3-`
       printf "%-5s %-15s %-20s %-20s \n" $f1 $f2 $f3 $f4
   fi
done < textfile >> fileB

Code:
cat fileB

File  Amount          Ref                  Account No

1     9:000,00:ABC'   000000007'           0170++AA'
2     9:389,47:ABC'   02110300000008'      0PSTBNL2A:25:5+BB'
3     9:3086,55DD'    00009'               0348932928++GG'

# 5  
Old 02-02-2009
Hope this helps:

Code:
sed "s/^NAD.*$//" FileA | tr -d "\n" | sed "s/SEQ++/\n/g;s/MOA+/,/g;s/RFF+AIK:/,/g;s/FII+PH+/,/g;" | sed '1d'

Changes keywords into a comma ","

So that output will be:

Code:
$ sed "s/^NAD.*$//" FileA | tr -d "\n" | sed "s/SEQ++/\n/g;s/MOA+/,/g;s/RFF+AIK
:/,/g;s/FII+PH+/,/g;" | sed '1d'
1',9:000,00:ABC',000000007',0170++AA'
2',9:389,47:ABC',02110300000008',0PSTBNL2A:25:5+BB'
3',9:3086,55DD',00009',0348932928++GG'

# 6  
Old 02-02-2009
Hi both,

Thanks a lot.[Smilie]

Last edited by navojit dutta; 02-02-2009 at 04:30 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Proper Column wise matching

My below code works fine if none of the columns has pipe as its content in it, If it has the pipe in any of the content then the value moves to the next column. I wanted my code to work fine even if the column has pipe in it apart from the delimiter. NOTE : If there is a pipe in the content... (6 Replies)
Discussion started by: nikhil jain
6 Replies

2. Shell Programming and Scripting

Column wise text adding

Hi I have pasted sample data as below:- in data.txt Please suggest any way out: as the 3rd field is cat data.txt 22:37:34 STARTING abc 22:37:40 FAILURE sadn 00:06:42 STARTING asd 00:06:51 FAILURE ad 02:06:38 STARTING acs 02:06:46 FAILURE cz 04:06:35 STARTING xzc... (1 Reply)
Discussion started by: Gaurav198
1 Replies

3. Red Hat

How to find a garbage entry in a column wise text file in Linux?

Suppose I have a file containing :- 1 Apple $50 2 Orange $30 3 Banana $10 4 Guava $25 5 Pine@apple $12 6 Strawberry $21 7 Grapes $12 In the 5th row, @ character inserted. I want through sort command or by any other way this row should either on top or bottom. By sort command garbage... (1 Reply)
Discussion started by: Dipankar Mitra
1 Replies

4. Shell Programming and Scripting

Adding content of two file in a single file column wise

Hi, I am trying to get the file in particular pattern using shell script. I have to add one column to some other file. For example consider two file as below. File1: name1 name2 name3 File2: Add1 age1 Add2 age2 Add3 age3 I want this two file in a single file format something like... (3 Replies)
Discussion started by: diehard
3 Replies

5. Shell Programming and Scripting

read file line by line print column wise

I have a .csv file which is seperated with (;) inputfile --------- ZZZZ;AAAA;BBB;CCCC;DDD;EEE; YYYY;BBBB;CCC;DDDD;EEE;FFF; ... ... reading file line by line till end of file. while reading each line output format should be . i need to print only specific columns let say 5th... (2 Replies)
Discussion started by: rocking77
2 Replies

6. Shell Programming and Scripting

Search a file column wise and delete it

Scottn, m really sorry but i have not got my answer yet. my concern is how to delete the row !!! i have a file which has a column that is unique i am intending to serach it and if it is there to remove the row. the file looks like ROLLNO,NAME ,SUB1,SUB2,SUB3,TOTAL,PERCENTAGE,RESULT... (9 Replies)
Discussion started by: gotam
9 Replies

7. Shell Programming and Scripting

Search a file column wise and delete it

i have a file which has a column that is unique i am intending to serach it and if it is there to remove the row. the file looks like ROLLNO,NAME ,SUB1,SUB2,SUB3,TOTAL,PERCENTAGE,RESULT 15 ,rig ,34 ,56 ,87 ,177 ,59 % ,PASS 23 ,wel ,45 ,76 ,56 ,177 ,59 % ... (0 Replies)
Discussion started by: gotam
0 Replies

8. Solaris

column wise substitution in a file

Hi, I have two files. Want to make an addition of the fifth column of from both the files and redirect it to a third file. Both files have same records except fifth field and same record should be inserted into new file having fifth field as addition of fifth fields of both files. for... (2 Replies)
Discussion started by: sanjay1979
2 Replies

9. Shell Programming and Scripting

o/p column wise by nawk

hi i have file which hav following entries 1501,AAA,2.00 1525,AAA,2.00 1501,AAA,2.00 1525,AAA,2.00 1501,AAA,3.00 1525,AAA,3.00 1525,AAA,3.00 1501,AAA,3.00 1501,AAA,3.00 i want to have a o/p coloum wise like 1501,AAA,13 1525,AAA,10 here 13 comes as a sum of last colum value... (6 Replies)
Discussion started by: aaysa123
6 Replies

10. Shell Programming and Scripting

processing matrix column wise

I have a m X n matrix written out to file, say like this: 1,2,3,4,5,6 2,6,3,10,34,67 1,45,6,7,8,8 I want to calculate the column averages in the MINIMUM amount of code or processing possible. I would have liked to use my favorite tool, "AWK" but since it processes rowwise, getting the... (5 Replies)
Discussion started by: Abhishek Ghose
5 Replies
Login or Register to Ask a Question