How to Remove the new line character inbetween a record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Remove the new line character inbetween a record
# 1  
Old 01-02-2012
How to Remove the new line character inbetween a record

I have a file, in which a single record spans across multiple lines,
Code:
File 1
====
14|\n 
leave request \n
accepted|Yes|
15|\n 
leave request not \n
acccepted|No|

I wanted to remove the '\n charecters. I used the below code (foudn somewhere in this forum)

Code:
perl -e 'while (<>) { if (! /\|$/ ) { chomp; } print ;}' input.dat > output.dat

The above code returns the ouptut as
line 1: 14|\n
line 2: leave request accepted|Yes| (end of record)

line3: 15|\n
line 4: leave request not acccepted|No| (end of record)

still the new line characters at the begining of the field still remains and a single record is split as two records.

My expected output is
line 1: 14|leave request accepted|Yes| (end of record)

line3: 15|leave request not acccepted|No| (end of record)

Can some one please help me in this issue?Smilie

Last edited by vbe; 01-02-2012 at 10:23 AM.. Reason: typos, missing code endtag...
# 2  
Old 01-02-2012
Hi machomaddy,

That 'perl' code seems to work for me, here you have other one (perl too). It supposes that each three input lines there should be an output line:
Code:
$ cat infile
14| 
leave request 
accepted|Yes|
15| 
leave request not 
acccepted|No|
$ perl -ne 's/(?<=\|)\s*$/\n/; if ( $. % 3 ) { chomp } print' infile
14|leave request accepted|Yes|
15|leave request not acccepted|No|

Regards,
Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove duplicated records and update last line record counts

Hi Gurus, I need to remove duplicate line in file and update TRAILER (last line) record count. the file is comma delimited, field 2 is key to identify duplicated record. I can use below command to remove duplicated. but don't know how to replace last line 2nd field to new count. awk -F","... (11 Replies)
Discussion started by: green_k
11 Replies

2. Shell Programming and Scripting

Adding end of line character in the last record

Need to add end of line character to last record in a fixed width file. When i take the record length of each line, for all the records it gives example like 200 except the last line as 199. Due to this my other script fails. Please help me on this. (4 Replies)
Discussion started by: Amrutha24
4 Replies

3. UNIX for Dummies Questions & Answers

Remove last character in each line

Hi guys, Does anyone know how to remove the last character in each of the line? This is what I have: ABCDE.1 GLSJD.2 HIJPL.2 HKAGB.3 IUBWQ.1 What I want (remove the dot and number): ABCDE GLSJD HIJPL HKAGB IUBWQ I tried to use this: sed 's/.*//' But I'm not sure if that is... (3 Replies)
Discussion started by: narachaid
3 Replies

4. UNIX for Dummies Questions & Answers

How to remove // inbetween the files contents?

Hi Am Using unix Aix Have a file name called FILE1 having the content of FILE1 is cat FILE1 01/11/2012 03/11/2012 // // // 04/11/2012 I Need Output as:- cat FILE1 01/11/2012 03/11/2012 04/11/2012 (7 Replies)
Discussion started by: Venkatesh1
7 Replies

5. Shell Programming and Scripting

How to remove , if first character on line

Hi, I have a file with lines such as the below. I want to remove the comma only if it is the first character on a line. I can't work out how to do this using sed. *ELSET, ELSET=WHEEL_TD2 63, 64, 65, 72, 82, 88, 89, 92, 120, 121, 152, 181, 190, 221, 252, 259 , 260, 282, 283, 285, 286,... (2 Replies)
Discussion started by: carlr
2 Replies

6. Shell Programming and Scripting

Remove newline character or join the broken record

Hi, I have a very huge file, around 1GB of data. I want to remove the newline characters in the file but not preceded by the original end delimiter {} sample data will look like this 1234567 abcd{} 1234sssss as67 abcd{} 12dsad3dad 4sdad567 abcdsadd{} this should look like this... (6 Replies)
Discussion started by: ratheeshjulk
6 Replies

7. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

8. UNIX for Advanced & Expert Users

To remove new line character

Hi, I am facing one interesting problem : I have a file which contains data like this 459,|1998-11-047|a |b |c \n efg | d|e | \n 459,|1998-11-047|a \n c|b |c \n efg | d|e | \n Basically what I have to do is , I have to remove all \n which is coming ( enclosed ) in between... (7 Replies)
Discussion started by: shihabvk
7 Replies

9. Shell Programming and Scripting

Finding a character in first line of a record

HI, I am pretty new to Unix scripting. I will need help in Finding a character in first line of a file or a set of files. The scenario is as follows: Lets consider a set of files which is having a character "ID"(without quotes) in the first line of each file.I need to find this character... (14 Replies)
Discussion started by: bsandeep_80
14 Replies

10. Shell Programming and Scripting

Remove Last Character of Line

Hi, I need to put the single line contents of a file into a variable, but remove the last character, for example the file would have this sort of contents: 2;4;3;10;67;54;96; And I want the variable to be: 2;4;3;10;67;54;96 (notice the last ";" has gone). Unfortunately I can't just... (4 Replies)
Discussion started by: danhodges99
4 Replies
Login or Register to Ask a Question