Shell Programming and Scripting

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Open and edit a file using a shell script

👤 Login to reply

Old 07-04-2017
ektubbe ektubbe is offline
Registered User
Open and edit a file using a shell script

Hello Folks,
I have a file named as date.dat present at /tmp/abc location which has following data -

I need to remove this line and replace it with something like below -

Param1, Param2 and param3 stores the date based on some calculation in the same script.
Any leads will be greatly appreciated.

Moderator's Comments:
Open and edit a file using a shell script edit by bakunin: by now you could know that code and data go in CODE-tags, yes? Start using themselves instead of letting us edit your posts, please. Thank you

Last edited by bakunin; 07-04-2017 at 05:38 AM..
Sponsored Links
Old 07-04-2017
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
First off: what do you mean by "something like"? I am sure you look for a solution, not something like a solution, no? So, please, tell us what you need, not "something alike".

If i get you correctly you want to replace a line in a file with 3 selectable values, yes?

If so:

#! /bin/ksh

typeset chVal1="$1"
typeset chVal2="$2"
typeset chVal3="$3"
typeset fIn="/tmp/abc/date.dat"

sed '/^20161030,20161031,20161101$/ {
      }' "$fIn" > "$fIn".tmp

if [ $? -eq 0 ] ; then
     mv "$fIn".tmp "$fIn"
     print -u2 "Error editing file!
     rm -f "$fIn".tmp
     exit 1

exit 0

Save this to a file, flag it executable and call it with ./ "param1" "param2" "param3".

Note that the script is a barebone solution - no effort is made for paramter checking, etc.. Add this functionality ad libitum.

I hope this helps.

Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Edit the file in shell script Abdul Navaz Homework & Coursework Questions 3 04-17-2014 09:40 PM
shell script to edit a file jacky29 Shell Programming and Scripting 2 04-01-2011 05:39 AM
Shell script to edit a file Titas Shell Programming and Scripting 3 02-14-2011 02:29 AM
Edit a config file using shell script rajeshomallur Shell Programming and Scripting 7 05-02-2008 02:57 PM
shell script to edit the content of a file tiger99 Shell Programming and Scripting 3 01-31-2008 03:43 AM

All times are GMT -4. The time now is 07:39 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
Show Password

Not a Forum Member?
Forgot Password?