Solved

Unix Shell Scripting to Change the header of the file in Unix

Posted on 2015-01-30
3
277 Views
Last Modified: 2015-02-05
Hi,

I have a situation here:

I have some files:
 update_1.dat, update_2.dat,update_3.dat.....update_x.dat

Each file will have a header as first record in it to determine it's sequence.

Say for ex:

update_1.dat
001|201501300940193201|XXXX02|1|201501303|YYYY30|20|1|1|

update_2.dat
001|201501300940193201|XXXX02|1|201501303|YYYY30|20|1|1|

The above is what I have right now..

I am expecting and what I am trying to achieve is
001|201501300940193201|XXXX02|1|201501303|YYYY30|20|1|2| -- Update_1.dat
001|201501300940193201|XXXX02|1|201501303|YYYY30|20|2|2| -- Update_2.dat and so on

If I have three files ,then  header should be

001|201501300940193201|XXXX02|1|201501303|YYYY30|20|1|3| -- Update_1.dat
001|201501300940193201|XXXX02|1|201501303|YYYY30|20|2|3| -- Update_2.dat and so on
001|201501300940193201|XXXX02|1|201501303|YYYY30|20|3|3| -- Update_2.dat and so on

I want some advices and suggestions to achieve this.

Also, it would be great if I get the sample code piece to achieve the above scenario.

Thanks
0
Comment
Question by:Nandika Kodituwakku
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
johnsone earned 500 total points
ID: 40579539
If I understand what you are trying to do, is that you want to change the first line of each file.  The last 2 fields need to be changed.  The last field is the number of files and the second to last field is the sequence number of the file.

If so, given your limited sample, this should work:
#!/bin/sh

num_files=`ls -1 *.dat | wc -l`
file_seq=1

for file in *.dat
do
  first_line="`head -1 $file`"
  start_line=`echo "$first_line" | cut -f1-7 -d'|'`
  new_first_line=`echo $start_line"|"$file_seq"|"$num_files"|"`
  echo $new_first_line > /tmp/tfile.$$
  tail -n +2 $file >> /tmp/tfile.$$
  mv /tmp/tfile.$$ $file.new
  file_seq=`expr $file_seq + 1`
done

Open in new window

This will create a modified file with a .new extension.  It should be pretty easy to change the one line and have it replace the original file if that is what you want.

If this doesn't do what you are looking for, please provide a more complete sample and expected results.

This is the brute force method to do the task.  There is probably a more pretty way to do it, but this way works and just about everyone can understand it.
0
 

Author Comment

by:Nandika Kodituwakku
ID: 40580613
I've requested that this question be deleted for the following reason:

I want to hide it..I got the solution from the expert..I am happy
0
 
LVL 34

Expert Comment

by:johnsone
ID: 40580614
If the solution is posted, you should accept the solution.  You cannot delete it, just because.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question