Apply condition on fixed width file and filter records


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Apply condition on fixed width file and filter records
# 1  
Old 06-27-2011
Apply condition on fixed width file and filter records

Dear members..

I have a fixed width file. Requirement is as below:-
1. Scan each record from this fixed width file
2. Check for value under field no "6" equals to "ABC". If yes, then filter this record into the output file

Please suggest a unix command to achieve this, my guess awk might work here

Thanks in advance
Suresh
# 2  
Old 06-27-2011
Please post some example lines using code tags.
# 3  
Old 06-27-2011
What about something like?

Code:
$ cat sample2.txt
AB DFDF GFGF BB DDFDFDDG ABC 1234
BB WKSK 12SD AA SHJDSDDS DEF 1345
GH SDSD 7878 99 DHFDFDFG ABC 3434

$ awk '{if (substr($0,26,3)=="ABC") print}' sample2.txt
AB DFDF GFGF BB DDFDFDDG ABC 1234
GH SDSD 7878 99 DHFDFDFG ABC 3434

# 4  
Old 06-27-2011
Apply condition on fixed width file and filter records

Dear members..

I am further refining my requirement as below:-

I have a fixed width file. Requirement is as below:-
1. Scan each record from this fixed width file
2. Check for value under field no "6" equals to "6". If yes, then filter this record into the output file

Please suggest a unix command to achieve this, my guess awk might work here

Illustration below:-
----------------

Suppose source file has 3 records as below

123456789 => As per requirement, this record should flow into output
123459789 => This records should NOT go to ouput
123455789 => This records should NOT go to ouput
658986215 => As per requirement, this record should flow into output
# 5  
Old 06-27-2011
Code:
% cat > testfile
123456789 => As per requirement, this record should flow into output
123459789 => This records should NOT go to ouput
123455789 => This records should NOT go to ouput
658986215 => As per requirement, this record should flow into output

% cat testfile | awk '{print $1}' | awk -vFS= '$6 == 6'


Last edited by yazu; 06-27-2011 at 10:57 AM..
# 6  
Old 06-27-2011
Code:
grep "^.....6" filename > output.txt

# 7  
Old 06-27-2011
Apply condition on fixed width file and filter records

Thanks guys for quick solutions..it works now Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Filter duplicate records from csv file with condition on one column

I have csv file with 30, 40 columns Pasting just three column for problem description I want to filter record if column 1 matches CN or DN then, check for values in column 2 if column contain 1235, 1235 then in column 3 values must be sequence of 2345, 2345 and if column 2 contains 6789, 6789... (5 Replies)
Discussion started by: as7951
5 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

Alter Fixed Width File

Thank u so much .Its working fine as expected. ---------- Post updated at 03:41 PM ---------- Previous update was at 01:46 PM ---------- I need one more help. I have another file(fixed length) that will get negative value (ex:-00000000003000) in postion (98 - 112) then i have to... (6 Replies)
Discussion started by: vinus
6 Replies

4. UNIX for Dummies Questions & Answers

Length of a fixed width file

I have a fixed width file of length 53. when is try to get the lengh of the record of that file i get 2 different answers. awk '{print length;exit}' <File_name> The above code gives me length 50. wc -L <File_name> The above code gives me length 53. Please clarify on... (2 Replies)
Discussion started by: Amrutha24
2 Replies

5. Shell Programming and Scripting

Comparing two fixed width file

Hi Guys I am checking the treads to get the answer but i am not able to get the answer for my question. I have two files. First file is a pattern file and the second file is the file i want to search in it. Output will be the lines from file2. File1: P2797f12af 44751228... (10 Replies)
Discussion started by: anshul_er
10 Replies

6. Shell Programming and Scripting

Manupulating Records in a fixed width file

I am trying to determine what would be a fast and simple way to manipulate data that comes in a fixed width format. This data has 6 segments within a record. Each record needs to written out with a header and the 6 segments. Based on the value in column #6 the fields will be defined accordingly.... (4 Replies)
Discussion started by: Muga801
4 Replies

7. Shell Programming and Scripting

Fixed-Width file from Oracle

Hi All, I have created a script which generates FIXED-WIDTH file by executing Oracle query. SELECT RPAD(NVL(col1,CHR(9)),20)||NVL(col2,CHR(9))||NVL(col3,CHR(9) FROM XYZ It generates the data file with proper alignment. But if same file i transfer to windows server or Mainframe... (5 Replies)
Discussion started by: Amit.Sagpariya
5 Replies

8. UNIX Desktop Questions & Answers

Help with Fixed width File Parsing

I am trying to parse a Fixed width file with data as below. I am trying to assign column values from each record to variables. When I parse the data, the spaces in all coumns are dropped. I would like to retain the spaces as part of the dat stored in the variables. Any help is appreciated. I... (4 Replies)
Discussion started by: sate911
4 Replies

9. Shell Programming and Scripting

Extracting records with unique fields from a fixed width txt file

Greetings, I would like to extract records from a fixed width text file that have unique field elements. Data is structured like this: John A Smith NY Mary C Jones WA Adam J Clark PA Mary Jones WA Fieldname / start-end position Firstname 1-10... (8 Replies)
Discussion started by: sitney
8 Replies

10. UNIX for Dummies Questions & Answers

Fixed Width file using AWK

I am using the following command at the Unix prompt to make my 'infile' into a fixed width file of 100 characters. awk '{printf "%-100s\n",$0}' infile > outfile However, there are some records with a special character "" These records are using 3 characters in place of one and my record... (2 Replies)
Discussion started by: alok.benjwal
2 Replies
Login or Register to Ask a Question