Open and edit a file using a shell script

Tags
file edit, shell script, shell scripts, unix & linux - unix commands

 
Thread Tools Search this Thread
# 1  
Old 07-04-2017
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 -
Code:
20161030,20161031,20161101

I need to remove this line and replace it with something like below -
Code:
$param1,$param2,$param3

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

Moderator's Comments:
Mod Comment 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 06:38 AM..
# 2  
Old 07-04-2017
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:

Code:
#! /bin/ksh

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

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

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

exit 0

Save this to a file script.sh, flag it executable and call it with ./script.sh "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.

bakunin

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Edit the file in shell script Abdul Navaz Homework & Coursework Questions 3 04-17-2014 10:40 PM
Automating using shell script : edit the file in a directory Abdul Navaz Shell Programming and Scripting 1 04-13-2014 07:15 PM
To open a file in shell script. Karthick N Shell Programming and Scripting 3 05-26-2013 02:06 PM
Open a file from within a Bourne shell Script dordrix Shell Programming and Scripting 3 02-15-2012 03:17 PM
Open the file and edit/append the text greenworld123 Shell Programming and Scripting 1 09-15-2011 12:02 PM
shell script to edit a file jacky29 Shell Programming and Scripting 2 04-01-2011 06:39 AM
Shell script to edit a file Titas Shell Programming and Scripting 3 02-14-2011 03:29 AM
Shell script to Open file in SFTP and get first 6 character cherriesh Shell Programming and Scripting 0 07-13-2010 06:58 AM
Can we edit crontab using a shell script rudrarajumk Shell Programming and Scripting 6 06-16-2009 09:45 AM
How to edit particular cell of csv file using shell script deepak_p86 Shell Programming and Scripting 6 04-06-2009 08:11 AM
shell script to edit file and delete entry tannu Shell Programming and Scripting 6 09-09-2008 04:04 PM
Edit a config file using shell script rajeshomallur Shell Programming and Scripting 7 05-02-2008 03:57 PM
shell script to edit the content of a file tiger99 Shell Programming and Scripting 3 01-31-2008 04:43 AM
open a file in shell script bankpro Shell Programming and Scripting 2 02-07-2006 12:59 PM
How to edit txt file by shell script? dupeng AIX 3 09-29-2005 04:43 AM