How to remove repetitive lines in a file with sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove repetitive lines in a file with sed?
# 1  
Old 08-25-2018
How to remove repetitive lines in a file with sed?

Hello,
My goal is the make all x times repeated lines into a single line.
I need to attain the expected output with sed -i , I need to overwrite the MyFile
MyFile:
Code:
Hello World
Welcome
Hello World
Welcome Back
This is my test

Expected output:
Code:
Hello World
Welcome
Welcome Back
This is my test

I found below code in another forum while searching but it did not solve the issue, even with sed -n:
Code:
sed -n '
:start
/^\[/{
    h
  :loop
    n
    /^\[/b start
    /^$/b loop
    x;p;g
}
p' MyFile

It would be nice if you would help me out on getting rid of this issue.
Many thanks
Boris

Last edited by baris35; 08-26-2018 at 07:56 AM.. Reason: upon more specific description request
# 2  
Old 08-25-2018
Code:
perl -i -ne 'print unless ${$_}++' MyFile

This User Gave Thanks to Aia For This Post:
# 3  
Old 08-25-2018
Hello Aia,
Much appreciated, thank you..


Kind regards
Boris
# 4  
Old 08-26-2018
Please make the request more specific: do you want to delete the
- third line
- first duplicate line
- any duplicate lines
- second "Hello World" line
?
# 5  
Old 08-26-2018
Dear Rudic,
Thank you for your attention.
Aia's post helped me with this.
My file was consisting of 29000 similar lines and now it has only 55

Code:
perl -i -ne 'print unless ${$_}++' Filename


Many thanks
Boris

PS: I have just updated the main thread upon your notification

Last edited by baris35; 08-26-2018 at 07:57 AM.. Reason: main thread updated
# 6  
Old 08-26-2018
You wanted sed? Try this; it works on your sample file, but I'm unsure how it would deal with above mentioned large file:
Code:
sed -rn '1h; 1!H; s/$/./; G; s/^(([^.]*)\.\n(.*\2.*))\2$/\3/; s/^[^.]*\.\n//; s/\n\n/\n/; h;  $p' file
Hello World
Welcome
Welcome Back
This is my test

# 7  
Old 08-26-2018
Dear Rudic,
My main aim was to overwrite with sed -i , for that reason I kindly asked if that was possible to execute with sed command but Perl is also okay.
Regarding your sed command, seems like my file is not compatible with your scenario. The terminal is waiting for printing the output but no action.


Kind regards
Boris

------ Post updated at 02:47 PM ------

Dear Rudic,
Even though I said "sorted out" but I see that sometimes newly created files are not accepting sed or perl command for this issue and it asks me to convert newly created files to unix with dos2unix command. Pc is running under ubuntu 18.04 bionic
How may I get rid of this? Maybe this could be a new thread topic.


Thanks in advance
Boris

Last edited by baris35; 08-26-2018 at 04:54 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed to remove all lines in file that are not .vcf.gz extention

I am trying to use sed to remove all lines in a file that are nor vcf.gz. The sed below runs but returns all the files with vcf.gz in them, rather then just the ones that end in only that extention. Thank you :). file ... (9 Replies)
Discussion started by: cmccabe
9 Replies

2. Shell Programming and Scripting

I need help to removing repetitive lines

Hello SuperUsers! First i wanna say my english sucks.. Don't hate me for that. :rolleyes: I need to make a Bash for removing smilar lines from an output file. My output file always same. Line 1 & 2 Stays. And others similar to this lines needs to be delete. </UsageData><?xml version="1.0"... (4 Replies)
Discussion started by: morphin
4 Replies

3. Shell Programming and Scripting

Using sed in a loop/to remove lines contained in variable from file

I've tried numerous commands, but I am not sure how to use sed in a loop. This is what I have: VARZ contains CARD_FILE_LIST and it also contains CARD_FILE_LIST2 so echo "$VARZ" CARD_FILE_LIST CARD_FILE_LIST2 I have a file with 60 lines in /tmp/testfile it and I want those lines deleted... (3 Replies)
Discussion started by: newbie2010
3 Replies

4. Shell Programming and Scripting

How to remove certain lines using sed?

Hi, I am new to unix and i started some scripting recently. Please go through the following script i wrote. #!/bin/sh file='path../tfile' file1='path../tfile1' rmfile='path../test2' C1=1 C2=1 exec 3< $file1 while read LINE1; do read LINE2 <&3 a=$LINE1 b=`expr $LINE2 - 1` ... (1 Reply)
Discussion started by: Subbu123
1 Replies

5. UNIX for Dummies Questions & Answers

How to remove certain lines using sed?

Hi I have the following kind of line sin my file . print ' this is first'. print ' this is firs and next ' ' line continuous '. -- this is entire print line. print ' this is first and next ' ' line continuous and' 'still there now over'. -- this 3lines together a single print line. ... (5 Replies)
Discussion started by: Sivajee
5 Replies

6. Shell Programming and Scripting

Remove a range of lines from a file using sed

Hi I am having some issue editing a file in sed. What I want to do is, in a loop pass a variable to a sed command. Sed should then search a file for a line that matches that variable, then remove all lines below until it reaches a line starting with a constant. I have managed to write a... (14 Replies)
Discussion started by: Andy82
14 Replies

7. Shell Programming and Scripting

Print lines between two repetitive patterns

Hi users I have one file which has number of occurrence of one pattern examples Adjustmenttype,11 xyz 10 dwe 9 abd 13 def 14 Adjustmenttype,11 xyz 24 dwe 34 abd 35 def 11 nmb 12 Adjustmenttype, not eleven .... ... ... (2 Replies)
Discussion started by: eranmoh
2 Replies

8. Shell Programming and Scripting

grep/sed to remove lines in file

Hi, I have a file with values, file1: BELL-1180-1180-81|577:1017| BELL-1180-1180-81|jm10i-auto-stub1/577:102| BELL-1180-1180-81|jm10i-auto-stub1/577:101| BELL-1180-1180-81|jm10i-auto-stub1/577:1700| BELL-1180-1180-81|jm10i-auto-stub1/577:1699| I need to remove the lines which has... (9 Replies)
Discussion started by: giri_luck
9 Replies

9. Shell Programming and Scripting

using sed to remove lines

Can somebody explain why my sed command is not working. I do the folloinwg: Generates a binary file to /tmp/x1.out /usr/lib/sa/sa2 -s 4:00 -e 8:00 -i 3600 -A -o /tmp/x1.out decodes the file (no problem so far) sar -f /tmp/x1.out When I do this it does not appear to delete the... (4 Replies)
Discussion started by: BeefStu
4 Replies

10. Shell Programming and Scripting

Unix Remove repetitive alphabets

Hi, I am trying to write a script that will take 2 or more instances of repetitive alphabets (ZZ) to be removed from a field. This should only happen from beginning and end of a field. For Example : Input File a) ZZZIBM Corporation b) ZZZIBM Corporation ZZZZZ b) IBM ZZZ... (26 Replies)
Discussion started by: msalam65
26 Replies
Login or Register to Ask a Question