Inserting newline in front of multi-character string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting newline in front of multi-character string
# 1  
Old 04-19-2011
Question Inserting newline in front of multi-character string

I'm working with a large file with multiple records, each record begins with ISA[^a-zA-Z0-9]. The issue is, sometimes ISA[^a-zA-Z0-9] is at the start of the line, sometimes it's in the middle of the line. So before I can csplit my main file into multiple records, I have to get each record header onto its own line.

I've been having trouble using sed or tr to do this. Do I have to capture each occurence of ISA[^a-zA-Z0-9] into a variable and then use the variable as part of the regex in a sed statement?

I'm open to all suggestions on how to accomplish this. This is my first time posting, so I apologize if I'm not asking in the forum style or detail.

Thanks!

Note: By ISA[^a-zA-Z0-9] , I'm meaning ISA(+ any non-alphanumeric character) ... in case there's a better way.
# 2  
Old 04-19-2011
Try:
Code:
perl -p0e 's/ISA[\W_]/\n$&/g' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 04-19-2011
bartus11 ... that worked beautifully, thank you so much ... now I'm rethinking my whole script!

Is there a \W equivalent for non-printable or non-ascii characters?
# 4  
Old 04-19-2011
As far as I know \W matches all non-alphanumeric characters, including non-ASCII and non-printable ones.
This User Gave Thanks to bartus11 For This Post:
# 5  
Old 04-19-2011
Thanks bartus11, this is an example of a record ...
Code:
ISA~00~ ~00~ ~ZZ~TPM ~ZZ~TPU~110301~0037~U~00200~000002452~0~P~<GS~FA~TPY~TPM~110301~0037~2470~X~004010ST~997~0932AK1~SQ~110456465AK2~866~0001AK5~AAK2~866~0002AK5~AAK9~A~2~2~2SE~8~0932GE~1~2470IEA~1~000002452

The main file has many of these records. The ~ in this case can be any non-alphanumeric value and that's fine, they can stay. In this case  (Hex 1C) needs to be replaced with newline. Throughout the file, the IC can be any non-ascii, non-printable character.

My original thought was to put the value at position 63 into a variable and replace it with a newline with sed but from your code example, I can see there are much better and concise ways to handle substitutions.

I haven't had a chance to experiment with perl and [^[:ascii:]] or [^[Smilierint:]] ... will either work? Is there an entirely different way?

Thanks again for taking time

Last edited by Franklin52; 04-20-2011 at 03:08 AM.. Reason: Please use code tags
# 6  
Old 04-19-2011
So you want to replace all the "hex 1C" characters with newline in your file? Or any non-printable character?
This User Gave Thanks to bartus11 For This Post:
# 7  
Old 04-19-2011
any non-printable character with a newline... IC was just an example
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

2. Shell Programming and Scripting

Bash - Inserting non printable character(s) in string variable

Hello. I have a string variable named L_TEMP to test a very simple filter. L_TEMP="50AwL.|KWp9jk" I want to insert a non printable character between K and W. I have try this : linux-g65k:~ # a='50AwL.|K' linux-g65k:~ # b='Wp9jk' linux-g65k:~ # L_TEMP="$a$'\x07'$b" linux-g65k:~ # echo... (6 Replies)
Discussion started by: jcdole
6 Replies

3. Shell Programming and Scripting

Remove last newline character..

Hi all.. I have a text file which looks like below: abcd efgh ijkl (blank space) I need to remove only the last (blank space) from the file. When I try wc -l the file name,the number of lines coming is 3 only, however blank space is there in the file. I have tried options like... (14 Replies)
Discussion started by: Sathya83aa
14 Replies

4. Shell Programming and Scripting

[Solved] SED - Bash - Inserting multi Tab character in the command

Hello. I am using : sed -i -e '/§name_script§/a#'"${MY_TAB11}"'# \ #'"${MY_TAB1}"'The Standard way'"${MY_TAB7}"'# \ #'"${MY_TAB1}"'==============='"${MY_TAB7}"'# \ ' "$CUR_FILE" Is there a better way to define "MY_TAB7","MY_TAB11" in other way than : MY_TAB1=$'\t' MY_TAB2=${MY_TAB1}$'\t'... (2 Replies)
Discussion started by: jcdole
2 Replies

5. UNIX for Dummies Questions & Answers

newline character in a variable

variable="unix\nlinux" echo $variable expected output: unix linux :wall: can i do that ?? thanks in advance!! (3 Replies)
Discussion started by: sathish92
3 Replies

6. UNIX for Dummies Questions & Answers

newline character

hi, I want to print the below lines "Message from bac logistics The Confirmation File has not been received." When i give like this in the code "Message from bac logistics\n The Confirmation File has not been received." It is giving only Message from bac logistics\n The... (9 Replies)
Discussion started by: trichyselva
9 Replies

7. Shell Programming and Scripting

Concatenate the string with the newline character

Hi , i want to Concatenate a string and use the following code str="i" str="$str am \n" str="$str a \n" str="$str boy \n" echo $str I want to ouput this i am a boy However it outputs i am \n a \n boy \n (3 Replies)
Discussion started by: youareapkman
3 Replies

8. UNIX for Advanced & Expert Users

newline character, space and tab after a string

no problem (6 Replies)
Discussion started by: angelina
6 Replies

9. Shell Programming and Scripting

adding a character in front of a line

Hi everyon, I am trying to search for a pattern in a file and add "//" to the begining of the file. lets say i want to comment out a line from my codes. the line that should be commented out contains the word "reset". i need to search for the word "reset" and comment out that specific line. is... (5 Replies)
Discussion started by: ROOZ
5 Replies

10. Shell Programming and Scripting

inserting a character between string

i have a file contains like this: i want to create a script that will insert a comma "." after the 10th character so it would be look like this thanks in advance (5 Replies)
Discussion started by: dakid
5 Replies
Login or Register to Ask a Question