Delete new line characters from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete new line characters from a file
# 1  
Old 04-12-2010
Delete new line characters from a file

Hi,

I have a file with about 25 colums separated with '~', but few of the lines have extra tabs ('^') and new line characters ('$'). Is there a way I can delete those characters if they are anywhere before the 25th column in a line?

example:

Code:
CLUB000650;12345678;0087788667;NOOP MEMBER                   ;Sam          ;Sam         ;31 bridge Rd.$
ABCD00001111;PQRS20393343;0088518295;NORTH AMERICAN DIRECTORY SERV.;TATE;HELLO;123 MAIN STREET;;;LOVELAND;CO;80538;Tate  HELLO                ;11 STREET^I       ;                              ;                              ;LOVELAND                      ;CO;80538         ;N TON 00477      ;                                                  ;BLK 9K #C90A FHP MEDIA  ;S1741721;2005-06-08 00:00:00;135;1;119.52;0$

Thanks

Last edited by Franklin52; 04-12-2010 at 01:40 PM.. Reason: Please use code tags!!
# 2  
Old 04-12-2010
Quote:
Originally Posted by rudoraj
...
I have a file with about 25 colums separated with '~',
...
example:

Code:
CLUB000650;12345678;0087788667;NOOP MEMBER                   ;Sam          ;Sam         ;31 bridge Rd.$
ABCD00001111;PQRS20393343;0088518295;NORTH AMERICAN DIRECTORY SERV.;TATE;HELLO;123 MAIN STREET;;;LOVELAND;CO;80538;Tate  HELLO                ;11 STREET^I       ;                              ;                              ;LOVELAND                      ;CO;80538         ;N TON 00477      ;                                                  ;BLK 9K #C90A FHP MEDIA  ;S1741721;2005-06-08 00:00:00;135;1;119.52;0$

Well, I don't even a single "~" character in your file !

Quote:
...
but few of the lines have extra tabs ('^') ...
The "^" is a "caret". It is not the same as a tab character.

tyler_durden
# 3  
Old 04-12-2010
as far as i understood you want to remove spaces or tabs from the beginning or end of each field, right? if so try this:

Code:
awk -F\; '{for(i=1;i<=NF;i++) gsub("^[ \t]*|[ \t]*$","",$i)}1' OFS=\; infile

ps: i havent seen tabs in your inputfile, only spaces, thats why i put spaces in gsub part.
# 4  
Old 04-12-2010
sorry, my delimeter is ";" , not sure what I was thinking while typing that earlier.

Thanks
# 5  
Old 04-12-2010
use double backslashes to get rid of $ at the end and ^
Code:
awk -F\; '{for(i=1;i<=NF;i++) gsub("^[ \t]*|[ \t]*$|\\^|\\$$","",$i)}1' OFS=\; infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to delete text between two characters in line?

Hi, I have a large text file with the following format: >gi|347545744|gb|JN204951.1| Dismorphia spio voucher 5 ATCAAATTCCTTCCTCTCCTTAAA >gi|17544664774|gb|WN204922.32| Rodapara nigens gene region CCGGGCAAATTCCTTCCTCTCCTTAAA >gi|555466400|gb|SG255122.8| Bombyx mandariana genbank 3... (1 Reply)
Discussion started by: euspilapteryx
1 Replies

2. Shell Programming and Scripting

How delete characters of specific line with sed?

Hi, I have a text file with some lines like this: /MEDIA/DISK1/23568742.MOV /MEDIA/DISK1/87456321.AVI /MEDIA/DISK2/PART1/45753131.AVI /IMPORT/44452.WAV ... I want to remove the last 12 characters in each line that it ends "AVI". Should look like this: /MEDIA/DISK1/23568742.MOV... (12 Replies)
Discussion started by: inaki
12 Replies

3. Shell Programming and Scripting

delete first 2 characters for each line, please help

hi, ./R1_970330_210505.sard ./R1_970403_223412.sard ./R1_970626_115235.sard ./R1_970626_214344.sard ./R1_970716_234214.sard ... ... ... for these strings, i wanna remove the ./ for each line how can i do that? i know it could possibly be done by sed, but i really have not idea how... (4 Replies)
Discussion started by: sunnydanniel
4 Replies

4. Shell Programming and Scripting

need to Delete first 10 characters of a file name

Hello Everyone, I need help in deleting first 10 characters from the filename in a directory eg: 1234567890samplefile1.txt 1234567890samplefile2.txt and so on.. need to get the output as samplefile1.txt Thanks in Advance!!!! (8 Replies)
Discussion started by: Olivia
8 Replies

5. Shell Programming and Scripting

Delete characters from each line

Hi, I have a file that has data in the following manner, tt_0.00001.dat 123.000 tt_0.00002.dat 124.000 tt_0.00002.dat 125.000 This is consistent for all the entries in the file. I want to delete the 'tt_' and '.dat' from each line. Could anyone please guide me how to do this using awk or... (2 Replies)
Discussion started by: lost.identity
2 Replies

6. Shell Programming and Scripting

delete duplicated characters in each line

I'm a biologist trying to analyse some data and I'll appreciate some help with the following problem. I have a column of characters which I'll like to delete the duplicated characters in each line and report only the unique one.No sorting should be done. E.g. The original data: GTG CTC CTC... (5 Replies)
Discussion started by: ivpz
5 Replies

7. Shell Programming and Scripting

How to delete characters using a file

Hi All, I have a configuration file (file.cfg) in which data will be like this ; , _ + a to z A to Z Now i have to read a textfile (file.txt) and i need to check whether there is any other character present in text file that is not existing in (file.cfg). If other characters are present... (4 Replies)
Discussion started by: krishna_gnv
4 Replies

8. UNIX for Dummies Questions & Answers

how to delete M-^M characters from a file

I am receiving a file with 'M-^M' characters...how do I get rid of these characters. I tried tr -d '\015' and sed '/^M//g', but they didnot work. Appreciate if someone can help me with this (1 Reply)
Discussion started by: hyennah
1 Replies

9. UNIX for Dummies Questions & Answers

delete non printable characters from file

i have a file which contains non printable characters like enter,escape etc i want to delete them from the file (2 Replies)
Discussion started by: alokjyotibal
2 Replies

10. UNIX for Dummies Questions & Answers

How to delete a file with special characters

I don't now exactly how I did it, but I created a file named " -C " cexdi:/home1 $ls -lt total 1801336 -rw------- 1 cexdi ced-group 922275840 23 mars 10:03 -C How do I delete this file ? cexdi:/home1 $rm -C rm: invalid option -- C Syntax : rm filename ... Doesn't work...... (5 Replies)
Discussion started by: yveslagace
5 Replies
Login or Register to Ask a Question