Solved

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

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoresSpecial  challenge 13 40
Excel object stays open 19 65
word0 challenge 4 53
C Programming - If Statement 8 49
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now