awk issue splitting a fixed-width file containing line feed in data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk issue splitting a fixed-width file containing line feed in data
# 8  
Old 02-17-2017
Quote:
Originally Posted by Scrutinizer
Try something like this to reconstruct the lines before processing:
Code:
awk -v search_col_pos=$search_col_pos -v search_str_len=$search_str_len -v segment_type="$segment_type" '
  function cond_print(record) { 
    if (substr(record, search_col_pos, search_str_len) ~ segment_type) print record
  }
  !/^[0-9]{25}/ {
    prev=prev RS $0
    next
  }
  {
    if(NR>1) cond_print(prev);
    prev=$0
  } 
  END {
    cond_print(prev)
  }
' file

Hi Scrutinizer.

Thank you for the code suggestion. I tried your code above with the input_file.txt attached but I didn't get any data in the output file.

This is what I executed:

Code:
awk -v search_col_pos=33 -v search_str_len=2 -v segment_type="UF" '
  function cond_print(record) { 
    if (substr(record, search_col_pos, search_str_len) ~ segment_type) print record
  }
  !/^[0-9]{25}/ {
    prev=prev RS $0
    next
  }
  {
    if(NR>1) cond_print(prev);
    prev=$0
  } 
  END {
    cond_print(prev)
  }
' input_file.txt > output_file.txt

Can you please help?
# 9  
Old 02-17-2017
Hi, your previous sample contains "UF" at position 29, not at 33 . And this sample does not contain "UF" at all.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Answers to Frequently Asked Questions

Fixed width file issue and resolutions

I have a fixed width file with no separators , but the file has JUNK characters in it and i know how to remove it. but in few cases these junk characters having created a mess by splitting single row into 2 or 3 rows. I need to put them back to a single line like rest of the rows. This fixed... (2 Replies)
Discussion started by: santoshkm
2 Replies

2. Shell Programming and Scripting

Replace using awk on fixed width file.

All, I used to use following command to replace specific location in a fixed width file. Recently looks like my command stopped working as intended. We are on AIX unix. awk 'function repl(s,f,t,v) { return substr(s,1,f-1) sprintf("%-*s", t-f+1, v) substr(s,t+1) } NR<=10 {... (3 Replies)
Discussion started by: pinnacle
3 Replies

3. Post Here to Contact Site Administrators and Moderators

How to sum up data in fixed width file with decimal point?

HI Everyone, I have below source file AAA|NAME1|ADDRESS1|300.20 BBB|NAME2|ADDRESS2|400.31 CCC|NAME3|ADDRESS3|300.34 I have requirement where I need to sum up fourth field in above fixed width pipe delimited flat file. When I use below code, it gives me value 1001.00 But I am expecting... (1 Reply)
Discussion started by: patricjemmy6
1 Replies

4. Shell Programming and Scripting

Print column details from fixed width file using awk command

hi, i have a fixed width file with multiple columns and need to print data using awk command. i use: awk -F "|" '($5 == BH) {print $1,$2,$3}' <non_AIM target>.txt for a delimiter file. but now i have a fixed width file like below: 7518 8269511BH 20141224951050N8262 11148 8269511BH... (5 Replies)
Discussion started by: kcdg859
5 Replies

5. Shell Programming and Scripting

Remove new line character and add space to convert into fixed width file

I have a file with different record length. The file as to be converted into fixed length by appending spaces at the end of record. The length should be calculated based on the record with maximum length in the file. If the length is less than the max length, the spaces should be appended... (4 Replies)
Discussion started by: Amrutha24
4 Replies

6. Shell Programming and Scripting

Ignore Header and Footer and Sort the data in fixed width file

Hi Experts, I want to Sort the data in fixed width file where i have Header and Footer also in file. I m using below commad to do the sort based on field satarting from 15 position to 17 position , but it is not ignoring the Header and Footer of the file while sorting. In the output i am... (5 Replies)
Discussion started by: sasikari
5 Replies

7. Shell Programming and Scripting

awk: creating a fixed-width single file from 2 different files

I have to create a single file from three files, Please see below for samples: day.txt 20090101 20090102 item.txt 123456789101 12345678910209 1234567891 str.txt 1 12 123 output.txt 20090101123456789101 1 0 2009010112345678910209 12 ... (2 Replies)
Discussion started by: tamahomekarasu
2 Replies

8. Shell Programming and Scripting

Appending string (charachters inside the line) to a fixed width file using awk or sed

Source File: abcdefghijklmnop01qrstuvwxyz abcdefghijklmnop02qrstuvwxyz abcdefghijklmnop03qrstuvwxyz abcdefghijklmnop04qrstuvwxyz abcdefghijklmnop05qrstuvwxyz Whatever characters are in 17-18 on each line of the file, it should be concatenated to the same line at the character number... (6 Replies)
Discussion started by: tamahomekarasu
6 Replies

9. Shell Programming and Scripting

edit entire column from a fixed-width file using awk or sed

Col1 Col2 Col3 Col4 12 Completed 08 0830 12 In Progress 09 0829 11 For F U 07 0828 Considering the file above, how could i replace the third column the most efficient way? The actual file size is almost 1G. I am... (10 Replies)
Discussion started by: tamahomekarasu
10 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