0

I have a text file and there was a mistake when it was created. To fix this I need to delete a line with a specific unique string and delete the characters in the following line that precede the @ symbol. I was able to do this with sed and cut but it only output that one line, not the many other 1000s of lines in my file. Here is an example of the part of the file that needs fixing. I know the line #s (delete 45603341 and modify 45603342) where this mistake occurs.

@HWI-1KL135:70:C305EACXX:5:2105:6727:102841 1:N:0:CAGATC
CCAAGTGTCACCTCTTTTATTTATTGATTT@HWI-1KL135:70:C305EACXX:5:1101:1178:2203 1:N:0:CAGATC

I need the output to look like this and for it to leave the rest of the file intact.

@HWI-1KL135:70:C305EACXX:5:1101:1178:2203 1:N:0:CAGATC

Thanks!

Jotne
  • 40,548
  • 12
  • 51
  • 55
  • You mind find sed's ability to join two lines into one (http://stackoverflow.com/questions/9605232/merge-two-lines-into-one) would help here. Get rid of the newline, and then chop out the part that you don't want, up to the '@' character. – OnlineCop May 28 '14 at 19:58
  • Can you show the sed/cut code that you're using? – mopsled May 28 '14 at 20:13

4 Answers4

1

How about:

sed -i -e '45603341d;45603342s/^.*\(@.*\)$/\1/' <filename>

where you replace <filename> with the name of your file.

holgero
  • 2,640
  • 1
  • 13
  • 17
0

If you want to change a particular line and delete the above line then run,

sed -ri '45603342s/^([^@]*)(@.*)$/\2/g; 45603341d' aa 

Example:

$ cat aa
@HWI-1KL135:70:C305EACXX:5:2105:6727:102841 1:N:0:CAGATC
CCAAGTGTCACCTCTTTTATTTATTGATTT@HWI-1KL135:70:C305EACXX:5:1101:1178:2203 1:N:0:CAGATC

$ sed -r '2s/^([^@]*)(@.*)$/\2/g; 1d' aa 
@HWI-1KL135:70:C305EACXX:5:1101:1178:2203 1:N:0:CAGATC
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

This might work for you (GNU sed):

sed '45603341!b;N;s/^.*\n[^@]*//' file

Leave as is any other line ecsept 45603341. On this line , append the following line and then remove everything from the start to the first non-@ in the the appended line.

potong
  • 55,640
  • 6
  • 51
  • 83
0

An alternative approach to 'sed' can be to use vim macros (This also works on Windows). The main disadvantage is that you will not be able to integrate inside scripts like 'sed' does. The main advantage is that it allows for complex replacements like "search for this pattern, then clear the line, go down 3 lines, move to column 40, switch lines,...). If you are already familiar with VIM it's also much more intuitive.

In this particular case you will have to do something like

qq (start macro recording)
/^@HWI.*CAGATC$ (search pattern)
dd (delete line)
vw (select word)
d (delete selected word)
q (end macro)

To run the macro 100 times:

100@q
earizon
  • 2,099
  • 19
  • 29