Sponsored Content
Top Forums Shell Programming and Scripting Checking the last line and deleting Post 92660 by linuxpenguin on Tuesday 13th of December 2005 01:04:00 PM
Old 12-13-2005
perl -pi -e "if(eof) { /^\s*$/ ; chomp ; }" filename

This will remove the last line of file only if that line is blank. It will directly edit the file without having to make a temp copy of the file.
Hope this helps
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Checking for a file in file pattern before deleting it

Hi, I need a script where I have delete all the files of type abc*.* from the directory /lmn/opq (passed as parameter to script) But I need to check if there is file of type abc*.* existing in the directory or not before I use the rm abc*.* command. Thanks (1 Reply)
Discussion started by: dsrookie
1 Replies

2. UNIX for Advanced & Expert Users

Deleting First 10 letters in a line

Hi, Could any one of you let me know any simple Unix command for deleting first 10 letters of first line in unix? Eg: 123456789ABC --Input ABC--Output Thanks Sue (9 Replies)
Discussion started by: pyaranoid
9 Replies

3. Shell Programming and Scripting

Checking files size and deleting if bigger than x

Hello , I have to write a crontab line make a check on a file and, if bigger than 2Gb, to stop apache daemon, delete the file and restart apache . Someone have suggestions ? Thanks (2 Replies)
Discussion started by: gogol_bordello
2 Replies

4. Shell Programming and Scripting

Deleting Characters at specific position in a line if the line is certain length

I've got a file that would have lines similar to: 12345678 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 23456781 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 34567812 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 45678123 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 xx.00... (10 Replies)
Discussion started by: Cailet
10 Replies

5. UNIX for Dummies Questions & Answers

Deleting every other Line in VI

Hello, How would one delete every other two lines using VI? For example, I have the following 8 lines: Line1 Line2 Line3 Line4 Line5 Line6 Line7 Line8 And wish to only delete lines 3-4 and 7-8. Is there a certain pattern which would make this easier? Thank you, -Jahn (8 Replies)
Discussion started by: Jahn
8 Replies

6. Shell Programming and Scripting

Deleting new line characters

Hi, I have a weird requirement. I am having a file with 12fields in it and the end of the line for each record is "\n" (Just \n and no carriage returns) and the field delimiter is "|". Problem is I can have new line characters in any field in the data and these new line characters can even come... (11 Replies)
Discussion started by: ngkumar
11 Replies

7. UNIX for Dummies Questions & Answers

Deleting a pattern in UNIX without deleting the entire line

Hi I have a file: r58778.3|SOURCES={KEY=f665931a...,fw,221-705}|ERRORS={16_1:T,30_1:T,56_1:C,57_1:T,59_1:A,101_1:A,115:-,158_1:C,186_1:A,204:-,271_1:T,305:-,350_1:C,368_1:G,442_1:C,472_1:G,477_1:A}|SOURCE_1="Contig_1092402550638"(f665931a359e36cea0976db191ff60ff09cc816e) I want to retain... (15 Replies)
Discussion started by: Alyaa
15 Replies

8. Shell Programming and Scripting

Checking number of commas in each line.

Hi All, I am checking whether each line is having "n" number of commas or nor. In case not then I need to exit the process. I tried cat "$TEMP_FILE" | while read LINE do processing_line=`expr $processing_line + 1` no_of_delimiters=`echo "$LINE" | awk -F ',' '{ print NF }'` if ... (4 Replies)
Discussion started by: Anupam_Halder
4 Replies

9. Shell Programming and Scripting

Checking has for a command line argument

I would like to search and print a match of the user entered $ARGV. Im terrible with hashes and really dont know where to go from here. The example data file name location phone Bugs holeintheground 5551212 Woody holeinthetree 6661313 Jerry holeinthewall 7771414... (4 Replies)
Discussion started by: sumguy
4 Replies

10. Shell Programming and Scripting

Deleting double quoted string from a line when line number is variable

I need to remove double quoted strings from specific lines in a file. The specific line numbers are a variable. For example, line 5 of the file contains A B C "string" I want to remove "string". The following sed command works: sed '5 s/\"*\"//' $file If there are multiple... (2 Replies)
Discussion started by: rennatsb
2 Replies
Perl6::Slurp(3pm)					User Contributed Perl Documentation					 Perl6::Slurp(3pm)

NAME
Perl6::Slurp - Implements the Perl 6 'slurp' built-in SYNOPSIS
use Perl6::Slurp; # Slurp a file by name... $file_contents = slurp 'filename'; $file_contents = slurp '<filename'; $file_contents = slurp '<', 'filename'; $file_contents = slurp '+<', 'filename'; # Slurp a file via an (already open!) handle... $file_contents = slurp *STDIN; $file_contents = slurp $filehandle; $file_contents = slurp IO::File->new('filename'); # Slurp a string... $str_contents = slurp $string; $str_contents = slurp '<', $string; # Slurp a pipe... $str_contents = slurp 'tail -20 $filename |'; $str_contents = slurp '-|', 'tail', -20, $filename; # Slurp with no source slurps from whatever $_ indicates... for (@files) { $contents .= slurp; } # ...or from the entire ARGV list, if $_ is undefined... $_ = undef; $ARGV_contents = slurp; # Specify I/O layers as part of mode... $file_contents = slurp '<:raw', $file; $file_contents = slurp '<:utf8', $file; $file_contents = slurp '<:raw :utf8', $file; # Specify I/O layers as separate options... $file_contents = slurp $file, {raw=>1}; $file_contents = slurp $file, {utf8=>1}; $file_contents = slurp $file, {raw=>1}, {utf8=>1}; $file_contents = slurp $file, [raw=>1, utf8=>1]; # Specify input record separator... $file_contents = slurp $file, {irs=>" "}; $file_contents = slurp '<', $file, {irs=>" "}; $file_contents = slurp {irs=>" "}, $file; # Input record separator can be regex... $file_contents = slurp $file, {irs=>qr/ +/}; $file_contents = slurp '<', $file, {irs=>qr/ +| {2,}}; # Specify autochomping... $file_contents = slurp $file, {chomp=>1}; $file_contents = slurp {chomp=>1}, $file; $file_contents = slurp $file, {chomp=>1, irs=>" "}; $file_contents = slurp $file, {chomp=>1, irs=>qr/ +/}; # Specify autochomping that replaces irs # with another string... $file_contents = slurp $file, {irs=>" ", chomp=>" "}; $file_contents = slurp $file, {chomp=>" "}, {irs=>qr/ +/}; # Specify autochomping that replaces # irs with a dynamically computed string... my $n = 1; $file_contents = slurp $file, {chomp=>sub{ " #line ".$n++." "}; # Slurp in a list context... @lines = slurp 'filename'; @lines = slurp $filehandle; @lines = slurp $string; @lines = slurp '<:utf8', 'filename', {irs=>"x{2020}", chomp=>" "}; DESCRIPTION
"slurp" takes: o a filename, o a filehandle, o a typeglob reference, o an IO::File object, or o a scalar reference, converts it to an input stream if necessary, and reads in the entire stream. If "slurp" fails to set up or read the stream, it throws an exception. If no data source is specified "slurp" uses the value of $_ as the source. If $_ is undefined, "slurp" uses the @ARGV list, and magically slurps the contents of all the sources listed in @ARGV. Note that the same magic is also applied if you explicitly slurp <*ARGV>, so the following three input operations: $contents = join "", <ARGV>; $contents = slurp *ARGV; $/ = undef; $contents = slurp; are identical in effect. In a scalar context "slurp" returns the stream contents as a single string. If the stream is at EOF, it returns an empty string. In a list context, it splits the contents after the appropriate input record separator and returns the resulting list of strings. You can set the input record separator ("{ irs => $your_irs_here}") for the input operation. The separator can be specified as a string or a regex. Note that an explicit input record separator has no effect in a scalar context, since "slurp" always reads in everything anyway. In a list context, changing the separator can change how the input is broken up within the list that is returned. If an input record separator is not explicitly specified, "slurp" defaults to " " (not to the current value of $/ X since Perl 6 doesn't have a $/); You can also tell "slurp" to automagically "chomp" the input as it is read in, by specifying: ("{ chomp => 1 }") Better still, you can tell "slurp" to automagically "chomp" the input and replace what it chomps with another string, by specifying: ("{ chomp => "another string" }") You can also tell "slurp" to compute the replacement string on-the-fly by specifying a subroutine as the "chomp" value: ("{ chomp => sub{...} }"). This subroutine is passed the string being chomped off, so for example you could squeeze single newlines to a single space and multiple conseqcutive newlines to a two newlines with: sub squeeze { my ($removed) = @_; if ($removed =~ tr/ / / == 1) { return " " } else { return " "; } } print slurp(*DATA, {irs=>qr/[ ]* +/, chomp=>&squeeze}), " "; Which would transform: This is the first paragraph This is the second paragraph This, the third This one is the very last to: This is the first paragraph This is the second paragraph This, the third This one is the very last Autochomping works in both scalar and list contexts. In scalar contexts every instance of the input record separator will be removed (or replaced) within the returned string. In list context, each list item returned with its terminating separator removed (or replaced). You can specify I/O layers, either using the Perl 5 notation: slurp "<:layer1 :layer2 :etc", $filename; or as an array of options: slurp $filename, [layer1=>1, layer2=>1, etc=>1]; slurp [layer1=>1, layer2=>1, etc=>1], $filename; or as individual options (each of which must be in a separate hash): slurp $filename, {layer1=>1}, {layer2=>1}, {etc=>1}; slurp {layer1=>1}, {layer2=>1}, {etc=>1}, $filename; (...which, of course, would look much cooler in Perl 6: # Perl 6 only :-( slurp $filename, :layer1 :layer2 :etc; slurp :layer1 :layer2 :etc, $filename; ) A common mistake is to put all the options together in one hash: slurp $filename, {layer1=>1, layer2=>1, etc=>1}; This is almost always a disaster, since the order of I/O layers is usually critical, and placing them all in one hash effectively randomizes that order. Use an array instead: slurp $filename, [layer1=>1, layer2=>1, etc=>1]; WARNING
The syntax and semantics of Perl 6 is still being finalized and consequently is at any time subject to change. That means the same caveat applies to this module. DEPENDENCIES
Requires: Perl 5.8.0, Perl6::Export AUTHOR
Damian Conway (damian@conway.org) COPYRIGHT
Copyright (c) 2003-2012, Damian Conway. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. perl v5.14.2 2012-06-14 Perl6::Slurp(3pm)
All times are GMT -4. The time now is 02:49 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy