Sponsored Content
Top Forums Shell Programming and Scripting How to insert text within a file? Post 302947429 by newbie_01 on Thursday 18th of June 2015 02:29:37 AM
Old 06-18-2015
How to insert text within a file?

Hi,

I am trying to check for missing dates in a file and would want to insert the missing date into the file.

Currently the script is as below

Code:
#!/bin/ksh

dates="dates"
cat ${dates} | grep -v "^#"

curr_month=`date '+%m`
curr_day=`date '+%d`
curr_year=`date '+%Y`

#curr_month=02
#curr_month=2016
#leap=${curr_year}; math=`echo "$leap%4" | bc`; [ ! -z $leap ] && [ $math -eq 0 ] && echo "$leap is a leap year!" || echo "$leap isn't a leap year";
leap=${curr_year}; math=`echo "$leap%4" | bc`; [ ! -z $leap ] && [ $math -eq 0 ] && leapyear="Y" || leapyear="N";

case "${curr_month}" in
   01|03|05|07|08|10|12 )
      max_days="31"
      ;;
   04|06|09|11 )
      max_days="30"
      ;;
   02 )
      case "${leapyear}" in
         Y )
            max_days=29
            ;;
         N )
            max_days=28
            ;;
      esac
      ;;
esac

echo
echo "curr_month = ${curr_month}"
echo "curr_day   = ${curr_day}"
echo "curr_year  = ${curr_year}"
echo "leapyear   = ${leapyear}"
echo "max_days   = ${max_days}"
echo

check_day=${curr_day}
while [[ ${check_day} -le ${max_days} ]]
do
   next_date="${curr_year}-${curr_month}-${check_day}"

   if [[ -z "`grep ${next_date} ${dates}`" ]] ; then
      echo "- Missing date ---> ${next_date}"
   fi

   let check_day=${check_day}+1
done

A sample run of the script gives output as below:

Code:
$ ./x.ksh
2015-05-01
2015-05-04
2015-05-05
2015-05-06
2015-05-07
2015-05-08
2015-05-12
2015-05-14
2015-05-15
2015-05-18
2015-05-27
2015-06-02
2015-06-03
2015-06-04
2015-06-05
2015-06-06
2015-06-07
2015-06-08
2015-06-09
2015-06-10
2015-06-11
2015-06-12
2015-06-13
2015-06-14
2015-06-15
2015-06-18
2015-06-22
2015-06-23
2015-06-24
2015-06-26
2015-06-30
2015-07-01

curr_month = 06
curr_day   = 18
curr_year  = 2015
leapyear   = N
max_days   = 30

- Missing date ---> 2015-06-19
- Missing date ---> 2015-06-20
- Missing date ---> 2015-06-21
- Missing date ---> 2015-06-25
- Missing date ---> 2015-06-27
- Missing date ---> 2015-06-28
- Missing date ---> 2015-06-29

I want to insert the missing date into the file in the right ascending order.

Any advice will be much appreciated. Thanks in advance.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

insert text into a dinamic file

I need help, It's probably a joke for you, but I'm about to explode: This is it: I have a file which is created by batch, and includes a list of db files (with the complete path). I want to use this list for a graph file - in order to exclude it from the daily backup. it means i need to add an... (2 Replies)
Discussion started by: sunbird
2 Replies

2. Shell Programming and Scripting

insert text in the middle of a file

I want to insert a text into the middle of a file (3 Replies)
Discussion started by: relle
3 Replies

3. UNIX for Dummies Questions & Answers

How to insert text in the middle of a file

Hey guys, how do we take a line of text as an argument from a user and then insert it in the middle of a file irrespective of the number of lines in the file. I am trying to do this without SED or AWK. Inserting it in the beginning and at the end is easy, but i am trying to accomplish inserting... (6 Replies)
Discussion started by: kartikkumar84@g
6 Replies

4. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

5. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

6. Shell Programming and Scripting

Insert Text On file

Hi All, Can someone pls help me to insert some text on a file. my file contains something like below.. AKBULBU, BALUMIL, BATCH,BATCH BOARROB, BOTAKAT, C57896, CAKIOZE, CHECMER, CICOFRA, CISZPAW,2194485 I want output as USER_ID, LOGIN_ID (6 Replies)
Discussion started by: harshakusam
6 Replies

7. Shell Programming and Scripting

Insert text to file via ssh

I want to create a script that will set up key pairs from "thishost" to "remotehosts". I have a validate script in ~/<dir> that I need to prefix the generated key in the authorized_keys file, and I'm having to manually do this everytime. Here are my attemps, perhaps you could perfect them for... (3 Replies)
Discussion started by: ScannerDarkly
3 Replies

8. UNIX for Dummies Questions & Answers

insert comma in a text file

Hi all, I have a text file and I need to insert comma after every 2 digit. -1-1-1-1-1-1-1-1-1 0 0 0 -1-1-1 2 0 0 3 311-1 0 1 -1-1 021 0 011-1-1 033 0I'd like to have this: -1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0 -1,-1,-1, 2, 0, 0, 3, 3,11,-1, 0, 1 -1,-1, 0,21, 0, 0,11,-1,-1, 0,33, 0Thanks for... (7 Replies)
Discussion started by: GoldenFire
7 Replies

9. Shell Programming and Scripting

Insert value to db from text file

Hi, I have a single value in insertval file. I want to load that value to database with the current date. I tried the below code but it is inserting <NULL> to database and echo $c is also null. cat insertval | awk -F ' ' '{print $1}' > c echo c=$c data=`sqlplus -s user/pwd@hostname <<EOF ... (5 Replies)
Discussion started by: Neethu
5 Replies

10. Shell Programming and Scripting

How to insert text using a second file in Linux

How can i insert text at a particular line number or before/after a search pattern? The text i want to insert is of 8-10 lines including new line characters. I have stored this text in a separate file. I know i can use sed '{/pattern/|/regexp/|n}{i|a|c}<text to be inserted>' file But i want... (7 Replies)
Discussion started by: abhitanshu
7 Replies
DP(8)								     [nmh-1.5]								     DP(8)

NAME
dp - parse dates 822-style SYNOPSIS
/usr/lib/mh/dp [-form formatfile] [-format string] [-width columns] [-version] [-help] dates ... DESCRIPTION
Dp is a program that parses dates according to the ARPA Internet standard. It also understands many non-standard formats, such as those produced by TOPS-20 sites and some UNIX sites using ctime(3). It is useful for seeing how nmh will interpret a date. The dp program treats each argument as a single date, and prints the date out in the official 822-format. Hence, it is usually best to enclose each argument in quotes for the shell. To override the output format used by dp, the -format string or -format file switches are used. This permits individual fields of the address to be extracted with ease. The string is simply a format string and the file is simply a format file. See mh-format(5) for the details. Here is the default format string used by dp: %<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%> which says that if an error was detected, print the error, a `:', and the date in error. Otherwise, output the 822-proper format of the date. FILES
$HOME/.mh_profile The user profile PROFILE COMPONENTS
None SEE ALSO
ap(8), Standard for the Format of ARPA Internet Text Messages (RFC-822) DEFAULTS
`-format' default as described above `-width' default to the width of the terminal CONTEXT
None BUGS
The argument to the -format switch must be interpreted as a single token by the shell that invokes dp. Therefore, one must usually place the argument to this switch inside quotes. MH.6.8 11 June 2012 DP(8)
All times are GMT -4. The time now is 03:56 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy