LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Truncate the Lines from the File (https://www.linuxquestions.org/questions/programming-9/truncate-the-lines-from-the-file-733103/)

saurabhchokshi 06-15-2009 12:23 PM

Truncate the Lines from the File
 
Hi,

I want to truncate the file once I found the desired pattern.

For Example :-

Input :-

Quote:

crw------- 1 vcsa tty 7, 3 Jun 13 01:22 /dev/vcs3
crw------- 1 vcsa tty 7, 4 Jun 13 01:22 /dev/vcs4
crw------- 1 vcsa tty 7, 5 Jun 13 01:22 /dev/vcs5
crw------- 1 vcsa tty 7, 6 Jun 13 01:22 /dev/vcs6
crw------- 1 vcsa tty 7, 128 Jun 13 01:21 /dev/vcsa
crw------- 1 vcsa tty 7, 129 Jun 13 01:21 /dev/vcsa1
crw------- 1 vcsa tty 7, 130 Jun 13 01:22 /dev/vcsa2
crw------- 1 vcsa tty 7, 131 Jun 13 01:22 /dev/vcsa3
crw------- 1 vcsa tty 7, 132 Jun 13 01:22 /dev/vcsa4
crw------- 1 vcsa tty 7, 133 Jun 13 01:22 /dev/vcsa5
crw------- 1 vcsa tty 7, 134 Jun 13 01:22 /dev/vcsa6
lrwxrwxrwx 1 root root 4 Jun 13 01:21 /dev/X0R -> null
crw-rw-rw- 1 root root 1, 5 Jun 13 01:21 /dev/zero

/dev/:
total 0
crw------- 1 root root 189, 130 Jun 13 01:21 2-2
crw------- 1 root root 189, 385 Jun 13 01:21 4-1
crw------- 1 root root 189, 513 Jun 13 01:21 5-1
crw------- 1 root root 10, 175 Jun 13 01:21 agpgart
crw------- 1 root root 10, 59 Jun 13 01:21 autofs
drwxr-xr-x 3 root root 60 Jun 13 01:21 bus
crw------- 1 root root 5, 1 Jun 13 01:22 console
lrwxrwxrwx 1 root root 11 Jun 13 01:21 core -> /proc/kcore
crw------- 1 root root 10, 63 Jun 13 01:21 cpu_dma_latency

/dev/bus:
total 0
drwxr-xr-x 8 root root 160 Jun 13 01:21 usb

/dev/bus/usb:
total 0
drwxr-xr-x 2 root root 60 Jun 13 01:21 001
drwxr-xr-x 2 root root 80 Jun 13 01:21 002
drwxr-xr-x 2 root root 60 Jun 13 01:21 003
drwxr-xr-x 2 root root 80 Jun 13 01:21 004
drwxr-xr-x 2 root root 80 Jun 13 01:21 005
drwxr-xr-x 2 root root 60 Jun 13 01:21 006
The Output should be :-

Quote:

crw------- 1 vcsa tty 7, 3 Jun 13 01:22 /dev/vcs3
crw------- 1 vcsa tty 7, 4 Jun 13 01:22 /dev/vcs4
crw------- 1 vcsa tty 7, 5 Jun 13 01:22 /dev/vcs5
crw------- 1 vcsa tty 7, 6 Jun 13 01:22 /dev/vcs6
crw------- 1 vcsa tty 7, 128 Jun 13 01:21 /dev/vcsa
crw------- 1 vcsa tty 7, 129 Jun 13 01:21 /dev/vcsa1
crw------- 1 vcsa tty 7, 130 Jun 13 01:22 /dev/vcsa2
crw------- 1 vcsa tty 7, 131 Jun 13 01:22 /dev/vcsa3
crw------- 1 vcsa tty 7, 132 Jun 13 01:22 /dev/vcsa4
crw------- 1 vcsa tty 7, 133 Jun 13 01:22 /dev/vcsa5
crw------- 1 vcsa tty 7, 134 Jun 13 01:22 /dev/vcsa6
lrwxrwxrwx 1 root root 4 Jun 13 01:21 /dev/X0R -> null
crw-rw-rw- 1 root root 1, 5 Jun 13 01:21 /dev/zero
So, Once I found the pattern something like /: or /<some name>/: or /name/name/: or /name/name:. I should stop searching my file and truncate the file.

Could you please help me out to write a script to truncate the file?

Thanks,
Saurabh Chokshi

pixellany 06-15-2009 02:16 PM

Well....you can probably do this with a SED address range:

Code:

sed -n '1,/<pattern>/p' oldfilename > newfilename
There are many ways to do something like this--what utilities are you most familiar with?

ghostdog74 06-16-2009 01:06 AM

@OP, awk
Code:

awk '/:$/{exit}1' file
Quote:

Originally Posted by pixellany (Post 3574872)
Well....you can probably do this with a SED address range:

Code:

sed -n '1,/<pattern>/p' oldfilename > newfilename
There are many ways to do something like this--what utilities are you most familiar with?

what should <pattern> be ?

pixellany 06-16-2009 06:48 AM

Quote:

Originally Posted by ghostdog74
what should <pattern> be ?

Quote:

Originally Posted by the OP
I want to truncate the file once I found the desired pattern

I guess it would be the desired pattern---n'est-ce pas?

ghostdog74 06-16-2009 07:22 AM

Quote:

Originally Posted by pixellany (Post 3575738)
I guess it would be the desired pattern---n'est-ce pas?

i know, if it were you, what is your desired pattern. can i guess its the ":" at the end ? like this?
Code:

sed -n '1,/:$/p'

pixellany 06-16-2009 08:06 AM

I must be dense this morning----I don't have a "desired pattern". I don't even want to do any scripting right now....I have to go to work......;)

Maybe I don't understand the question.........

Anyone notice who's missing in this conversation???

ghostdog74 06-16-2009 08:22 AM

maybe let me rephrase, you supplied the solution
Code:

sed -n '1,/<pattern>/p'
. For the <pattern> part, which pattern do you think you would use to produce OP's desired result ?

pixellany 06-16-2009 09:12 AM

OP gave several examples of the "desired pattern"---I'd say it is up to him/her to answer this....

The more general answer (for the OP) is: Try things to see what happens. Scripting is learned by doing.


All times are GMT -5. The time now is 06:19 PM.