Unix/Linux Go Back    


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

Shell Programming and Scripting


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

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-04-2017   -   Original Discussion by ektubbe
ektubbe's Unix or Linux Image
ektubbe ektubbe is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 11 July 2017, 10:11 AM EDT
Posts: 39
Thanks: 11
Thanked 0 Times in 0 Posts
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..
Sponsored Links
    #2  
Old Unix and Linux 07-04-2017   -   Original Discussion by ektubbe
bakunin's Unix or Linux Image
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 19 April 2018, 3:21 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,740
Thanks: 112
Thanked 1,666 Times in 1,225 Posts
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
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 05:52 AM.