Split by Position

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Split by Position
# 8  
Old 06-18-2018
The "file" represents the input file; replace by your file name.
We have a boolean expression here, evaluating the char at pos 75, the result is 0 for FALSE and 1 for TRUE, which in turn is used as the index into the Output File Name array. Please note that not everyone adores this construct.
Header and trailer ONLY to the reject file?
# 9  
Old 06-18-2018
@ RudiC

I want to run the AWK command through a script by passing input file name as a parameter to the script as Each run will have a different input file.

The header and trailer should be in both files.

How can I fit the above requirements in to a script ?

Thanks in Advance
# 10  
Old 06-18-2018
With all of the help you've gotten from us answering questions very similar to this in the last four years, why don't you try to modify the code RudiC suggested to meet your added requirements on your own?

If you can't get it to work, show us what you have tried, show us the output you are getting from your code, and explain what still isn't working.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 11  
Old 06-19-2018
@Don

Here is the working script I have , can you please improvise it and make it more efficient

Code:
#!/usr/bin/ksh

# Get header Record

grep '^0' Source.txt |head -1 > Output.txt

# Get Trailer Record

grep '^Z' Source.txt |tail -1 > Temp.txt

# Get the matching records

cat Source.txt | awk '{{DCL=substr($0,75,2)} {print DCL,$0}}' > Tempfile1.txt

cat Tempfile1.txt | awk '{{DCLM=substr($0,0,1)} if (DCLM ~/^[D]/){print $0}}' > Tempfile2.txt

cat Tempfile2.txt | cut -c 4- >> Output.txt

# append trailer record

cat Temp.txt >> Output.txt

# 12  
Old 06-19-2018
I'm not sure I can follow. And it doesn't seem you want / need my awk proposal. Please answer Don Cragun's questions to their entirety.
# 13  
Old 06-19-2018
It may be a small win, but try to use the file reading of the tools rather than cat | some-command, so you would have:-
Code:
:
:
awk '{{DCL=substr($0,75,2)} {print DCL,$0}}' Source.txt > Tempfile1.txt

awk '{{DCLM=substr($0,0,1)} if (DCLM ~/^[D]/){print $0}}' Tempfile1.txt > Tempfile2.txt

cut -c 4- Tempfile2.txt >> Output.txt
:
:

However you can improve this further by combining the commands of the Tempfilei.txt are not needed afterwards. Badly, you could:-
Code:
:
:
awk '{{DCL=substr($0,75,2)} {print DCL,$0}}' Source.txt     | \
  awk '{{DCLM=substr($0,0,1)} if (DCLM ~/^[D]/){print $0}}' | \
  cut -c 4- >> Output.txt
:
:

This will save the disk IO, but obviously at a memory cost. Perhaps you can find a way to combine this into a single awk statement. Have a go, show us your attempts and we can work on a more elegant and efficient solution.



Kind regards,
Robin
# 14  
Old 06-19-2018
Quote:
Originally Posted by techedipro
@Don

Here is the working script I have , can you please improvise it and make it more efficient

Code:
#!/usr/bin/ksh

# Get header Record

grep '^0' Source.txt |head -1 > Output.txt

# Get Trailer Record

grep '^Z' Source.txt |tail -1 > Temp.txt

# Get the matching records

cat Source.txt | awk '{{DCL=substr($0,75,2)} {print DCL,$0}}' > Tempfile1.txt

cat Tempfile1.txt | awk '{{DCLM=substr($0,0,1)} if (DCLM ~/^[D]/){print $0}}' > Tempfile2.txt

cat Tempfile2.txt | cut -c 4- >> Output.txt

# append trailer record

cat Temp.txt >> Output.txt

rbatte1 has already given you code that makes your code above more efficient. But, despite you saying that your script is working, it does not:
  • take a parameter naming your input file,
  • does not take input filenames in the format specified in post #1,
  • does not produce output filenames in the format specified in post #1, and
  • does not produce output in any of your output files adhering to the requirements you specified in post #1 nor to the requirements you specified in post #7.

Please reconsider what I said in post #10, clearly explain your new requirements (if what you requested in post #1 as modified by post #7 is not what you are now trying to do), and then answer all of the questions I raised there.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting value at a particular position without changing the position of other characters

Hi All, I wanted a sed/awk command to add a value/character on a particular position without disturbing the position of other characters. I have file a.txt OL 10031 Day Black Midi Good Value P01 P07 OL 10031 Day Black Short Good Value P01 P07 I want to get the output as... (2 Replies)
Discussion started by: rahulsk
2 Replies

2. Shell Programming and Scripting

Split string by position

Hello, I have a file, where the strings in the lines are with fixed length. Like this 1234ABXX234ABC123456 And I want to split that line as it is colored. The output to be like this: 1234A;BX;X234;ABC1234;56 I am trying now with substring, but I believe there is a better way. Can you... (7 Replies)
Discussion started by: apenkov
7 Replies

3. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

4. Shell Programming and Scripting

Need command or script to print all lines from 2nd position to last but one position

hi guys, i want command or script to display the content of file from 2nd position to last but one position of a file abcdefghdasdasdsd 123,345,678,345,323 434,656,656,656,656 678,878,878,989,545 4565656667,65656 i want to display the same above file without first and... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

5. Shell Programming and Scripting

Split file based on distinct value at specific position

OS : Linux 2.6x Shell : Korn In a single file , how can I identify all the Uniqe values at a specific character position and length of each record , and simultaneously SPLIT the records of the file based on each of these values and write them in seperate files . Lets say : a) I want to... (4 Replies)
Discussion started by: kumarjt
4 Replies

6. Shell Programming and Scripting

Remove text from n position to n position sed/awk

I want to remove text from nth position to nth position couple of times in same line my line is "hello is there anyone can help me with this question" I need like this ello is there anyone can help me with question 'h' is removed and 'this' removed from the line. I want to do this... (5 Replies)
Discussion started by: elamurugu
5 Replies

7. Shell Programming and Scripting

Moving first position in a file to the last position

hi, I have a file which consists of some records: 2010_06_4010093_001_001|10|ABCDEFGH|9|4010093||0040400||31.12.2009|S|O|X||||20100602093851-31.12.2009|XXBBFC|EFG||||00001| 2010_06_4010162_001_001|11|ABCDEFGH|9|4010162||0040400||31.12.2009|S|O|X||||20100602093851-31.12.2009|XXBBFC|EFG||||00002|... (11 Replies)
Discussion started by: pparthiv
11 Replies

8. UNIX for Dummies Questions & Answers

find if a position is between a given start and end position

Hi, I am a newbie in unix programming so maybe this is a simple question. I would like to know how can I make a script that outputs only the values that are not between any given start and end positions Example file1: 2 30 40 80 82 100 file2: ID1 1 ID2 35 ID3 80 ID4 81 ID6... (9 Replies)
Discussion started by: fadista
9 Replies

9. Shell Programming and Scripting

how to find a position and print some string in the next and same position

I need a script for... how to find a position of column data and print some string in the next line and same position position should find based on *HEADER8* in text for ex: ord123 abs 123 987HEADER89 test234 ord124 abc 124 987HEADER88 test235 ... (1 Reply)
Discussion started by: naveenkcl
1 Replies

10. UNIX for Dummies Questions & Answers

How to split a value according to character position

Hello all, I have a script which picks up a series of large numbers, each of which are actually amalgamations of a series of other numbers. Unfortunately there are no separator characters so I can't use awk -F. I am looking for a way of splitting them into variables according to their... (4 Replies)
Discussion started by: michaeltravisuk
4 Replies
Login or Register to Ask a Question