Remove Carriage returns between strings in a field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove Carriage returns between strings in a field
# 1  
Old 08-24-2005
Remove Carriage returns between strings in a field

Is there any way to remove carriage retuns between the records?

We have input records separated by TABS and have carriage returns as below:

123 456 789 ABC "1952.00" 678 "abcdef
ghik
lmno"

Above we have 7 fields, notice that the 7th field in the records has carriage returns and make it into records...

I need to remove the carriage returns and make it look like below:

123 456 789 ABC "1952.00" 678 "abcdef ghik lmno"

Let me know your thoughts?

Thanks,
AC
# 2  
Old 08-24-2005
Use Awk:

Code:
{ record = record $0
  # If number of quotes is odd, continue reading record.
  if ( gsub( /"/, "&", record ) % 2 )
  { record = record " "
    next
  }
}
{ print record
  record = ""
}

# 3  
Old 08-24-2005
Code:
 > cat file
123 456 789 ABC "1952.00" 678 "abcdef
ghik
lmno"
 > cat file | tr  \\n " " ; echo
123 456 789 ABC "1952.00" 678 "abcdef ghik lmno"

A less fancy way is:
Code:
for i in `cat file`
   do echo -n "$i "
done
echo

In Solaris replace echo with /usr/ucb/echo
# 4  
Old 08-24-2005
Quote:
cat file | tr \\n " "
Why use cat when cat is a do-nothing program here? Why not
Code:
tr  \\n " " <file

What if the file contains more than one record?

Record1 "1952.00" 678 "abcdef
ghik
lmno"
Record2 "1952.00" 678 "abcdef
ghik
lmno"

Wouldn't the output be

Record1 "1952.00" 678 "abcdef ghik lmno" Record2 "1952.00" 678 "abcdef ghik mno"
# 5  
Old 08-24-2005
Good points.
# 6  
Old 08-25-2005
Thanks a lot for all your inputs. The below code soles the problem with the example I had given. When the data had double quotes...

{ record = record $0
# If number of quotes is odd, continue reading record.
if ( gsub( /"/, "&", record ) % 2 )
{ record = record " "
next
}
}
{ print record
record = ""
}

The tr \\n " " removes all the new lines and forms a one big record. I don't want this to happen.

Thanks again everybody.

AC
This User Gave Thanks to acheepi For This Post:
# 7  
Old 08-25-2005
Thanks!!

Quote:
Originally Posted by futurelet
Use Awk:

Code:
{ record = record $0
  # If number of quotes is odd, continue reading record.
  if ( gsub( /"/, "&", record ) % 2 )
  { record = record " "
    next
  }
}
{ print record
  record = ""
}

One more thing.. Could you tell me what it does excatly....
especially if ( gsub( /"/, "&", record ) % 2 )

Thanks in advance.

AC
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove carriage returns from awk output

I'm on Linux version 2.6.32-696.3.1.el6.x86_64, using the Ksh shell. I'm working with the input file: John Daggett, 341 King Road, Plymouth MA Alice Ford, 22 East Broadway, Richmond VA Orville Thomas, 11345 Oak Bridge Road, Tulsa OK Terry Kalkas, 402 Lans Road, Beaver Falls PA Eric Adams,... (2 Replies)
Discussion started by: prooney
2 Replies

2. Shell Programming and Scripting

awk to remove field and match strings to add text

In file1 field $18 is removed.... column header is "Otherinfo", then each line in file1 is used to search file2 for a match. When a match is found the last four strings in file2 are copied to file1. Maybe: cut -f1-17 file1 and then match each line to file2 file1 Chr Start End ... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

TR not removing carriage returns

I have a CSV with carriage returns in place of newlines. I am trying to use tr to remove them, but it isn't working. Academic year,Term,Course name,Period,Last name,Nickname 2012-2013,First Semester,English 12,4th Period,Arnold,Adam 2012-2013,First Semester,English 12,4th Period,Adams,Jim... (1 Reply)
Discussion started by: nextyoyoma
1 Replies

4. Shell Programming and Scripting

Awk to remove carriage return from 65th field

Hi, I have a pipe delimited file. There are around 700 columns in the file. The 65th column has carriage return which is causing read issue with our ETL process. I would like to replace the new line characters in 65th field with "nothing" i have return the following code and need help to... (7 Replies)
Discussion started by: pinnacle
7 Replies

5. Shell Programming and Scripting

Removal of carriage returns from a comma delimited file

Hi, I have a file which is having some carriage return in one of the field for which single line is coming in multiple lines. I want to combine all those multiple lines of that field into one line. Eg: Input: Id, Name, Location, Comments, Dept 2, John, US, I am from US. I... (5 Replies)
Discussion started by: mahish20
5 Replies

6. Shell Programming and Scripting

removing carriage returns in text file

Hi I have a text file that looks like this: A B C D E F G H I I want it to be reformatted to A;B;C; D;E;F; G;H;I; (4 Replies)
Discussion started by: coolnfunky
4 Replies

7. Shell Programming and Scripting

Replacing Carriage returns without loosing EOL

Hello, I have read a few threads on this subject and tried a few things out, but still come up short. There was one good example, then the last reply was something to the effect of 'Use Sed' & 'Read a book'... Well I read a bunch of online tutorials on sed, awk, tr, but still can't get the... (2 Replies)
Discussion started by: Majiktom
2 Replies

8. Shell Programming and Scripting

removing thousand of carriage returns using sed

I need to replace thousands of carriage returns/line breaks in a large xml file and with spaces. I hope to do so with a script, called, for example, "removeCRs." I would invoke this at the command line as ml5003$ sed -f /Users/ml5003/removeCRs oldFile > newFile The script, I presume, would... (4 Replies)
Discussion started by: ml5003
4 Replies

9. Shell Programming and Scripting

Removing carriage returns with sed

How do we delete all carriage returns after a particular string using sed inside a K Shell? e.g. I have a text file named file1 below: $ more file1 Group#=1 User=A Role=a1 Group#=2 User=B Role=a1 Role=b1 Group#=3 User=C Role=b1 I want the carriage returns to be delete on the... (12 Replies)
Discussion started by: stevefox
12 Replies

10. Shell Programming and Scripting

spaces and carriage returns in 'here documents'

As the title suggests, i am having some trouble figuring out how to pass spaces and carriage returns to a 'here document' ie #!/bin/bash /usr/local/install_script.sh <<SCRIPT yes no <pass carriage retun here> yes no <pass a space and then a carriage return here> exit SCRIPT any... (0 Replies)
Discussion started by: hcclnoodles
0 Replies
Login or Register to Ask a Question