Home Man
Search
Today's Posts
Register

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

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

Login to Reply

 
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:
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..
# 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
Login to Reply

« Previous Thread | Next Thread »
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
Automating using shell script : edit the file in a directory Abdul Navaz Shell Programming and Scripting 1 04-13-2014 06:15 PM
Open the file and edit/append the text greenworld123 Shell Programming and Scripting 1 09-15-2011 11:02 AM
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
How to edit particular cell of csv file using shell script deepak_p86 Shell Programming and Scripting 6 04-06-2009 07:11 AM
shell script to edit file and delete entry tannu Shell Programming and Scripting 6 09-09-2008 03:04 PM
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
How to edit txt file by shell script? dupeng AIX 3 09-29-2005 03:43 AM


All times are GMT -4. The time now is 02:43 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password