Removal of last-semicolons in line with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removal of last-semicolons in line with sed
# 1  
Old 10-13-2011
Removal of last-semicolons in line with sed

Hello,

I'm trying to remove an arbitrary number of semicolons at the end of each line in the input file.

Input:
Code:
 44;I;1000031;;;B;0137;0;;01.02.2008;03.02.2009;;;;;;;;;;;;;0028-101746;;;
 45;I;1000031;;;B;0137;0;;01.02.2008;03.02.2009;;;;;;;;;;;;;0028-101746;;;;;
 18026;I;1000031;;;B;0137;0;;03.12.2008;31.12.2999;;;;;;;;;;;;;842010;;;

Output:
Code:
 44;I;1000031;;;B;0137;0;;01.02.2008;03.02.2009;;;;;;;;;;;;;0028-101746
 45;I;1000031;;;B;0137;0;;01.02.2008;03.02.2009;;;;;;;;;;;;;0028-101746
 18026;I;1000031;;;B;0137;0;;03.12.2008;31.12.2999;;;;;;;;;;;;;842010

The closest I have come to a solution is this:
Code:
 sed 's/[ ;]*$//'

but doesn't work.

Please share some light - Thank you!
# 2  
Old 10-13-2011
Why you're saying that it doesn't work? What's the output?
Do you want to remove the trailing semicolons only or you need to remove the trailing white spaces too?
# 3  
Old 10-13-2011
Your sed command itself is working good .. Any error you got??
Code:
$ sed 's/[ ;]*$//' infile
44;I;1000031;;;B;0137;0;;01.02.2008;03.02.2009;;;;;;;;;;;;;0028-101746
 45;I;1000031;;;B;0137;0;;01.02.2008;03.02.2009;;;;;;;;;;;;;0028-101746
 18026;I;1000031;;;B;0137;0;;03.12.2008;31.12.2999;;;;;;;;;;;;;842010

# 4  
Old 10-13-2011
I don't want to remove trailing spaces, only the last semicolons ";" in the line.
it's not working for me; the output file is the same as the input file (no changes). Can it be because of the end of file character? I'm using GNU sed 4.1.2

Code:
[umbrella@wasp csv]$ cat aaa
asd asd asd asd asdas;das dasdasD;SAdasDAS;D;;;;;;
asdasdasd;ASDAS;DAS;D;ASdas;DAS;D;AS;;;;;;
asdasdasdas das d asd asd as d asd as

[umbrella@wasp csv]$ sed 's/[ \;]$//' aaa

asd asd asd asd asdas;das dasdasD;SAdasDAS;D;;;;;
asdasdasd;ASDAS;DAS;D;ASdas;DAS;D;AS;;;;;
asdasdasdas das d asd asd as d asd as

# 5  
Old 10-13-2011
Code:
# cat aaa
asd asd asd asd asdas;das dasdasD;SAdasDAS;D;;;;;;
asdasdasd;ASDAS;DAS;D;ASdas;DAS;D;AS;;;;;;
asdasdasdas das d asd asd as d asd as

Code:
# sed 's/[;]*$//g' aaa
asd asd asd asd asdas;das dasdasD;SAdasDAS;D
asdasdasd;ASDAS;DAS;D;ASdas;DAS;D;AS
asdasdasdas das d asd asd as d asd as

# 6  
Old 10-13-2011
You lost the '*'.

Also, if you're not removing whitespace you don't need it in the regexp:
Code:
# sed 's/;*$//' /tmp/aaa
asd asd asd asd asdas;das dasdasD;SAdasDAS;D
asdasdasd;ASDAS;DAS;D;ASdas;DAS;D;AS
asdasdasdas das d asd asd as d asd as

(GNU sed 4.1.5 on bash)
# 7  
Old 10-13-2011
I think I am getting close, thank you for the help!

When I vim the files where sed works I have regular end-of-line characters. When I open the original file I have ^M at the end of each line; that's the problem I suppose. Can I extend the sed command to also consider this ^M as an end of line?

---------- Post updated at 06:57 AM ---------- Previous update was at 06:47 AM ----------

thank you guys, dos2unix fixes the issue. I run it on the initial file, then sed works properly!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Section Removal With sed; and With a Twist . . .

Hello folks! Raised a bump on my head trying to figure this one out ;) I have an xml file which needs to be edited, removing an entire property section in the work. Here's what the target section layout looks like: <property name="something"> {any number of lines go here} </property>... (7 Replies)
Discussion started by: LinQ
7 Replies

2. Shell Programming and Scripting

Honey, I broke awk! (duplicate line removal in 30M line 3.7GB csv file)

I have a script that builds a database ~30 million lines, ~3.7 GB .cvs file. After multiple optimzations It takes about 62 min to bring in and parse all the files and used to take 10 min to remove duplicates until I was requested to add another column. I am using the highly optimized awk code: awk... (34 Replies)
Discussion started by: Michael Stora
34 Replies

3. Shell Programming and Scripting

Duplicate line removal matching some columns only

I'm looking to remove duplicate rows from a CSV file with a twist. The first row is a header. There are 31 columns. I want to remove duplicates when the first 29 rows are identical ignoring row 30 and 31 BUT the duplicate that is kept should have the shortest total character length in rows 30... (6 Replies)
Discussion started by: Michael Stora
6 Replies

4. UNIX for Dummies Questions & Answers

2 semicolons

Just wondering what 2 semicolons together after a command means (2 Replies)
Discussion started by: millsy5
2 Replies

5. Shell Programming and Scripting

sed help with character removal

Hello I've got a string of text with a number in pence, e.g. 0.52p, I need to remove the 'p' so that it just reads 0.52 without of course removing all the other 'p' characters. Many thanks (1 Reply)
Discussion started by: mrpugster
1 Replies

6. Shell Programming and Scripting

any savant ? using AWK/SED to remove newline character between two strings : conditional removal

I'd like to remove (do a pattern or precise replacement - this I can handle in SED using Regex ) ---AFTER THE 1ST Occurrence ( i.e. on the 2nd occurrence - from the 2nd to fourth occurance ) of a specific string : type 1 -- After the 1st occurrence of 1 string1 till the 1st occurrence of... (4 Replies)
Discussion started by: sieger007
4 Replies

7. Shell Programming and Scripting

Adding semicolons

Lets say I wanted to add a ; before the last 6 characters of my variable how would I do this? (2 Replies)
Discussion started by: puttster
2 Replies

8. Shell Programming and Scripting

Removal of new line character in double quotes

Hi, Could you please help me in removal of newline chracter present in between the double quotes and replacing it with space. For example ... Every field is wrapped with double quotes with comma delimiter, so I need to travese from first double quote occerence to till second double... (7 Replies)
Discussion started by: vsairam
7 Replies

9. Shell Programming and Scripting

Column Search and Line Removal

Hello Gurus, I need to remove lines within a file if it contains specific criteria. Here is what I am trying to resolve: Users of AppRuntime: (Total of 10 licenses issued; Total of 6 licenses in use) buih02 dsktp501 AppGui 1 (compute_lic/27006 3122), start Mon 2/22 7:58 dingj1... (3 Replies)
Discussion started by: leepet01
3 Replies

10. Shell Programming and Scripting

Special Character SED/AWK removal

I have a script that produces an output containing '/.ssh'. I am trying to find a way of parsing only this data from a single line, without removing any other special characters contained within the output as a result of the parse. Any help would be appreciated (6 Replies)
Discussion started by: Raggedranger333
6 Replies
Login or Register to Ask a Question