Sponsored Content
Top Forums Shell Programming and Scripting Search and swap multiple lines in file using Perl Post 302689553 by alister on Tuesday 21st of August 2012 03:53:58 PM
Old 08-21-2012
Quote:
Originally Posted by Corona688
Sort based on the first and third characters of the fifth column:
Code:
sort -r -k 5.1,5.1 -k 5.3,5.3 < input > output

That sort does not behave as intended. The -k5.1,5.1 restricted key never has any effect. While it restricts that key to the first character of the fifth field, since leading blanks aren't ignored by default, when using the default delimiter, 1-index is always going to be a whitespace character.

Code:
$ cat data
2 2 2 2 a 2
1 1 1 1 b 1

# 5.1 for both lines is a space, whole-line lexicographical comparison breaks tie
$ sort -k 5.1,5.1 data
1 1 1 1 b 1
2 2 2 2 a 2

# -b ignores leading blanks so that 5.1 yields 'a' and 'b', correct result.
$ sort -b -k 5.1,5.1 data 
2 2 2 2 a 2
1 1 1 1 b 1

Note that if the b modifier is used in a key definition, the modifier only applies to the component to which it is attached, and the -b option (if present) is ignored. Therefore it's possible that even within the same key definition, identical indices may refer to different positions. Given the three character field, ab (space character followed by two letters):
5.1b,5.1b ==> a (1 character)
5.1,5.1b ==> a (2 characters: space, a)
5.1b,5.1 ==> (0 characters: null result since the second component points to a character preceding the starting component)

Regards,
Alister

EDIT: The thread moved on while I was preparing this post. Perhaps it's no longer relevant to the solution, but the information may still be of use to someone. Smilie

Last edited by alister; 08-21-2012 at 10:20 PM..
These 2 Users Gave Thanks to alister For This Post:
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl - How to search a text file with multiple patterns?

Good day, great gurus, I'm new to Perl, and programming in general. I'm trying to retrieve a column of data from my text file which spans a non-specific number of lines. So I did a regexp that will pick out the columns. However,my pattern would vary. I tried using a foreach loop unsuccessfully.... (2 Replies)
Discussion started by: Sp3ck
2 Replies

2. Shell Programming and Scripting

Pattern search in multiple lines

Hi, I have to search those statements from the file which starts from "shanky"(only shanky, shanky09 or 09shanky is not allowed) and ends with ");". These two string can be in a same line or different line. And also i have to negate those lines which starts with #. Can any one please give me... (2 Replies)
Discussion started by: shanky09
2 Replies

3. Shell Programming and Scripting

Search for multiple lines in large file

Hi, I have a requirement to search for a string in a large log file along with few lines before and after the the string. The following script was sufficient to search such an entry. STRING_TO_GREP="$1" FILE_TO_GREP="$2" NUMBER_OF_LINES_BEFORE=$3 NUMBER_OF_LINES_AFTER=$4 for i in `grep... (3 Replies)
Discussion started by: praveen123
3 Replies

4. Shell Programming and Scripting

Using Perl to Merge Multiple Lines in a File

I've hunted and hunted but nothing seems to apply to what I need. Any help will be much appreciated! My input file looks like (Unix): marker,allele1,allele2 RS1002244,1,1 RS1002244,1,3 RS1002244,3,3 RS1003719,2,2 RS1003719,2,4 RS1003719,4,4 Most markers are listed 3 times but a few... (2 Replies)
Discussion started by: Peggy White
2 Replies

5. Shell Programming and Scripting

Perl to Search through next lines

I have log file that I need to extract time difference occurance when two events happend, between first occurance of TV and when W_NO happend. also read the value=, below example...I can only read the next line but not able to seach all the next lines untill i see W_NO.. Thanks for your help.... (6 Replies)
Discussion started by: bataf
6 Replies

6. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

7. Shell Programming and Scripting

Combine multiple unique lines from event log text file into one line, use PERL or AWK?

I can't decide if I should use AWK or PERL after pouring over these forums for hours today I decided I'd post something and see if I couldn't get some advice. I've got a text file full of hundreds of events in this format: Record Number : 1 Records in Seq : ... (3 Replies)
Discussion started by: Mayday22
3 Replies

8. Shell Programming and Scripting

How to search multiple patterns and remove lines from a file?

Hi, I have a file content as below. Table : PAYR Displayed fields: 15 of 15 Fixed columns: 4 List width 0999... (4 Replies)
Discussion started by: shirdi
4 Replies

9. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies
All times are GMT -4. The time now is 09:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy