delete only part of a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete only part of a line
# 1  
Old 03-23-2011
Bug delete only part of a line

Hi,

I've a file as below-

Quote:
...
lastupd_inftim datetime year to minute
) lock mode row;

....
lastupd_inftim datetime year to minute
)extent size 50000 next size 25000 lock mode row;

...
cnv_driver char(3))extent size 50000 next size 25000 lock mode row;
I need to delete only the part of the line after the pattern 'extent /lock' and my output has to be like below.

Quote:
...
lastupd_inftim datetime year to minute
);

....
lastupd_inftim datetime year to minute
);

...
cnv_driver char(3));
Thanks
# 2  
Old 03-23-2011
Code:
perl -pnle 's/(extent|lock).*(.)$/$2/g' inputfile

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 03-23-2011
MySQL

Quote:
Originally Posted by pravin27
Code:
perl -pnle 's/(extent|lock).*(.)$/$2/g' inputfile

Thanks Pravin!

That does exactly what i need, can u pls give a brief explanation on logic behind it, as im new to perl.

And also, can u pls suggest me some good books for perl.

Thanks.
# 4  
Old 03-23-2011
there's no need to use backrefereneces.
Code:
$ ruby -ne 'print $_.gsub(/(lock|extent).*$/,"")' file

# 5  
Old 03-23-2011
Code:
perl -pnle 's/(extent|lock).*(.)$/$2/g' inputfile

The -e switch allows to write Perl scripts directly on the command line.
The -n switch wraps a while loop around your program
The -p switch prints the value of $_ at the end of each iteration.
The -l switch print newline.
Code:
s/(extent|lock).*(.)$/$2/g

- Substitute "extent OR (|) lock followed by any character till second last character, (.) last character." with last character.i.e. group 2
# 6  
Old 03-23-2011
Quote:
Originally Posted by pravin27
Code:
perl -pnle 's/(extent|lock).*(.)$/$2/g' inputfile

The -e switch allows to write Perl scripts directly on the command line.
The -n switch wraps a while loop around your program
The -p switch prints the value of $_ at the end of each iteration.
The -l switch print newline.
Code:
s/(extent|lock).*(.)$/$2/g

- Substitute "extent OR (|) lock followed by any character till second last character, (.) last character." with last character.i.e. group 2

I got a problem here, there are few cases where it is split in to multiple lines.. But i need to remove the content till it finds a ; symbol.

Quote:
lastupd_inftim datetime year to minute
)extent size 50000
next size 25000 lock mode row;
Thanks
# 7  
Old 03-23-2011
There is no need to specify "-n" when you already have "-p". "-p" is a superset of "-n" Smilie
This User Gave Thanks to bartus11 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To Delete the duplicates using Part of File Name

I am using the below script to delete duplicate files but it is not working for directories with more than 10k files "Argument is too long" is getting for ls -t. Tried to replace ls -t with find . -type f \( -iname "*.xml" \) -printf '%T@ %p\n' | sort -rg | sed -r 's/* //' | awk... (8 Replies)
Discussion started by: gold2k8
8 Replies

2. Programming

Mismatched free() / delete / delete [] line no missing

Could you tell me the possibilities of the reason to get the Mismatched free() / delete / delete . I unable to see the line no in the valgrind report. it displays the function name. with that function name, I am not able to find where exactly the issue is there.I am getting the Mismatched free()... (3 Replies)
Discussion started by: SA_Palani
3 Replies

3. Shell Programming and Scripting

Delete part of a column

I want to delete a part of the 4th column from the given file below: <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456"... (2 Replies)
Discussion started by: my_Perl
2 Replies

4. Shell Programming and Scripting

Find and delete part of field with awk or sed

I've got a file that looks like this (the whitespace between commas is intentional): 123456789,12,JOHN H DOE ,DOE/JOHN H ,,,DOE/JOHN H ,,,,,123 FAKE STREET ,SPRINGFIELD,XX, I want to strip just the first name out of the third field so it reads "JOHN,". So far I... (6 Replies)
Discussion started by: Scottie1954
6 Replies

5. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

6. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

7. Shell Programming and Scripting

Delete Last part

Hello, I am on solaris (ksh). I have a log file like this with directory structure and file name : /DIR1/DIR2/DIR3/filename /DIR1/DIR2/filename /DIR1/DIR2/DIR3/DIR4/DIR5/filename I am looking for a way to create a new file to be formated like this : DIR2/DIR3 DIR2... (9 Replies)
Discussion started by: Aswex
9 Replies

8. Shell Programming and Scripting

delete part of file from pattern1 to pattern2

Hi all! How can I delete all the text starting from <string1> to <string2> in all the .txt files of the folder "FOLDER" ? Thanks a lot! mjomba ... </s> <s> <w></w> </s> <s> ... to get: (1 Reply)
Discussion started by: mjomba
1 Replies

9. Shell Programming and Scripting

Delete part of string

This command: du -s /Applications/TextMate.app Returns an output like this: 65792 /Applications/TextMate.app I need to delete the space and the file path in the output leaving just the number. Thanks (2 Replies)
Discussion started by: pcwiz
2 Replies

10. Shell Programming and Scripting

How to delete part of a file?

Suppose i have a file which contains 10 lines.... i have to delete a line which starts with the word "BEGIN" and delete the consecutive lines till i find a start of line with the word "END" how to do this? (6 Replies)
Discussion started by: brkavi_in
6 Replies
Login or Register to Ask a Question