Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2015-01-30
3
Medium Priority
?
285 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Make the most of your online learning experience.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Starting up a Project
Suggested Courses

610 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