Remove \n at the beginning of a field in a record.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove \n at the beginning of a field in a record.
# 1  
Old 01-20-2012
Remove \n at the beginning of a field in a record.

Hi,

In my file, I have few records which are split across multiple lines.

Code:
File 1:
=====
james,\n
pre-auth completed,in patient,\n
Fac_Id:23451,ramson,Dallas
 
Expected is:
==========
james,pre-auth completed,in patient,Fac_Id:23451,ramson,Dallas

# 2  
Old 01-20-2012
Assuming a double new-line is the actual record separator something like the following might work for you...
Code:
skrynesaver@busybox ~/tmp$ cat tmp.dat
field 1 in record 1
field 2 in record 1
field 3 in record 1
field 4 in record 1
field 5 in record 1

field 1 in record 2
field 2 in record 2
field 3 in record 2
field 4 in record 2
field 5 in record 2
skrynesaver@busybox ~/tmp$ perl -e '$/="\n\n";while(<>){@record=split(/\n/,$_);$record=join",",@record;print "$record\n";}' tmp.dat
field 1 in record 1,field 2 in record 1,field 3 in record 1,field 4 in record 1,field 5 in record 1
field 1 in record 2,field 2 in record 2,field 3 in record 2,field 4 in record 2,field 5 in record 2
skrynesaver@busybox ~/tmp$


Last edited by Skrynesaver; 01-20-2012 at 05:44 AM..
# 3  
Old 01-20-2012
Under the same assumption, the awk version:
Code:
awk '$1=$1' OFS= RS= file


Last edited by Scrutinizer; 01-20-2012 at 07:18 AM..
# 4  
Old 01-20-2012
Try:
Code:
perl -00 -lpe 's/\n//' FILE

# 5  
Old 01-21-2012
in my file I have proper records too.
Code:
Proper records:
1,abc,def,ghi
Improper records:
2,jk
l,mno,pqr
3,stu,vw
x,yz

I am trying to make the above as
Code:
1,abc,def,ghi
2,jkl,mno,pqr
3,stu,vwx,yz

# 6  
Old 01-21-2012
Try:
Code:
awk -F, -vOFS="," 'NF<4{f=f""$0;n=split(f,a,",");if (n==4){print f;f=""}}NF==4' file

This User Gave Thanks to bartus11 For This Post:
# 7  
Old 01-21-2012
Bartus11' Thanks it worked for my file... but this will not work if we have , inbetween a field. Say:
1,"a,bc",def,ghi ?
Anyways, Thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display combination of 4 field uniqe record and along with concatenate 5th and 6th field.

Table ACN|NAME|CITY|CTY|NO1|NO2 115|AKKK|ASH|IND|10|15 115|AKKK|ASH|IND|20|20 115|AKKK|ASH|IND|30|35 115|AKKK|ASH|IND|30|35 112|ABC|FL|USA|15|15 112|ABC|FL|USA|25|20 112|ABC|FL|USA|25|45 i have written shell script using cut command and awk programming getting error correct it and add... (5 Replies)
Discussion started by: udhal
5 Replies

2. Shell Programming and Scripting

Replacing entire fields with specific text at end or beginning of field

Greetings. I've got a csv file with data along these lines: Spumoni's Pizza Place, Placemats n Things, Just Lamps Counterfeit Dollars by Vinnie, Just Shades, Dollar StoreI want to replace the entire comma-delimited field if it matches something ending in "Place" or beginning with "Dollar",... (2 Replies)
Discussion started by: palmfrond
2 Replies

3. Shell Programming and Scripting

remove all occurrences of a character at the beginning of a string

Hi there, i need some help to remove all occurrences of a certain character at the beginning of a string. Example: my string is 00102030 and i want to remove all zeros from beginning of string so the result is 102030 (3 Replies)
Discussion started by: gigagigosu
3 Replies

4. Shell Programming and Scripting

Remove filenames beginning with multiple dots

hi all, I want to remove filenames beginning with multiple dots.how I can do this. Thanks in advance (5 Replies)
Discussion started by: sriharsharavi
5 Replies

5. Shell Programming and Scripting

Remove comma and next rows beginning from the end

Hello friends, I have a file which consists of many rows, I use a couple of commands to convert it so i can use in a database query for filtering. I need the first columns (msisdns) in a row, seperated with commas, 9855162267,4,5,2010-11-03 17:02:07.627 9594567938f,5,5,2010-11-02... (9 Replies)
Discussion started by: EAGL€
9 Replies

6. Shell Programming and Scripting

Remove spaces in the beginning of a string

Hi, I am trying to remove spaces from the beginning of a string (which i am using in my shell script). For ex - my string looks like this - " no rows selected" and i want the string to look like this - "no rows selected" How can i achieve this? Thanks! (18 Replies)
Discussion started by: shrutihardas
18 Replies

7. Shell Programming and Scripting

Remove repeating pattern from beginning of file names.

I want a shell script that will traverse a file system starting at specific path. And look at all file names for repeating sequences of and remove them from the file name. The portion of the name that gets removed has to be a repeating sequence of the same characters. So the script would... (3 Replies)
Discussion started by: z399y
3 Replies

8. UNIX for Dummies Questions & Answers

Remove words beginning with a certain character from a file

Hi, how could you go about removing words that begin with a certain character. assuming that this character is '-' I currently have echo "-hello" | sed s/-/""/ which replaces the leading dash with nothing but I want to remove the whole word, even if there are multiple words beginning... (3 Replies)
Discussion started by: skinnygav
3 Replies

9. Shell Programming and Scripting

delete beginning field.

I want to script out deleting the first field of a file when it is created. I'm looking to store the output of an ls -l commad to a file but leaving off the permissions so when the file is opened I see something like; 2 bin bin 256 Feb 6 2005 mnt 5 root system 256... (2 Replies)
Discussion started by: daveisme
2 Replies

10. Shell Programming and Scripting

Remove white space at the beginning of lines

Hi! I store some data obtained with grep or awk in a file. The problem is that some lines have white space at the begining : line1 line2 line3 I use something like grep WORD INFILE >> OUTFILE awk >> OUTFILE I would love if it were possible to remove the white whitout parsing the... (4 Replies)
Discussion started by: tipi
4 Replies
Login or Register to Ask a Question