Filter lines based on values at specific positions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filter lines based on values at specific positions
# 1  
Old 03-17-2016
Filter lines based on values at specific positions

hi.

I have a Fixed Length text file as input where the character positions 4-5(two character positions starting from 4th position) indicates the LOB indicator. The file structure is something like below:
Code:
10126Apple DrinkOmaha      
10231Milkshake   New Jersey
103   Billabong    Illinois

The requirement is to capture all the record(here the first and second) that contains strings like "26", "31", "24" and "78" as LOB Indicators in the 4th and 5th position(there can be more valid LOB indicator values coming up in the near future) of each record.

I know the process of awk, but want to know if this could be done using grep/sed.

Regards,
Kumarjit.
# 2  
Old 03-17-2016
Quote:
Originally Posted by kumarjt
The requirement is to capture all the record(here the first and second) that contains strings like "26", "31", "24" and "78" as LOB Indicators in the 4th and 5th position(there can be more valid LOB indicator values coming up in the near future) of each record.
Capture and do what with it?

Code:
sed -n 's/...\([0-9][0-9]\).*/\1/p' file

# 3  
Old 03-17-2016
they will serve as input to another downstream process.

In your solution, you've just checked the value of two consecutive column positions starting from position 4 against any numeric values between 0 and 9, but the requirement is completely different, as the data in these two positions will have discrete values like "26", "31", "24" or "78".

How to achieve this without using AWK?


Regards.
# 4  
Old 03-17-2016
Try:
Code:
grep -E '^...(26|31|24|78)' file




--
On Solaris use /usr/xpg4/bin/grep
# 5  
Old 03-17-2016
Code:
cut -c 4,5 <your_file.txt

# 6  
Old 03-21-2016
Thanks for all the feedback.

@Scrutinizer: What if the starting position of the LOB indicator field is 29th character position instead of 4th? Should I put 29 dots in my grep expression??


Warm Regards.
Kumarjit.
# 7  
Old 03-21-2016
28 you mean? If it starts at the 29th position, you can try this:
Code:
grep -E '^.{28}(26|31|24|78)' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and replace specific positions of specific lines

Hi, I have a file with hundreds of lines. I want to search for particular lines starting with 4000, search and replace the 137-139 position characters; which will be '000', with '036'. Can all of this be done without opening a temp file and then moving that temp file to the original file name. ... (7 Replies)
Discussion started by: dsid
7 Replies

2. Shell Programming and Scripting

UNIX command -Filter rows in fixed width file based on column values

Hi All, I am trying to select the rows in a fixed width file based on values in the columns. I want to select only the rows if column position 3-4 has the value AB I am using cut command to get the column values. Is it possible to check if cut -c3-4 = AB is true then select only that... (2 Replies)
Discussion started by: ashok.k
2 Replies

3. Shell Programming and Scripting

Extract specific lines based on another file

I have a folder containing text files. I need to extract specific lines from the files of this folder based on another file input.txt. How can I do this with awk/sed? file1 ARG 81.9 8 81.9 0 LEU 27.1 9 27.1 0 PHE .0 10 .0 0 ASP 59.8 11 59.8 0 ASN 27.6 12 27.6 0 ALA .0 13 .0 0... (5 Replies)
Discussion started by: alanmathew84
5 Replies

4. Shell Programming and Scripting

awk to filter out lines containing unique values in a specified column

Hi, I have multiple files that each contain four columns of strings: File1: Code: 123 abc gfh 273 456 ddff jfh 837 789 ghi u4u 395 File2: Code: 123 abc dd fu 456 def 457 nd 891 384 djh 783 I want to compare the strings in Column 1 of File 1 with each other file and Print in... (3 Replies)
Discussion started by: owwow14
3 Replies

5. Linux

Filter a .CSV file based on the 5th column values

I have a .CSV file with the below format: "column 1","column 2","column 3","column 4","column 5","column 6","column 7","column 8","column 9","column 10 "12310","42324564756","a simple string with a , comma","string with or, without commas","string 1","USD","12","70%","08/01/2013",""... (2 Replies)
Discussion started by: dhruuv369
2 Replies

6. Shell Programming and Scripting

Count specific characters at specific column positions

Hi all, I need help. I have an input text file (input.txt) like this: 21 GTGCAACACCGTCTTGAGAGG 50 21 GACCGAGACAGAATGAAAATC 73 21 CGGGTCTGTAGTAGCAAACGC 108 21 CGAAAAATGAACCCCTTTATC 220 21 CGTGATCCTGTTGAAGGGTCG 259 Now I need to count A/T/G/C numbers at each character location in column... (2 Replies)
Discussion started by: thienxho
2 Replies

7. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

8. Shell Programming and Scripting

PERL - Selecting specific files based on 'date stamp' values

Hi, I've list of files in a directory, which have date stamp value in their names. ex: abc_data_20071102.csv, abc_data_20091221.csv, abc_data_20100110.csv, abc_data_20100222.csv, abc_data_20080620.csv,... etc., I need to select and process only files, within the given date... (4 Replies)
Discussion started by: ganapati
4 Replies

9. Shell Programming and Scripting

Filter out lines of a cvs from values from an other file

Hi there, I have a comma seperated file which looks like 16-Jun-08,KLM forwarder,,AMS,DXB,AMS,C,Y,G10,074-02580900,milestone failed - message not received,C,OK,,13/06/2008 00:00,KL427,13/06/2008 00:00,KL427,Rebooked,C,milestone failed - message not received,milestone failed - evented... (3 Replies)
Discussion started by: sickboy
3 Replies

10. Shell Programming and Scripting

need help--script to filter specific lines from multiple txt files

Hi folks, - I have 800 txt files - those files are cisco router configs router1.txt router2.txt ... router800.txt I want to accomplish the following: - I want to have a seperate file with all the filenames that I want to process - I want a script that goes trough all those... (7 Replies)
Discussion started by: I-1
7 Replies
Login or Register to Ask a Question