?
Solved

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

Posted on 2010-01-12
7
Medium Priority
?
568 Views
Last Modified: 2013-12-21
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
Comment
Question by:ben1211
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Accepted Solution

by:
vikas_madhusudana earned 1000 total points
ID: 26300379
tail -n+2 ph.txt | tee ph.txt
0
 

Author Comment

by:ben1211
ID: 26300767
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
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 1000 total points
ID: 26300944
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 7

Assisted Solution

by:vikas_madhusudana
vikas_madhusudana earned 1000 total points
ID: 26301220
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
 
LVL 7

Expert Comment

by:vikas_madhusudana
ID: 26301222
tee i used to redirect the output to file
0
 

Author Comment

by:ben1211
ID: 26310583
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
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 1000 total points
ID: 26312237
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question