Solved

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

Posted on 2015-01-30
3
280 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 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

695 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