Operating System - HP-UX
1827422 Members
4565 Online
109965 Solutions
New Discussion

Re: Help in automatically removing lines.

 
SOLVED
Go to solution
Soul_1
Respected Contributor

Help in automatically removing lines.

Hi,

i just want to remove the lines in a file from a particular text to end of the file.

eg: consider a file "test" in that
line no 1
line no 2
line no 3
#new lines
Word1
word2

my requirement is to remove the lines after "#new lines"

Please suggest me on the same.
22 REPLIES 22
Soul_1
Respected Contributor

Re: Help in automatically removing lines.

hi

Forget to mention :

I need one script to automatically read and delete the kines after the particular text.


Regards,
Dennis Handly
Acclaimed Contributor

Re: Help in automatically removing lines.

awk '
/#new lines/ {
print $0
exit
}
{print $0}' file > file.new
Soul_1
Respected Contributor

Re: Help in automatically removing lines.

Hi Dennis,

Thanks for your response.
Can you brief what this script will do ?

Dennis Handly
Acclaimed Contributor
Solution

Re: Help in automatically removing lines.

>Can you brief what this script will do?

It will print lines from file to file.new.
When it sees a line with "#new lines", it will print that, then exit.
James R. Ferguson
Acclaimed Contributor

Re: Help in automatically removing lines.

Hi:

One way is:

# sed -n '1,/^#new/p' file

This begins by turning off any printing (the '-n'). If the line number is within the inclusive range of line number one (1) through a line that begins (^) with the string "new", then print the line.

Regards!

...JRF...
Hemmetter
Esteemed Contributor

Re: Help in automatically removing lines.

Hi
and inversed expression to JRF's sed:

# sed '/new lines/,$d' test

rgds
HGH
Arturo Galbiati
Esteemed Contributor

Re: Help in automatically removing lines.

hello,
in general to delete lines between START END:
sed -n'/^START/,/^END/d' file.in >file.out

HTH,
Art
Steve Post
Trusted Contributor

Re: Help in automatically removing lines.

Don't forget about that -n in the sed command. It tells sed to suppress printing out unless you call for it.

$ cat junk
one
two
three
START
four
five
six
seven
END
eight
nine
ten

$ cat junk | sed '/^START/,/^END/d'
one
two
three
eight
nine
ten

$ cat junk | sed -n '/^START/,/^END/p'
START
four
five
six
seven
END

Siddhartha Sarkar
New Member

Re: Help in automatically removing lines.

you can use :
echo "head -$(($(grep -n '#new lines' test | awk -F : '{print ($1)}') -1 )) test >> test2" |sh

This will find the line number where pattern #new lines is , then subtract 1 from it and use head to put it in another file test2.

Thanks
Dennis Handly
Acclaimed Contributor

Re: Help in automatically removing lines.

>Siddhartha: This will find the line number where pattern #new lines is

And will also read the whole file, then the first part again.
Soul_1
Respected Contributor

Re: Help in automatically removing lines.

Hi,

I want to print the previous day date.
Please let me know how can i achieve it.


Thanks in advance
Horia Chirculescu
Honored Contributor

Re: Help in automatically removing lines.

Hello,

echo `(export TZ=XYZ+24; date "+%d/%m/%y")`

Horia.
Best regards from Romania,
Horia.
Soul_1
Respected Contributor

Re: Help in automatically removing lines.

Hi Horia,

Thanks a lot for your response

can you just brief what this command will do ?

Also what will happen at the starting of month. say jan 1. In that case will it display the last year an last month date ?

Regards,
Horia Chirculescu
Honored Contributor

Re: Help in automatically removing lines.

Hello.

>Also what will happen at the starting of month. say jan 1. In that case will it display the last year an last month date ?

Yes, it will work. Will work also on March, 1 showing February 28/29 and so...

Horia.
Best regards from Romania,
Horia.
Soul_1
Respected Contributor

Re: Help in automatically removing lines.

Hi,
I want to customize the year part of the o/p.

rt now its showing as 07/03/10 , iwant it in 07/03/2010.

Regards
Horia Chirculescu
Honored Contributor

Re: Help in automatically removing lines.

>now its showing as 07/03/10 , iwant it in 07/03/2010.

Just change %y with %Y

man date should help.

Horia.
Best regards from Romania,
Horia.
James R. Ferguson
Acclaimed Contributor

Re: Help in automatically removing lines.

Hi:

> I want to customize the year part of the o/p...its showing as 07/03/10 , i want it in 07/03/2010

# perl -MPOSIX -le 'print strftime "%02m/%02d/%Y",localtime(time-(60*60*24))'

...will give you yesterday's date correctly, crossing year boundaries as necessary.

You can change the format of the output just like you would using a simple 'date' command format.

Do *not* be fooled by trying to manipulate your 'TZ' variable by adding or subtracting 24-hours. This works correctly only for UTC (GMT) timezones. Use Perl;

Regards!

...JRF...

Horia Chirculescu
Honored Contributor

Re: Help in automatically removing lines.

James: > This works correctly only for UTC (GMT) timezones


Can you please explain this?


Horia.
Best regards from Romania,
Horia.
Dennis Handly
Acclaimed Contributor

Re: Help in automatically removing lines.

>Horia: Can you please explain this?

The code in libc only expects to have valid timezones. These are UTC +/- 12 hours.

What you did was:
$ TZ=XYZ+24 date "+%d/%m/%y %Z"
08/03/10 XYZ

This converts to UTC time and adds 24 hours. What if there was a DST transition and you skip or lose a day?
Horia Chirculescu
Honored Contributor

Re: Help in automatically removing lines.

Dennis,
>This converts to UTC time and adds 24 hours. What if there was a DST transition and you skip or lose a day?

I understand.

>What you did was:
>$ TZ=XYZ+24 date "+%d/%m/%y %Z"
>08/03/10 XYZ

When the timezone is not defined (XYZ), it defaults to UTC/GMT


I found a similar discussion from some years ago:

http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1074176

James explains that only in Europe (and why) this would work.

Horia.
Best regards from Romania,
Horia.
Soul_1
Respected Contributor

Re: Help in automatically removing lines.

Hi all,

I am confused, :)

GMT+24, PST+24 and XYZ+24 all are giving the same o/p.

Please help me to find the difference b/w these.


Regards,
James R. Ferguson
Acclaimed Contributor

Re: Help in automatically removing lines.

Hi:

> I am confused, :)

As I said in your thread, and as I and others said in the thread Horia referenced, persisting in trying to add or subtract an hour offset to TZ is not always going to work!

The tiny Perl snippet to obtain yesterday's or tomorrow's date (or virtually any date in the past or future by subtracting or adding some number of Epoch seconds) is guaranteed to yield accurate date and time offsets.

Regards!

...JRF...