LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-15-2009, 12:23 PM   #1
saurabhchokshi
Member
 
Registered: May 2007
Posts: 35

Rep: Reputation: 15
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
 
Old 06-15-2009, 02:16 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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?
 
Old 06-16-2009, 01:06 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
@OP, awk
Code:
 awk '/:$/{exit}1' file
Quote:
Originally Posted by pixellany View Post
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 ?

Last edited by ghostdog74; 06-16-2009 at 01:07 AM.
 
Old 06-16-2009, 06:48 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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?
 
Old 06-16-2009, 07:22 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pixellany View Post
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'
 
Old 06-16-2009, 08:06 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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???
 
Old 06-16-2009, 08:22 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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 ?
 
Old 06-16-2009, 09:12 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Truncate from beginning of the file lvprabhu Programming 12 05-25-2010 09:43 PM
Truncate A File Dovetails Linux - Newbie 2 10-22-2005 06:58 PM
truncate a text file in CLI? hottdogg Linux - General 2 09-06-2005 09:32 PM
truncate lines with c alaios Programming 17 08-25-2005 08:51 AM
Zipslack 9.1 long file names truncate jimk Slackware - Installation 5 05-16-2004 06:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration