• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 590
  • Last Modified:

HP-UX Korn Script - How to delete the first line from a file?

I need help with a HP-UX Script. I'm using Korn Script.

I compare two dates, one is the system date and the other is a list of dates listed in a file called ph.txt

If the system date (variable $datentime) and phdate (from ph.txt) match, and are the same, I would like to delete the first line from the file ph.txt.

How do I do this, please?


#!/usr/bin/ksh

datentime=$(date '+%Y%m%d')
phdate=$(head -1 /usr/users/amsbmi/tmp/ph.txt)

if [[ $datentime -eq $phdate ]];
then
echo "Correct"
else
echo "Wrong"
fi
0
ben1211
Asked:
ben1211
  • 3
  • 2
  • 2
4 Solutions
 
vikas_madhusudanaCommented:
tail -n+2 ph.txt | tee ph.txt
0
 
ben1211Author Commented:
Hi, could you explain what this command does please? I would like to have some understanding of what I'm doing.

By the way, tail removes the last line of the file right? I would like to remove the first line from that file.
0
 
TintinCommented:
Here you do
!/usr/bin/ksh

FILE=/usr/users/amsbmi/tmp/ph.txt

datentime=$(date '+%Y%m%d')
phdate=$(head -1 $FILE)

if [[ $datentime -eq $phdate ]]
then
   sed 1d $FILE >/tmp/$$ && mv $/tmp/$$ $FILE
fi

Open in new window

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
vikas_madhusudanaCommented:
no tail -n+2 will remove the first line

ie you are asking to print the lines till end of file starting from line number 2.
0
 
vikas_madhusudanaCommented:
tee i used to redirect the output to file
0
 
ben1211Author Commented:
Thanks Vikas. Much appreciated.

Tintin, thanks for the code too. Could please explain the line sed 1d $FILE >/tmp/$$ && mv $/tmp/$$ $FILE to me, please?

I've tried looking it up, can't get a good understanding of it. Your help is much appreciated.
0
 
TintinCommented:
sed 1d

means (d)elete line 1.

As sed can't directly make the changes to the file, it needs to write it to a temporary file (/tmp/$$).  The $$ gives you a semi-unique name as it is the PID of the running process.

The && is a logical AND that ensures you don't write back the temporary file over the original if the sed statement failed (eg: disk full).

Please note I made a typo

sed 1d $FILE >/tmp/$$ && mv $/tmp/$$ $FILE

should be

sed 1d $FILE >/tmp/$$ && mv /tmp/$$ $FILE

ie: no $ in front of /tmp
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now