Split string by position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split string by position
# 1  
Old 02-10-2017
Split string by position

Hello,
I have a file, where the strings in the lines are with fixed length. Like this
Code:
1234ABXX234ABC123456

And I want to split that line as it is colored. The output to be like this:
Code:
1234A;BX;X234;ABC1234;56

I am trying now with substring, but I believe there is a better way.
Can you help please!
Thanks in advance!
# 2  
Old 02-10-2017
If people knew what the positions for the ; were, and how/where they are retrieved... not by the colours, I guess. Any preferred tools?
# 3  
Old 02-10-2017
OK, Sorry. Here are the length of the fileds
Code:
str1=5
str2=2
str3=4
str4=7
str5=2

# 4  
Old 02-10-2017
Are those in a file? A variable? on paper, your mind ... just kidding.
# 5  
Old 02-10-2017
Okay, so you can use variable substitution to slice up the variables in the current shell, i.e. you don't need to call cut etc.

If you know the field lengths are fixed, then perhaps something like this will work:-
Code:
c=0
while read record
do
   ((c=$c+1))                             # Increment record counter
   remainder="${record#?????}"            # 5x? to cut off first five characters
   p1="${record%${remainder}}"            # Get the first part of the record dropping the remainder
   record="${remainder}"

   remainder="${record#??}"               # 2x? to cut off first two characters
   p2="${record%${remainder}}"            # Get the next part of the record dropping the remainder
   record="${remainder}"

   remainder="${record#????}"             # 4x? to cut off first four characters
   p3="${record%${remainder}}"            # Get the next part of the record dropping the remainder
   record="${remainder}"

   remainder="${record#???????}"          # 7x? to cut off first seven characters
   p4="${record%${remainder}}"            # Get the next part of the record dropping the remainder
   record="${remainder}"

   remainder="${record#??}"               # 2x? to try to cut off first two characters
   if [ "${remainder" = "${record}" ]
   then
      p5="${record}"                      # Use the whole of the remaining record if there are no other characters
   else
      printf "Error line %d\n" "${c}" >&2 # Write to standard error
   fi
   
   printf "%s;%s;%s;%s;%s\n" "$p1" "$p2" "$p3" "$p4" "$p5"
done < in_file > out_file  2> err_file

Does that sort of structure help? There may be a neater way in awk to read and split the record and I'd be happy to learn.



Robin
This User Gave Thanks to rbatte1 For This Post:
# 6  
Old 02-10-2017
Given the substring lengths are found in a file structured like above, and an empty field separator splits a line into single characters,try
Code:
awk '
NR == FNR       {P[MX = NR] = $2
                 next
                }
                {POS = 0
                 for (i=1; i<MX; i++)   {POS+=P[i]; $POS = $POS ";"
                                        }
                }
1
' FS="=" file2 FS="" OFS="" file1
1234A;BX;X234;ABC1234;56

If the lengths are in a variable, a slight adaption will yield the same result.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 02-10-2017
Hi,
At the end I did it with substr
Code:
while read string
do
str1=${string:0:5}
str2=${string:5:2}
str3=${string:7:4}
str4=${string:11:7}
str5=${string:18:2}
done < input.txt

But the 2 examples above are very usefull and helpfull.
Thanks for the help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Split by Position

I am on AIX. I need to use AWK to split the source file based on a character at a certain position. Position 75 with a value of 'R' should go in one output file and the rest should go in another file. I need proper names for the output files. Source FileName : abc_xyz_pqr_a_1_yymmdd... (15 Replies)
Discussion started by: techedipro
15 Replies

2. 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

3. 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

4. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

7. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

8. Shell Programming and Scripting

Search for a string and replace the searched string in the same position

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last... (15 Replies)
Discussion started by: ganesh_248
15 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