?
Solved

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

Posted on 2015-01-30
3
Medium Priority
?
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
johnsone earned 2000 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 35

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Simple Linear Regression
Suggested Courses

752 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