Solved

Unix Shell script

Posted on 2007-12-02
7
1,106 Views
Last Modified: 2013-12-20
I need to write a small sample script with the next features, it shouldn't be more thatn 10 lines of code:

I need a program that, for all the files (whose name is given as a parameter), will create a copy of them, changing the last 2 lines of each file for the content of an existing file in the current directory called ABC. The name of the new file should be the same as in the original file, but adding the string "mod" at the end.
0
Comment
Question by:axtur
  • 3
  • 2
  • 2
7 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 20393031
Is this an assignment question?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 20393084
please post what you`ve done so far and tell us where you have problems
0
 

Author Comment

by:axtur
ID: 20397410
This is for a friend who needs help, I should know how to do this, but I've forgotten completely!!

I've done this so far:

for i
do
echo "$i = "
cat $i
done

This code shows on screen the content of the files given as parameters, show the filename before the content of the file.
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 51

Expert Comment

by:ahoffmann
ID: 20397531
for i in "$*"; do
  cp "$i" ABC/
done
0
 

Author Comment

by:axtur
ID: 20397656
it looks like this won't change the content of the  last two lines of each file and won't rename the file to existing_filenameMOD (concatenate the string "MOD" to the filename), how can I add such features to the existing script?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 20398565
for i in "$*"; do
  cp "$i" ABC/"$i"mod
  cat existing-file-with-2-lines >> ABC/"$i"mod
done
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 20398863
ahoffmann, you've misinterpreted the requirements.  Here's a solution


#!/bin/sh
for i in $*
do
  [ -f $i ] || continue
 
  l=`wc -l <$i`
  l=`expr $l - 1`
  sed $l,\$d $i >${i}mod
  cat ABC >>${i}mod
done

Open in new window

0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
unix example issues 18 90
Not needed 13 112
JQuery serialize and unserialize 8 85
Work with App store 7 52
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

777 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